【发布时间】:2016-03-24 23:17:07
【问题描述】:
代码是这样的:
Mat mat = imread("123.jpg");
cv::imshow("123.jpg",mat);
Mat hsv;
cvtColor(mat,hsv,COLOR_BGR2HSV);
Mat dst;
inRange(hsv,Scalar(0,0,49),Scalar(47,165,111),dst);
cout<<"dst channels:"<<dst.channels()<<endl;
int c0=0,c255=0,other=0;
for(int i=0;i<dst.rows;i++){
for(int j=0;j<dst.cols;j++){
int v = dst.at<uchar>(i,j);
if(v == 0){
c0++;
}else if(v == 255){
c255++;
}else{
other++;
}
}
}
cout<<"0 count:"<<c0<<",255 count:"<<c255<<",other value count:"<<other<<endl;
cv::erode(dst,dst,cv::Mat(),cv::Point(1,1),6);
cv::dilate(dst,dst,cv::Mat(),cv::Point(1,1),6);
cv::medianBlur(dst,dst,15);
cv::imshow("inRange",dst);
vector< vector<Point> > contours;
vector<Vec4i> hierarchy;
Mat temp;
dst.copyTo(temp);
findContours(temp,contours,hierarchy,CV_RETR_EXTERNAL,CV_CHAIN_APPROX_SIMPLE );
cout<<"contours size:"<<contours.size()<<endl;
cout<<"hierarchy size:"<<hierarchy.size()<<endl;
for (int index = 0; index >= 0; index = hierarchy[index][0]) {
cout<<"hierarchy at:"<<index<<","<<hierarchy[index]<<endl;
cv::drawContours(mat,contours,index,Scalar(120,255,0));
}
cv::imshow("contours",mat);
waitKey(0);
return 0;
我不知道为什么轮廓尺寸小于层次结构尺寸。实际上,应该有三个“EXTERNAL”形状。似乎错过了输出轮廓中的一种形状。这个问题困扰了我一天,我现在要发疯了...有人知道为什么轮廓中只有两个元素吗?我做错了什么吗?
【问题讨论】:
-
AFAIK 当你用 CV_RETR_EXTERNAL 参数层次调用 findContours() 时没有意义