本文介绍了在opencv中找到对象的凸包?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已根据此处 a>,但是我无法获得图像的凸包(我使用的是与本教程中显示的图像相似的手形图像).我得到的源和边缘输出很好,但是应该绘制轮廓线和凸包线的"Drawings"输出没有显示任何内容,而是完全黑色.关于为什么会这样的任何想法?

I've written this based on the tutorial here but I'm unable to obtain the convex hull of the image (I'm using a similar hand image as shown in the tutorial). I get the source and edges output fine but the "Drawings" output which should draw the contour and convex hull lines don't show anything drawn and instead is completely black. Any ideas as to why this could be?

#include <opencv/cv.h>
#include <opencv/highgui.h>
#include <opencv/cxcore.h>

int main(int argc,char **argv)
{
    cvNamedWindow( "Source", 1 );
    cvNamedWindow( "edges window", 1 );
    cvNamedWindow( "Drawings", 1 );

    IplImage* src = cvLoadImage( "img.jpg", 0 );
    IplImage* edges = cvCreateImage( cvGetSize(src), 8, 1 );

    // Finding edges
    cvThreshold( src, edges, 150, 255, CV_THRESH_BINARY );

    CvMemStorage* storage = cvCreateMemStorage();
    CvSeq* first_contour = NULL;

    int Nc = cvFindContours(
        edges,
        storage,
        &first_contour,
        sizeof(CvContour),
        CV_RETR_LIST );

    // Finding convex Hull
    CvMemStorage* hull_storage = cvCreateMemStorage();
    CvSeq* retHulls = NULL;

    for(CvSeq* i = first_contour; i != 0; i = i->h_next){
    // note h_next is next sequence.
    retHulls = cvConvexHull2(first_contour,hull_storage,CV_CLOCKWISE,1);

    }

    // drawing contours and hull
    IplImage* draw = cvCreateImage(cvGetSize(edges), 8, 3 );

    for(CvSeq* i = first_contour; i != 0; i = i->h_next){
        cvDrawContours(draw,first_contour,cvScalar(255,0,0,0),cvScalar(255,0,0,0),0,1,8);
        cvDrawContours(draw,retHulls,cvScalar(255,0,0,0),cvScalar(255,0,0,0),0,1,8);

    }

    cvShowImage( "Source", src );
    cvShowImage( "edges window", edges );
    cvShowImage( "Drawings", draw );
    cvWaitKey();

    cvDestroyAllWindows();

    cvReleaseImage( &src );
    cvReleaseImage( &edges );
    cvReleaseImage( &draw );

    return 0;
}

推荐答案

您必须进行以下更改:

  1. cvFindContours函数中将参数从CV_RETR_LIST更改为CV_RETR_EXTERNAL.
  2. cvThreshold中将CV_THRESH_BINARY更改为CV_THRESH_OTSU.
  1. Change parameter from CV_RETR_LIST to CV_RETR_EXTERNAL in cvFindContours function.
  2. Change CV_THRESH_BINARY to CV_THRESH_OTSU in cvThreshold.

这里的证明(输入/输出):

Here's proof (input/output):

这篇关于在opencv中找到对象的凸包?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!