简体   繁体   中英

Matching regionprop(x,'Area') of Matlab in OpenCV

I need to imitate what regionprop(BW,'Area') of Matlab does with OpenCV.

I have this code in Matlab

[L,N]           = bwlabel(image1,8);
S = regionprops(L,'Area','PixelIdxList');
output=[];
for i=1:N,
    output(i)=S(i).Area;
end

And in OpenCV

cv::findContours(cvImageMat,contours,CV_RETR_LIST,CV_CHAIN_APPROX_NONE);
int iNumSegments = contours.size();
for(int i=0;i<iNumSegments;i++) 
{
    cv::vector<cv::Point> approx;
    cv::approxPolyDP(cv::Mat(contours[i]), approx, 1.0e-10, true);
    double area = fabs( cv::contourArea( approx ) );
    dvResult.push_back(area);
}

But each difference between two areas computed are about 400 in case of an image with 2048 x 2048 size. They should be the same for me to go forward. Does anybody have any advice on this?

Ended up creating my own function.

double cvArea(cv::Mat mat, cv::vector<cv::vector<cv::Point>> &contours, int index)
{
    cv::Mat mask = cv::Mat::zeros(mat.cols,mat.rows,CV_8UC1);
    cv::drawContours(mask,contours,index,cv::Scalar(1),CV_FILLED);
    return cv::sum(mask).val[0];
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM