重叠形状识别(OpenCV) [英] Overlapping shapes recognition (OpenCV)

查看:666
本文介绍了重叠形状识别(OpenCV)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的图像,其中包含一些形状:一些矩形和一些椭圆,总数为4或5。可以旋转,缩放和重叠形状。有一个示例输入:

我的任务是检测所有这些数字并准备一些有关它们的信息:大小,位置,旋转度等。意见认为,核心问题是形状可以相互重叠的事实。我尝试搜索有关此类问题的一些信息,发现OpenCV库非常有用。



OpenCV可以检测轮廓,然后尝试拟合椭圆或这些轮廓的矩形。问题在于形状重叠时,轮廓混合在一起。



我考虑以下算法:检测所有特征点:并在其上放白点。我得到了类似的内容,其中每个数字都分为不同的部分:

然后我可以尝试使用一些信息来链接这些部分,例如复杂性值(我将曲线近似多边形适合轮廓,并读取了轮廓的多少个部分)。但这开始变得非常困难。另一个想法是尝试链接轮廓并将图形适合它们的所有排列。将输出最佳编译结果。



任何想法如何创建简单而优雅的解决方案?

解决方案

模糊图像有助于找到代码中所示的交点

  #include opencv2 / imgproc.hpp 
#include opencv2 / highgui.hpp

使用名称空间cv;

int main(int argc,char ** argv)
{
Mat src = imread(argv [1]);
垫子灰色,模糊;
cvtColor(src,grey,COLOR_BGR2GRAY);
阈值(grey,grey,127,255,THRESH_BINARY);
GaussianBlur(grey,blured,Size(),9);
阈值(模糊,模糊,200、255,THRESH_BINARY_INV);
gray.setTo(255,模糊);
imshow( result,gray);
waitKey();

返回0;
}

结果图像:



第2步



简单地说,是从



I have a simple image containing some shapes: some rectangles and some ellipses, in total number of 4 or 5. The shapes can be rotated, scaled and overlapped. There is a sample input: My task is to detect all of these figures and prepare some information about them: size, position, rotation, etc. In my opinion, the core problem is the fact that the shapes can be overlapped by each other. I tried to search some information about this kind of problem and find that OpenCV library can be very useful.

OpenCV has the ability to detect contours and then try to fit ellipses or rectangles to these contours. The problem is when shapes are overllaped, the contours are mixed up.

I think about following algorithm: detect all characteristic points: and put white dot at them. I got something like these where every figure is divided into separate sections: Then I can try to link these parts using some information, for example the complexity value (I fit the curve approxPolyDP to the contour and the read how many parts it has). But it starts to be very hard. The other idea is to try all of the permutations of linking the contours and trying to fit the figures to them. The best compilation will be output.

Any ideas how to create simple but elegant solution?

解决方案

blurring image helps to find intersections as seen in the code

#include "opencv2/imgproc.hpp"
#include "opencv2/highgui.hpp"

using namespace cv;

int main( int argc, char** argv )
{
    Mat src = imread( argv[1] );
    Mat gray, blurred;
    cvtColor( src, gray, COLOR_BGR2GRAY );
    threshold( gray, gray, 127, 255, THRESH_BINARY );
    GaussianBlur( gray, blurred, Size(), 9 );
    threshold( blurred, blurred, 200, 255, THRESH_BINARY_INV );
    gray.setTo( 255, blurred );
    imshow("result",gray);
    waitKey();

    return 0;
}

result image:

step 2

simply, borrowed code from generalContours_demo2.cpp

#include "opencv2/imgproc.hpp"
#include "opencv2/highgui.hpp"

using namespace cv;
using namespace std;

int main( int argc, char** argv )
{
    Mat src = imread( argv[1] );
    Mat gray, blurred;
    cvtColor( src, gray, COLOR_BGR2GRAY );
    threshold( gray, gray, 127, 255, THRESH_BINARY );
    GaussianBlur( gray, blurred, Size(), 5 );
    threshold( blurred, blurred, 180, 255, THRESH_BINARY_INV );
    gray.setTo( 255, blurred );
    imshow("result of step 1",gray);

    vector<vector<Point> > contours;

    /// Find contours
    findContours( gray.clone(), contours, RETR_TREE, CHAIN_APPROX_SIMPLE );

    /// Find the rotated rectangles and ellipses for each contour
    vector<RotatedRect> minRect( contours.size() );
    vector<RotatedRect> minEllipse( contours.size() );

    for( size_t i = 0; i < contours.size(); i++ )
    {
        minRect[i] = minAreaRect( Mat(contours[i]) );
        if( contours[i].size() > 5 )
        {
            minEllipse[i] = fitEllipse( Mat(contours[i]) );
        }
    }

    /// Draw contours + rotated rects + ellipses
    for( size_t i = 0; i< contours.size(); i++ )
    {
        Mat drawing = src.clone();
        // contour
        //drawContours( drawing, contours, (int)i, color, 1, 8, vector<Vec4i>(), 0, Point() );
        // ellipse
        ellipse( drawing, minEllipse[i], Scalar( 0, 0, 255 ), 2 );
        // rotated rectangle
        Point2f rect_points[4];
        minRect[i].points( rect_points );
        for( int j = 0; j < 4; j++ )
            line( drawing, rect_points[j], rect_points[(j+1)%4], Scalar( 0, 255, 0 ), 2 );
        /// Show in a window
        imshow( "results of step 2", drawing );
        waitKey();
    }

    return 0;
}

you can get following result images among others. i hope you will solve final step.

这篇关于重叠形状识别(OpenCV)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