regionprops vs. findContours
Is there a way to get the same results for cDist=regionprops(bwImg, 'Area'); and openCV's findContours?
Here is what I have tried so far:
dst.convertTo(dst,CV_8U);
cv::vector<cv::vector<cv::Point> > contours_1;
cv::vector<cv::Vec4i> hierarchy_1;
cv::findContours(dst,contours_1,hierarchy_1,CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE);
double maxLabelSize = (dst.rows/4.0) * (dst.cols/6.0);
double minLabelSize = ((dst.rows/40.0) * (dst.cols/60.0));
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]);
}
}
cv::Mat filterContours = cv::Mat::zeros(dst.size(),CV_8UC3);
for (int i = 0; i < goodContours.size(); i++)
{
cv::RNG rng(12345);
cv::Scalar color = cv::Scalar( rng.uniform(0, 255), rng.uniform(0,255), rng.uniform(0,255) );
drawContours( filterContours, goodContours, i, color, 2, 8, hierarchy_1, 0, cv::Point() );
}
cv::imshow( "Contours", filterContours );
cv::waitKey(0);
the source image was created using average smoothing and
cv::Mat utils::im2bw(cv::Mat src, double grayThresh)
{
cv::Mat dst;
cv::threshold(src, dst, grayThresh, 1, CV_THRESH_BINARY);
return dst;
}
Matlab's Version:
% Calculate each separated object area
cDist=regionprops(bwImg, 'Area');
cDist=[cDist.Area];
% Label each object
[bwImgLabeled, ~]=bwlabel(bwImg);
% Calculate min and max object size based on assumptions
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)];
% Remove over/undersized objects
for n=1:length(remInd)
ri = bwImgLabeled == remInd(n);
bwImgLabeled(ri) = 0;
end
Please note the left bottom square is missing from the openCV image. should I do Canny edge detection before findContours?
your issue seems not to be the findContours() functionality but the pre-processing that you are applying before. If you could provide the source image and some more info/code about the way that you obtain the
dst
image that you pass into the findContours() function I am sure that we could provide some help ;-)well the image is a little too big to put it in here. but i can share the code.
you can resize it and just upload a smaller version of it ;-), also by source I was meaning the original input image before any thresholding/binarization. Moreover, it would help to tell us what you want to extract. By the images I suppose the squares on the image, is that right please correct if I am wrong.
I have uploaded the source images, which are both black and white images, and the function which creates the black and white image. I did some some smoothing filtering, but as you can see the images are identical.
Can it be that because i'm doing dst.convertTo(dst,CV_8U); i'm loosing some information ? what do i need to do ?
@Gilad Darmon please read again my previous comment.
Yes correct i want to extract the squares, uploaded the original image