这是我尝试在openCV C ++中重新编码的matlab代码 原版的
cDist=regionprops(bwImg, 'Area');
cDist=[cDist.Area];
% Label each object
[bwImgLabeled, ~]=bwlabel(bwImg);
% Calculate min and max object size based on assumptions on the color
% checker size
maxLabelSize = prod(size(imageData)./[4 6]);
minLabelSize = prod(size(imageData)./[4 6]./10);
% Find label indices for objects that are too large or too small
remInd = find(cDist > maxLabelSize);
remInd = [remInd find(cDist < minLabelSize)];
这是我的代码:
bwImg.convertTo(bwImg,CV_8U);
cv::vector<cv::vector<cv::Point> > contours_1;
cv::findContours(bwImg,contours_1,CV_RETR_TREE,CV_CHAIN_APPROX_NONE,cv::Point(0,0));
//DEBUG contours
cv::Mat drawing1 = cv::Mat::zeros(bwImg.size(),CV_8UC3);
for (int i = 0; i < contours_1.size(); i++)
{
cv::RNG rng(12345);
cv::Scalar color = cv::Scalar( rng.uniform(0, 255), rng.uniform(0,255), rng.uniform(0,255) );
cv::drawContours(drawing1,contours_1,i,color,CV_FILLED);
}
//show contours update//
cv::imshow("contours",drawing1);
cv::waitKey(0);
drawing1.release();
//% Calculate min and max object size based on assumptions on the color
// % checker size
// maxLabelSize = prod(size(imageData)./[4 6]);
// minLabelSize = prod(size(imageData)./[4 6]./10);
double maxLabelSize = (bwImg.rows/4.0) * (bwImg.cols/6.0);
double minLabelSize = ((bwImg.rows/40.0) * (bwImg.cols/60.0));
// % Remove over/undersized objects
// for n=1:length(remInd)
// ri = bwImgLabeled == remInd(n);
// bwImgLabeled(ri) = 0;
// end
cv::vector<cv::vector<cv::Point> > goodContours;
for (int i = 0; i < contours_1.size(); i++)
{
double size = cv::contourArea(contours_1[i]);
if (size < maxLabelSize && size > minLabelSize)
{
goodContours.push_back(contours_1[i]);
}
}
然而,我没有得到相同的结果。在matlab中,我在openCV中获得更多轮廓。你能看出我的问题吗?