尝试通过以下代码,它将为您的问题提供答案.
IplImage img=cvLoadImage("pathtosourceimage");
CvSize cvSize = cvSize(img.width(),img.height());
IplImage gry=cvCreateImage(cvSize,255,CV_THRESH_BINARY);
cvAdaptiveThreshold(gry,CV_ADAPTIVE_THRESH_MEAN_C,CV_THRESH_BINARY_INV,11,5);
CvMemStorage storage = CvMemStorage.create();
CvSeq contours = new CvContour(null);
int noOfContors = cvFindContours(gry,storage,CV_CHAIN_APPROX_NONE,new CvPoint(0,0));
CvSeq ptr = new CvSeq();
int count =1;
CvPoint p1 = new CvPoint(0,0),p2 = new CvPoint(0,0);
for (ptr = contours; ptr != null; ptr = ptr.h_next()) {
CvScalar color = CvScalar.BLUE;
CvRect sq = cvBoundingRect(ptr,0);
System.out.println("Contour No ="+count);
System.out.println("X ="+ sq.x()+" Y="+ sq.y());
System.out.println("Height ="+sq.height()+" Width ="+sq.width());
System.out.println("");
p1.x(sq.x());
p2.x(sq.x()+sq.width());
p1.y(sq.y());
p2.y(sq.y()+sq.height());
cvRectangle(img,p1,p2,CV_RGB(255,2,8,0);
cvDrawContours(img,ptr,color,CV_RGB(0,-1,CV_FILLED,0));
count++;
}
cvShowImage("contures",img);
cvWaitKey(0);
这是我给出的给定图像的输出.