Programming Assignment 3 : Pattern Recognition

这周的这个问题是在给定一系列的点中寻找多点共线的模式。

计算机视觉重要的两个部分:特征检测(Feature Dectection)和模式识别(Pattern Recognition)。特征检测提取出图片的重要特征,模式识别发掘出这些特征中的模式。这里探究的点共线的问题在现实生活中也有很多应用,比如统计数据分析。

Problem. 从二维平面上的N个互不相同的点中,绘制出每个(最多)连接的4个或4个以上点集合的线段。

Point data type. 给定的Point类型的API

public class Point implements Comparable<Point> {
       public final Comparator<Point> SLOPE_ORDER;        // compare points by slope to this point

       public Point(int x, int y)                         // construct the point (x, y)

       public   void draw()                               // draw this point
       public   void drawTo(Point that)                   // draw the line segment from this point to that point
       public String toString()                           // string representation

       public    int compareTo(Point that)                // is this point lexicographically smaller than that point?
       public double slopeTo(Point that)                  // the slope between this point and that point
}

draw(), drawTo(), toString()方法已经实现好了,需要实现compareTo(),slopeTo(),SLOPE_ORDER comparator.

compareTo(): 是比较按照y轴坐标排序。如果点(x0,y0)<(x1,y1),那么y0<y1,或者y0=y1,x0<x1.

slopeTo: 返回两个点的斜率。(y1-y0)/(x1-x0), 水平线段的斜率为Positive Zero, 垂直线段的斜率为Positive Infinity, 退化成一个点的线段斜率为Negative Infinity

SLOPE_ORDER comparator: 用来进行斜率的比较排序。使用一个调用点(x0, y0),如果(x1,y1)小于(x2,y2),当且仅当(y1-y0)/(x1-x0)小于(y2-y0)/(x2-x0)。这里直接调用slopeTo(), 水平,垂直,退化情况也已经考虑在里面了。

Brute force. 暴力的方法来寻找p,q,r,s这样的4个点在一条直线上共线。先对所有的点进行排序,枚举p,q,r,s,判断p-q, p-r, p-s是否斜率相同,如果找到了,那么我们就直接从p到s连接一条直线。时间复杂度最差为N^4,使用的空间比例为N。

int N = points.length;
Arrays.sort(points);
//BruteForce Implementation
for (int ip = 0; ip < N; ip++) {
    Point p = points[ip];
    for (int iq = ip + 1; iq < N; iq++) {
        Point q = points[iq];
        for (int ir = iq + 1; ir < N; ir++) {
            Point r = points[ir];
            for (int is = ir + 1; is < N; is++) {
                Point s = points[is];
                if (p.slopeTo(q) == p.slopeTo(r)
                        && p.slopeTo(q) == p.slopeTo(s)) {
                    p.drawTo(s);
                    StdOut.println(p.toString() + " -> "
                                       + q.toString() + " -> "
                                       + r.toString() + " -> "
                                       + s.toString());
                }
            }
        }
    }
}

A faster, sorting-based solution. 给一个点p, 对于其他的所有点,按照它们与p的斜率进行排序,那么如果存在相邻的3个点或以上的斜率相等,那么它们就共线。

这样算法只需要枚举所有的origin点(p点),然后把剩余点按照origin点的斜率排序,然后去在排序后的点中寻找是否有共线,复杂度为N(枚举)+NlogN(排序)+N(遍历查找),要求使用的空间比例为N。

注意统计时候不要出现共线的子线段和同一共线线段输出多次,这里使用compareTo()比较点的位置关系来避免。

Sample data files 里面有很多输入的例子可以拿来做测试。

Programming Assignment 3 : Pattern Recognition

时间: 2024-10-12 15:24:25

Programming Assignment 3 : Pattern Recognition的相关文章

Algorithm Part I:Programming Assignment(3)

问题描述: Programming Assignment 3: Pattern Recognition Write a program to recognize line patterns in a given set of points. Computer vision involves analyzing patterns in visual images and reconstructing the real-world objects that produced them. The pr

模式识别(Pattern Recognition)书单

Recommended Books Here is a list of books which I have read and feel it is worth recommending to friends who are interested in computer science. Machine Learning Pattern Recognition and Machine Learning Christopher M. Bishop A new treatment of classi

6CCS3PRE &amp; 7CCSMPNN Pattern Recognition

6CCS3PRE & 7CCSMPNN Pattern RecognitionCoursework Assignment 1This coursework is assessed. A type-written report needs to be submitted online through KEATSby the deadline specified on the module's KEATS webpage. Only include in your report theinforma

CMPT 459.1-19. Programming Assignment

---title: "CMPT 459.1-19. Programming Assignment 1"subtitle: "FIFA 19Players"author: "Name - Student ID"output: html_notebook---### IntroductionThe data has detailed attributes for every player registered inthe latest edition

Algorithm Part I:Programming Assignment(2)

问题描述: Programming Assignment 2: Randomized Queues and Deques Write a generic data type for a deque and a randomized queue. The goal of this assignment is to implement elementary data structures using arrays and linked lists, and to introduce you to g

Pattern Recognition and Machine Learning (preface translation)

前言 鉴于机器学习产生自计算机科学,图像识别却起源于工程学.然而,这些活动能被看做同一个领域的两个方面,并且他们同时在这过去的十年间经历了本质上的发展.特别是,当图像模型已经作为一个用来描述和应用概率模型的框架出现时,贝叶斯定理(Bayesian methods)就已经从一个专家级别的知识范畴发展成为主流.通过一系列近似算法推论,例如变分贝叶斯和期望传播(variational Bayes and expectation propagation),贝叶斯定理的实际适用范围也已经大幅度的提高.与此

Programming Assignment 4: 8 Puzzle

The Problem. 求解8数码问题.用最少的移动次数能使8数码还原. Best-first search.使用A*算法来解决,我们定义一个Seach Node,它是当前搜索局面的一种状态,记录了从初始到达当前状态的移动次数和上一个状态.初始化时候,当前状态移动次数为0,上一个状态为null,将其放入优先级队列,通过不断的从优先级队列中取出Seach Node去扩展下一级的状态,直到找到目标状态.对于优先级队列中优先级的定义我们可以采用:Hamming priority function 和

Programming Assignment 1: WordNet

题目地址:http://coursera.cs.princeton.edu/algs4/assignments/wordnet.html 1. 题目阅读 WordNet定义 WordNet是指一个包含唯一根的有向无环图,图中每一组词表示同一集合,每一条边v→w表示w是v的上位词.和树不同的地方是,每一个子节点可以有许多父节点. 输入格式 同义词表 文件中每行包含一次同义名词.首先是序号:然后是词,用空格分开.若为词组,则使用下划线连接词组.最后是同义名词的注释 36,AND_circuit AN

Algorithms: Design and Analysis, Part 1 - Programming Assignment #1

自我总结: 1.编程的思维不够,虽然分析有哪些需要的函数,但是不能比较好的汇总整合 2.写代码能力,容易挫败感,经常有bug,很烦心,耐心不够好 题目: In this programming assignment you will implement one or more of the integer multiplication algorithms described in lecture. To get the most out of this assignment, your pro