answerOpenCV轮廓类问题解析

简介: contour在opencv中是一个基础的数据结构,灵活运用的话,作用很大。以contour为关键字,在answerOpenCV中能够发现很多有趣的东西。1、无法解决的问题http://answers.
contour在opencv中是一个基础的数据结构,灵活运用的话,作用很大。以contour为关键字,在answerOpenCV中能够发现很多有趣的东西。
img_491df057f17545c53011cac5ca86ee2c.png

1、无法解决的问题
img_16e1f3a19fa806bf16adfbfa4882c204.jpe
The problem is that. I just want to take the external contours of the main leaf in the image. Or If it is possible, The image can be cleaned. The second important point is that the process should be standard for all images.
解析:这个提问者希望从上面的叶子图片,得到叶子的轮廓。但实际上这是不可能完成的任务,这个图片质量达不到要求,关于这一点,是图像处理的常识。
也就是根本无法轮廓分析,因为没有稳定的轮廓。

2、如何从图像中获得完整轮廓

How to find dimensions of an object in the image


这道题厉害了,一看这个需求就是专业的:
I want to find the length of an object in the image (image length not the real physical length). My first idea is was to use boundingRect to find the dimensions, but some of the masks I have split in between them so the boundingRect method fails. Can someone suggest me a robust method to find the length of the object in the given mask

Mask with split
他需要从这个图中活动轮廓的长度(这个应该是一个脚印),但是因为图上轮廓可能有多个,所以不知道怎么办。
解析: 这道题的关键, 就在于实际上,每张图的轮廓只有一个。 这是重要的先验条件。那怎么办?把识别出来的轮廓连起来呀。
连的方法有多种,我给出两种比较保险:
1、形态学变化
img_9a95d87d96bc2b17e8530a4856de6101.png
    dilate(bw,bw,Mat(11,11,CV_8UC1));
    erode(bw,bw,Mat(11,11,CV_8UC1));
img_8822cd804ab7aa466390a86a542b6c6f.png
2、实在距离太远,靠不上了,直接把中线连起来吧
img_858d68e0bef972f8ca7a364e36614abc.jpe

由于这道题目前还没有比较好的解决方法,所以我实现了一个,应该是可以用的,这是结果:
img_b065e6c891cdbb3989679dd140893c8c.jpe
网站代码也已经提交到网站上了
//程序主要部分
int mainint argcchar** argv )
{
    //读入图像,转换为灰度
    Mat img = imread("e:/sandbox/1234.png");
    Mat bw;
    bool dRet;
    cvtColor(imgbwCOLOR_BGR2GRAY);
    //阈值处理
    threshold(bwbw, 150, 255, CV_THRESH_BINARY);
    bitwise_not(bw,bw);
    //形态学变化
    dilate(bw,bw,Mat(11,11,CV_8UC1));
    erode(bw,bw,Mat(11,11,CV_8UC1));
    //寻找轮廓
    vector<vector<Point> > contours;
    vector<Vec4ihierarchy;
    findContours(bwcontourshierarchyCV_RETR_LISTCV_CHAIN_APPROX_NONE);
    /// 计算矩
    vector<Momentsmu(contours.size() );
    forint i = 0; i < contours.size(); i++ )
        mu[i] = momentscontours[i], false ); 
    ///  计算中心矩:
    vector<Point2fmccontours.size() );
    forint i = 0; i < contours.size(); i++ )
        mc[i] = Point2fmu[i].m10/mu[i].m00 , mu[i].m01/mu[i].m00 ); 
    //connect all contours into ONE
    for (int i = 0; i < contours.size(); ++i)
    {
        Scalar color = Scalarrng12345.uniform(0, 255), rng12345.uniform(0,255), rng12345.uniform(0,255) );
        drawContoursimgcontoursicolor, 2, 8, hierarchy, 0, Point() );
        circleimgmc[i], 4, color, -1, 8, 0 );
        //connect
        if (i+1 <contours.size())
            line(bw,mc[i],mc[i+1],Scalar(255,255,255));
    }
    contours.clear();
    hierarchy.clear();
    //寻找结果
    findContours(bwcontourshierarchyCV_RETR_LISTCV_CHAIN_APPROX_NONE);
    for (int i = 0;i<contours.size();i++)
    {
        RotatedRect minRect = minAreaRectMat(contours[i]) );
        Point2f rect_points[4];
        minRect.pointsrect_points ); 
        forint j = 0; j < 4; j++ )
            lineimgrect_points[j], rect_points[(j+1)%4],Scalar(255,255,0),2);
        float fshort = std::min(minRect.size.width,minRect.size.height); //short
        float flong = std::max(minRect.size.width,minRect.size.height);  //long
    }
    imshow("img",img);
    waitKey();
    return 0;
}

