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);
cvCvtColor(frame,imgHSV,CV_BGR2HSV);
imgFilter= cvCreateImage(cvSize(frame->width,frame->height),IPL_DEPTH_8U,1);
cvInRangeS(imgHSV,cvScalar(ui->hueSlide1->value(),ui->satSlide1->value(),ui->lumSlide1->value(),0),cvScalar(ui->hueSlide2->value(),ui->satSlide2->value(),ui->lumSlide2->value(),0),imgFilter);QImage imgThresdhed = QImage ((const uchar*)imgFilter->imageData,imgFilter->width,imgFilter->height,QImage::Format_Indexed8).rgbSwapped(); ui->filter->setPixmap(QPixmap::fromImage(imgThresdhed)); 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 ) { cvSeqRemove(contour,0); continue; } total =0; total_S=0; total_V=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); if((avg>=ui->hueSlide1->value())&&(avg<=ui->hueSlide2->value())&&(avg_S>=ui->satSlide1->value())&&(avg_S<=ui->satSlide2->value())&&(avg_V>=ui->lumSlide1->value())&&(avg_V<=ui->lumSlide2->value())) { 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; } } break; } } } } cvReleaseImage(&imgFilter); cvReleaseImage(&imgHSV);
}
@ERROR:
@OpenCV Error: Null pointer () in cvStartFindContours, file /home/pi/OpenCV-2.4.2/modules/imgproc/src/contours.cpp, line 187terminate 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":https://github.com/Itseez/opencv/blob/master/modules/imgproc/src/contours.cpp#L187 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.