使用areaContours(OpenCV)绘制最大元素连接 [英] Draw the biggest element conncted using areaContours (OpenCV )

查看:818
本文介绍了使用areaContours(OpenCV)绘制最大元素连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

im new in this topic.i'm尝试使用一些openCV库来做一个项目,但我有一些问题findContour,drawContours.After我读取一个输入图像并做一个阈值,我使用findContours在代码

i m new in this topic.i'm trying to use some openCV library to make a project but i have some problems with findContour,drawContours.After i read an input image and make a tresholding , i use findContours as in the code

cv::Mat cont;    // i create a matrix


    result2.copyTo(cont);  // this is the copy of the input image tresholded    
    std::vector<std::vector<cv::Point> > contours;          
    std::vector<cv::Vec4i> hierarchy;
            cv::findContours(cont,contours, hierarchy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE,  cv::Point(0,0 ));

    eiid::imshow("Im_Find", cont);   // I watch the points
            for( int s = 0; s< contours.size(); s++ )
    {

     printf(" * Contour[%d]=  Area OpenCV: %.2f \n", s,cv::contourArea(contours[s])) ;   
     drawContours( cont, contours, s, cv::Scalar(255,255), 10, 1, hierarchy, 0, cv::Point() );          
 }
   eiid::imshow ( "FINE" , cont);

我有7个元素连接,所以7 areaContours
这是我的目标:knowing我的所有元素的区域,我想画,画画,只有最大的区域(因此排除其他6个区域)
我不能做,有人可以帮助我吗?
(我试图存储我的7个区域在双阵列,但我不能去:()

I have 7 elements connected, ( so 7 areaContours) This is my objective: knowing the areas of my all elemets, I want to draw, to paint, only the biggest area (and so exclude the other 6 areas ) I can't do it , someone can help me please? (I tried to store my 7 areas in double array but i can't go on :( )

感谢回答我的男人,但我还是一个问题你的代码工作,但我不知道为什么一些像素仍然在图像...当我看最终的形象,我看到我最大的轮廓,但也有一些白色像素:( ..问题是,我有使用这个算法更多的图片,所以我的代码必须是所有的图片的标准..可能我不正确地使用的功能

Thanks to answer me man, but i still have a problem. Your code works but i don't know why some pixels are still in the image... When i watch the final image i see my biggest contour but also some white pixels :( .. The problem is that i have to use this algoritm for more pictures, so my code has to be standard for all pictures..Maybe i use uncorrectly the functions??

我的图像是黑白的,但颜色(255,255)我认为是好的我的情况。Howerver我明白我的问题是findContours放像素在我的图像,所以我复制在其他图像,现在我没有问题。然而我仍然有一个问题:(现在我有最大的轮廓,但我需要应用它在我的原始图像排除一些文本从它。问题是,最大的轮廓的内部是黑色,但在我的原始图像,内部是白色的...所以我不能得到一个操作像少 - (interseption).... :(我怎么能做人?

My image is black and white but Color(255,255) i think is good in my case. Howerver i understood my problem is that findContours put pixels in my image,so i copyed in other image and now i don t have problems. HOwever i still have a problem :( Now i have the biggest contour,but i need to apply it on my original image to exclude some text from it..The problem is that the inside of the biggest contour is black , but in my original image , the inside is white...so i can 't get a operation like " less - " (interseption) ....:( how can i do man?

推荐答案

我们去。

cv::Mat findBiggestBlob(cv::Mat & matImage){
    int largest_area=0;
    int largest_contour_index=0;

    vector< vector<Point> > contours; // Vector for storing contour
    vector<Vec4i> hierarchy;

    findContours( matImage, contours, hierarchy,CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE ); // Find the contours in the image

    for( int i = 0; i< contours.size(); i++ ) {// iterate through each contour. 
        double a=contourArea( contours[i],false);  //  Find the area of contour
        if(a>largest_area){
            largest_area=a;
            largest_contour_index=i;                //Store the index of largest contour
            //bounding_rect=boundingRect(contours[i]); // Find the bounding rectangle for biggest contour
        }
    }

    drawContours( matImage, contours, largest_contour_index, Scalar(255), CV_FILLED, 8, hierarchy ); // Draw the largest contour using previously stored index.
    return matImage;
}

这篇关于使用areaContours(OpenCV)绘制最大元素连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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