3、新函数

Orientation of two contours


image description

这个topic希望能够获得两个轮廓之间的角度。并且后期通过旋转将两者重合。
I try to calculate the orientation of 2 contours. At the end i want to rotate one contour, so it is in cover with the other one. With my code I get a result, but it isn't that accurate. Also I get the same orientations although the contours is rotated around 90 degrees.

解析: 如果是我,一定会直接使用pca分别求出两个轮廓的角度,然后算差。但是原文中使用了,并且提出了独特的解决方法。

Shape Distance and Common Interfaces

https://docs.opencv.org/3.0-beta/modules/shape/doc/shape_distances.html#shapecontextdistanceextractor

Shape Distance algorithms in OpenCV are derivated from a common interface that allows you toswitch between them in a practical way for solving the same problem with different methods.Thus, all objects that implement shape distance measures inherit the ShapeDistanceExtractor interface.
当然,有了这个函数,做轮廓匹配也是非常方便:
image description
http://answers.opencv.org/question/28489/how-to-compare-two-contours-translated-from-one-another/
 
4、发现opencv的不足

I need a maxEnclosingCircle function


opencv目前是没有最大内接圆函数的(当然它还没有很多函数),但是这个只有研究要一定程度的人才会发现。这里他提问了,我帮助解决下:

img_b95eac4eaaf638b00adf7cf2710ef34f.jpe


# include   "stdafx.h"
# include   < iostream >
 
using   namespace  std;
using   namespace  cv;
 
VP FindBigestContour(Mat src){    
     int  imax  =   0 //代表最大轮廓的序号
     int  imaxcontour  =   - 1 //代表最大轮廓的大小
    std : : vector < std : : vector < cv : : Point >> contours;    
    findContours(src,contours,CV_RETR_LIST,CV_CHAIN_APPROX_SIMPLE);
     for  ( int  i = 0 ;i < contours.size();i ++ ){
         int  itmp  =   contourArea(contours[i]); //这里采用的是轮廓大小
         if  (imaxcontour  <  itmp ){
            imax  =  i;
            imaxcontour  =  itmp;
        }
    }
     return  contours[imax];
}
int  main( int  argc,  char *  argv[])
{
    Mat src  =  imread( "e:/template/cloud.png" );
    Mat temp;
    cvtColor(src,temp,COLOR_BGR2GRAY);
    threshold(temp,temp, 100 , 255 ,THRESH_OTSU);
    imshow( "src" ,temp);
     //寻找最大轮廓
    VP VPResult  =  FindBigestContour(temp);
     //寻找最大内切圆
     int  dist  =   0 ;
     int  maxdist  =   0 ;
    Point center;
     for ( int  i = 0 ;i < src.cols;i ++ )
    {
         for ( int  j = 0 ;j < src.rows;j ++ )
        {
            dist  =  pointPolygonTest(VPResult,cv : : Point(i,j), true );
             if (dist > maxdist)
            {
                maxdist = dist;
                center = cv : : Point(i,j);
            }
        }
    }
     //绘制结果
    circle(src,center,maxdist,Scalar( 0 , 0 , 255 ));
    imshow( "dst" ,src);
    waitKey();
}
    



另过程中,发现了pyimagesearch上的一些不错文章,感谢这个blog的作者的长期、高质量的付出,向他学习。
1、 Removing contours from an image using Python and OpenCV


3、 Finding extreme points in contours with OpenCV
https://www.pyimagesearch.com/2016/04/11/finding-extreme-points-in-contours-with-opencv/

结语:
实际上,最近我正在做关于轮廓的事情,这也是今天我做这个研究的直接原因。

我的问题是:如何识别出轮廓准确的长和宽
比如这张,其中2的这个外轮廓明显是识别错误的,这样它的长宽也是错误的(注意里面我标红的1和2)
image description

