Opencv - findContours

cv::Mat src_gray;
cv::cvtColor(matROI_IDCard, src_gray, cv::COLOR_BGR2GRAY);
cv::Mat filtered;
cv::GaussianBlur(src_gray, filtered, cv::Size(3, 3), 0);
Mat threshMat;
cv::adaptiveThreshold(src_gray, threshMat, 255, ADAPTIVE_THRESH_GAUSSIAN_C, THRESH_BINARY, 39 ,15);
cv::imwrite("/sdcard/threshMat.jpg", threshMat);
// Find contours and store them in a list
std::vector<std::vector<cv::Point> > contours;
cv::findContours(threshMat, contours, cv::RETR_CCOMP, cv::CHAIN_APPROX_SIMPLE);

std::vector<std::vector<cv::Point> > contours2;
for(auto c : contours)
{
if(cv::contourArea(c) > 100)
{
contours2.push_back(c);
Rect temp = cv::boundingRect(c);
if(abs(temp.width - temp.height) < 30 && temp.area() < threshMat.cols * threshMat.rows * 0.5 && temp.area() > threshMat.cols * threshMat.rows * 0.1) {
rectangle(matROI_IDCard, temp, Scalar(0, 0, 255), 1);
break;
}
}
}

//cv::drawContours(matROI_IDCard, contours2, -1, Scalar(0,0,255), -1);
cv::imwrite("/sdcard/out_dilated2.jpg", matROI_IDCard);
posted @ 2017-10-13 14:43  飞晨信息  阅读(209)  评论(0)    收藏  举报