How to find a color contours after convert img to black and white??

  • Hi all,

    I have already converted the img to black and white,

    i would like to find the contours and use a rectangle frame to show frame the wanted part.
    I am not sure how to achieve in Qt.

    I have tried using opencv, but fail, code as following

    @void Dialog::prcFrame()
    imgHSV= cvCloneImage(frame);
    imgFilter= cvCreateImage(cvSize(frame->width,frame->height),IPL_DEPTH_8U,1);

    QImage imgThresdhed = QImage ((const uchar*)imgFilter->imageData,imgFilter->width,imgFilter->height,QImage::Format_Indexed8).rgbSwapped();
    unsigned char *data_hsv= (unsigned char*)imgHSV->imageData;
    int step_hsv = imgHSV->widthStep/sizeof(unsigned char), chanels_hsv=imgHSV->nChannels;
    cvFindContours(imgFilter, storage, &contour, sizeof(CvContour), CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE,cvPoint(0,0));
        for(;contour;contour = contour->h_next)
            area1=fabs(cvContourArea(contour,CV_WHOLE_SEQ,1 ));
            if(area1<100 || area1>50000 )
            total =0;
            for(l = 0;l<contour->total;++l)
                pt = (CvPoint *)cvGetSeqElem(contour,l);
                H = data_hsv[step_hsv*pt->y+chanels_hsv*pt->x+0];
                S = data_hsv[step_hsv*pt->y+chanels_hsv*pt->x+1];
                V = data_hsv[step_hsv*pt->y+chanels_hsv*pt->x+2];
                total = H + total;
                total_S= S + total_S;
                total_V= V + total_V;
            avg = total / (contour->total);
            avg_S=total_S / (contour->total);
            avg_V=total_V / (contour->total);
                for(i = N-1; i >= 0; --i)
                  if(area1 > maxArea1[i])
                    maxArea1[i] = area1;
                    contours1[i] = contour;
                    for(m = (i-1); m >= 0; --m)
                       if(maxArea1[m] < maxArea1[m+1])
                         tmp_area1 = maxArea1[m+1];
                         tmp_cont = contours1[m+1];
                         maxArea1[m+1] = maxArea1[m];
                         contours1[m+1] = contours1[m];
                         maxArea1[m] = tmp_area1;
                         contours1[m] = tmp_cont;


    @OpenCV Error: Null pointer () in cvStartFindContours, file /home/pi/OpenCV-2.4.2/modules/imgproc/src/contours.cpp, line 187

    terminate called after throwing an instance of 'cv::Exception'

    what(): /home/pi/OpenCV-2.4.2/modules/imgproc/src/contours.cpp:187: error: (-27) in function cvStartFindContours

    The program has unexpectedly finished.

    /home/pi/qt/getCam/getCam exited with code 0@

    Please advise!
    Thanks a lot

  • [quote]OpenCV Error: Null pointer () in cvStartFindContours, file /home/pi/OpenCV-2.4.2/modules/imgproc/src/contours.cpp, line 187[/quote]

    According to "contours.cpp:187": your storage variable is nullptr

    I don't see where you declare/allocate this variable so make sure you do it right.

  • hi , thanks a lot!
    when i draw rectangle,
    I get weird value of my cvpoint() value, i also not sure what is the problem.

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.