Ask Your Question
-1

How to segment a part of any object for counting purpose as per given binary image?

asked 2013-01-02 06:39:12 -0600

Rick2047 gravatar image

updated 2020-11-06 05:52:29 -0600

Objective: I want to count image description from image below.

image description

What are the ideas can work here?

I tried FindContour(). It returns boundary. Further I need to use those contour points. Using matchShape() and Contour.slice() is not helping. Any working example for this case will be very helpful.

Any help will be appreciated.

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
4

answered 2013-01-04 04:53:41 -0600

updated 2013-01-04 05:20:02 -0600

You can use template matching for solve this problem before matching you do preprocessing by canny & smooth.

void bhDrawBox2D(IplImage* srcImage,CvBox2D box2d,CvScalar color,int thickness)
{
  CvPoint2D32f pts[4];
  cvBoxPoints(box2d,pts);
  for (int i=0; i < 3;i++)
  {
      cvLine(srcImage, cvPointFrom32f(pts[i]),cvPointFrom32f(pts[i+1]),color,thickness);
  }
  cvLine(srcImage, cvPointFrom32f(pts[3]),cvPointFrom32f(pts[0]),color,thickness);

}
CvBox2D bhGetImageBox2d(IplImage* srcImage)
{
    CvBox2D result;
    IplImage* tempImg = cvCloneImage(srcImage);
    CvMemStorage* storage = cvCreateMemStorage(0);
    CvSeq* seq = 0;
     cvFindContours(tempImg,storage,&seq,sizeof(CvContour),CV_RETR_EXTERNAL);
    result = cvMinAreaRect2(seq,storage);

        cvReleaseMemStorage(&storage);
    cvReleaseImage(&tempImg);
    return result;
}
int main(int argc, char** argv )
{
    IplImage* srcImg = cvLoadImage("c:\\org.jpg",0);
    IplImage* tempImg = cvLoadImage("c:\\temp.jpg",0);
    cvThreshold(tempImg,tempImg,128,255,CV_THRESH_OTSU);
    IplImage* orgsrcImg = cvCloneImage(srcImg);

    cvNot(srcImg,srcImg);
    cvNot(tempImg,tempImg);



    CvBox2D tempBox = bhGetImageBox2d(tempImg);

    cvCanny(srcImg,srcImg,10,50);
    cvSmooth(srcImg,srcImg,CV_GAUSSIAN,9);
    cvCanny(tempImg,tempImg,10,50);
    cvSmooth(tempImg,tempImg,CV_GAUSSIAN,9);


    IplImage* resultImg;
    IplImage* resultImg2;
    resultImg = cvCreateImage(cvSize(srcImg->width-tempImg->width+1,srcImg->height-tempImg->height+1),32,1);
    resultImg2= cvCreateImage(cvSize(srcImg->width-tempImg->width+1,srcImg->height-tempImg->height+1),8,1);

    cvMatchTemplate(srcImg,tempImg,resultImg,2);
    double minV,maxV;

    cvNormalize(resultImg,resultImg2,0,255,CV_MINMAX);
    cvMinMaxLoc(resultImg2,&minV,&maxV);
    cvThreshold(resultImg2,resultImg2,maxV - 60,255,CV_THRESH_BINARY);

    CvMemStorage* storage = cvCreateMemStorage(0);
    CvSeq* seq = 0;
    cvFindContours(resultImg2,storage,&seq,sizeof(CvContour),CV_RETR_EXTERNAL);

    for (CvSeq* c= seq;c != 0 ;c = c->h_next)
    {
        CvRect r = cvBoundingRect(c);
        cvSetImageROI(resultImg,r);
        CvPoint minPnt,maxPnt;
        cvMinMaxLoc(resultImg,&minV,&maxV,&minPnt,&maxPnt);
        tempBox.center = cvPoint2D32f(maxPnt.x + r.x + tempImg->width/2,maxPnt.y+r.y + tempImg->height/2);

        bhDrawBox2D(orgsrcImg,tempBox,cvScalarAll(128),2);
        cvResetImageROI(resultImg);

    }
    cvReleaseMemStorage(&storage);
    cvShowImage("view",orgsrcImg);

    cvWaitKey(0);

}

image description

edit flag offensive delete link more

Comments

I need some time to start working on it. I got some other priorities. But this algo looks promising. Thanks.

Rick2047 gravatar imageRick2047 ( 2013-01-09 23:48:10 -0600 )edit

Very crude but solves the purpose. Also got a lot of ideas. Thanks.

Rick2047 gravatar imageRick2047 ( 2013-01-17 01:57:30 -0600 )edit

Question Tools

Stats

Asked: 2013-01-02 06:39:12 -0600

Seen: 2,695 times

Last updated: Jan 04 '13