代码:
int mainint argcchar** argv )
{
    //read the image
    Mat img = imread("e:/sandbox/leaf.jpg");
    Mat bw;
    bool dRet;
    //resize
    pyrDown(img,img);
    pyrDown(img,img);
    cvtColor(imgbwCOLOR_BGR2GRAY);
    //morphology operation    
    threshold(bwbw, 150, 255, CV_THRESH_BINARY);
    //bitwise_not(bw,bw);
    //find and draw contours
    vector<vector<Point> > contours;
    vector<Vec4ihierarchy;
    findContours(bwcontourshierarchyCV_RETR_LISTCV_CHAIN_APPROX_NONE);
    for (int i = 0;i<contours.size();i++)
    {
        RotatedRect minRect = minAreaRectMat(contours[i]) );
        Point2f rect_points[4];
        minRect.pointsrect_points ); 
        forint j = 0; j < 4; j++ )
            lineimgrect_points[j], rect_points[(j+1)%4],Scalar(255,255,0),2);
    }
    imshow("img",img);
    waitKey();
    return 0;
}

我们要得到这样的结果
image description

当然,这个代码我已经差不多写出来了,如何获得轮廓的真实的长宽?这个问题很实际,opencv没有实现,目前看来answeropencv也人问?
就是这张图片?想看看大家的想法。也可以直接在answeropencv上进行讨论。
img_ca730f3c368cd889257eb5abf7a32d24.jpe

answerOpencv的讨论地址为:



感谢阅读至此,希望有所帮助。








目前方向:图像拼接融合、图像识别 联系方式:jsxyhelu@foxmail.com
目录
相关文章
|
26天前
|
存储 C++ 容器
C++入门指南:string类文档详细解析(非常经典,建议收藏)
C++入门指南:string类文档详细解析(非常经典,建议收藏)
34 0
|
27天前
|
XML 存储 Java
Spring重要类解析
Spring重要类解析
20 0
|
11天前
|
Java
Java 15 神秘登场:隐藏类解析未知领域
Java 15 神秘登场:隐藏类解析未知领域
15 0
|
28天前
|
存储 程序员 编译器
【C++ 模板类与虚函数】解析C++中的多态与泛型
【C++ 模板类与虚函数】解析C++中的多态与泛型
46 0
|
30天前
|
Python
Python类与对象:深入解析与应用
本文介绍了Python中的核心概念——类和对象,以及它们在面向对象编程中的应用。类是用户定义的类型,描述具有相同属性和行为的对象集合;对象是类的实例,具备类的属性和方法。文章通过示例讲解了如何定义类、创建及使用对象,包括`__init__`方法、属性访问和方法调用。此外,还阐述了类的继承,允许子类继承父类的属性和方法并进行扩展。掌握这些概念有助于提升Python编程的效率和灵活性。
|
1月前
|
存储 安全 程序员
【C++ 包装器类 智能指针】完全教程:std::unique_ptr、std::shared_ptr、std::weak_ptr的用法解析与优化 — 初学者至进阶指南
【C++ 包装器类 智能指针】完全教程:std::unique_ptr、std::shared_ptr、std::weak_ptr的用法解析与优化 — 初学者至进阶指南
68 0
|
1月前
|
消息中间件 Linux API
跨进程通信设计:Qt 进程间通讯类全面解析
跨进程通信设计:Qt 进程间通讯类全面解析
80 0
|
1月前
|
域名解析 缓存 网络协议
探索Qt 网络编程:网络地址与服务类全解析
探索Qt 网络编程:网络地址与服务类全解析
55 0
|
1月前
|
编译器 程序员 数据安全/隐私保护
C++类成员解析:编译器如何识别和处理声明与定义(C++ 类的作用域以及查找顺序)
C++类成员解析:编译器如何识别和处理声明与定义(C++ 类的作用域以及查找顺序)
14 0
|
1月前
|
安全 C++ 开发者
【C++多线程同步】C++多线程同步和互斥的关键:std::mutex和相关类的全面使用教程与深度解析
【C++多线程同步】C++多线程同步和互斥的关键:std::mutex和相关类的全面使用教程与深度解析
18 0

推荐镜像

更多