• opencv入门笔记之三 简单图像识别,识别线,点,圆,轮廓


    Edge detection

    int main(){
    IplImage* girl = cvLoadImage(“road.jpg”, 0);
    // convert color to grag
    Mat boy;
    //*************start******************************///
    Mat cow = Mat(girl);
    //Mat() convert IplImage to Mat
    imshow(“cow”, cow);
    Canny(cow, boy, 125, 350);
    //125< |pixel - pixel| <350 (hysteresis thresholding)
    imshow(“boy”, boy);
    Mat Inv;
    threshold(boy, Inv, 128, 255, THRESH_BINARY_INV);
    // invert color
    //*****************end**************************///
    duration = static_cast(getTickCount()) - duration;
    duration /= getTickFrequency();
    cout << duration;
    //////////////////////////////////////////////////////////////////////////
    imshow(“Inv”, Inv);
    waitKey(0);

    }

    2/5/2015 10:12 AM - Screen Clipping

    Lines detection
    Mat boundless = Mat(cvLoadImage(“road.jpg”, 0));
    //convert color to grey
    Mat bound;
    Canny(boundless, bound, 125, 350);
    vector lines;
    HoughLines(bound, lines, 1, M_PI / 180, 80);
    // if u wanna use PI there must be a #define _USE_MATH_DEFINES before math.h
    vector::const_iterator it = lines.begin();
    while (it != lines.end()){
    float rho = (*it)[0];
    float theta = (*it)[1];
    if (theta < M_PI_4||theta>3*M_PI_4)
    {
    Point pt1(rho / cos(theta), 0);
    Point pt2(rho - bound.rows*sin(theta) / cos(theta), bound.rows);
    line(boundless,pt1, pt2, Scalar(255), 1);
    }
    else
    {
    Point pt1(0, rho / sin(theta));
    Point pt2(bound.cols, rho - bound.cols*cos(theta) / sin(theta));
    line(boundless, pt1, pt2, Scalar(255), 1);
    }
    ++it;
    }
    imshow(“new”, boundless);

    Probabilistic Hough transform
    class findline
    {
    public:
    findline() :deltarho(1), deltatheta(M_PI / 180), minvote(10), minlength(0.), maxGap(0.){}
    // ~findline();
    void setAccResolution(double rho=1, double theta=M_PI/180){
    deltatheta = theta;
    deltarho = rho;
    }// smaller deltatheta and deltarho means more time and more precise
    void setMinVote(int minv){
    minvote = minv;
    }// more votes, less lines
    void setLineLengthAndGap(double length, double gap){
    minlength = length;
    maxGap = gap;
    }// gap between lines and length of lines
    vector findLines(Mat& binary){
    lines.clear();
    HoughLinesP(binary, lines, deltarho, deltatheta, minvote, minlength, maxGap);
    return lines;
    } //use probabilistic hough transform
    void drawDetectedLines(Mat &image, Scalar color=Scalar(255,255,255)){
    vector::const_iterator it2 = lines.begin();
    while(it2 != lines.end()){
    Point pt1((*it2)[0], (*it2)[1]);
    Point pt2((*it2)[2], (*it2)[3]);
    line(image, pt1, pt2, color);
    ++it2;
    }
    }// use const_iterator to draw lines

    private:
    Mat image;
    vector lines;
    double deltarho, deltatheta;
    int minvote;
    double minlength;
    double maxGap;
    };

    void m(){
    findline finder;
    finder.setMinVote(80);
    finder.setLineLengthAndGap(100, 20);
    Mat image = imread(“ground.jpg”);
    Mat coutours = Mat(cvLoadImage(“ground.jpg”, 0));
    Canny(coutours, coutours, 125, 350);
    // imshow(“hen”, coutours);
    vector lines = finder.findLines(coutours);
    finder.drawDetectedLines(image,Scalar(0,0,0));
    imshow(“Hough”, image);
    imwrite(“ok.jpg”, image);
    }

    vector

  • 相关阅读:
    Logstash-input-jdbc同步mysql数据到ES - sql_last_value
    filebeat7.5.2 在 windows server 2008 R2 设置系统服务报错
    mysql8.0.19压缩版安装
    不是RESTful不好,是你姿势有问题
    logback运行时动态创建日志文件
    jpa 主键重复导致查询list的数据总是重复第一条数据
    解决MySQL Workbench导出低版本MySQL时报错Unknown table ‘column_statistics’ in information_schema的问题
    windows10系统修改JDK版本后配置环境变量不生效怎么办
    application.properties 中文乱码问题解决
    JAVA抽象类和抽象方法(abstract)
  • 原文地址:https://www.cnblogs.com/Pomodori/p/4316620.html
Copyright © 2020-2023  润新知