Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

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);

OpenCV's result

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

Matlab's result

Please note the left bottom square is missing from the openCV image.

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);

image description

OpenCV's result

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

image description

Matlab's result

Please note the left bottom square is missing from the openCV image.

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; 
}

image description

OpenCV's result

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

image description

Matlab's result

Please note the left bottom square is missing from the openCV image.

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; 
}

original image image description

OpenCV's result

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

image description

Matlab's result

Please note the left bottom square is missing from the openCV image.

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; 
}

original image image description

OpenCV's result

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

image description

Matlab's result

Please note the left bottom square is missing from the openCV image.image. should I do Canny edge detection before findContours?

click to hide/show revision 6
retagged

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; 
}

original image image description

OpenCV's result

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

image description

Matlab's result

Please note the left bottom square is missing from the openCV image. should I do Canny edge detection before findContours?