Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

cv::cvtColor(mat, mat, CV_BGR2GRAY); cv::GaussianBlur(mat, mat, cv::Size(3,3), 0); cv::Mat kernel = cv::getStructuringElement(cv::MORPH_RECT, cv::Point(9,9)); cv::Mat dilated; cv::dilate(mat, dilated, kernel);

cv::Mat edges; cv::Canny(dilated, edges, 84, 3);

lines.clear(); cv::HoughLinesP(edges, lines, 1, CV_PI/180, 25); std::vector<cv::vec4i>::iterator it = lines.begin(); for(; it!=lines.end(); ++it) { cv::Vec4i l = *it; cv::line(edges, cv::Point(l[0], l[1]), cv::Point(l[2], l[3]), cv::Scalar(255,0,0), 2, 8); } std::vector< std::vector<cv::point> > contours; cv::findContours(edges, contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_TC89_KCOS); std::vector< std::vector<cv::point> > contoursCleaned; for (int i=0; i < contours.size(); i++) { if (cv::arcLength(contours[i], false) > 100) contoursCleaned.push_back(contours[i]); } std::vector<std::vector<cv::point> > contoursArea;

for (int i=0; i < contoursCleaned.size(); i++) { if (cv::contourArea(contoursCleaned[i]) > 10000){ contoursArea.push_back(contoursCleaned[i]); } } std::vector<std::vector<cv::point> > contoursDraw (contoursCleaned.size()); for (int i=0; i < contoursArea.size(); i++){ cv::approxPolyDP(Mat(contoursArea[i]), contoursDraw[i], 40, true); } Mat drawing = Mat::zeros( mat.size(), CV_8UC3 ); cv::drawContours(drawing, contoursDraw, -1, cv::Scalar(0,255,0),1);

I finished up making my own implementation:

cv::cvtColor(mat, mat, CV_BGR2GRAY);
cv::GaussianBlur(mat, mat, cv::Size(3,3), 0);
cv::Mat kernel = cv::getStructuringElement(cv::MORPH_RECT, cv::Point(9,9));
cv::Mat dilated;
cv::dilate(mat, dilated, kernel);

kernel);

cv::Mat edges; cv::Canny(dilated, edges, 84, 3);

3);

lines.clear(); cv::HoughLinesP(edges, lines, 1, CV_PI/180, 25); std::vector<cv::vec4i>::iterator std::vector<cv::Vec4i>::iterator it = lines.begin(); for(; it!=lines.end(); ++it) { cv::Vec4i l = *it; cv::line(edges, cv::Point(l[0], l[1]), cv::Point(l[2], l[3]), cv::Scalar(255,0,0), 2, 8); } std::vector< std::vector<cv::point> std::vector<cv::Point> > contours; cv::findContours(edges, contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_TC89_KCOS); std::vector< std::vector<cv::point> std::vector<cv::Point> > contoursCleaned; for (int i=0; i < contours.size(); i++) { if (cv::arcLength(contours[i], false) > 100) contoursCleaned.push_back(contours[i]); } std::vector<std::vector<cv::point> std::vector<std::vector<cv::Point> > contoursArea;

contoursArea;

for (int i=0; i < contoursCleaned.size(); i++) { if (cv::contourArea(contoursCleaned[i]) > 10000){ contoursArea.push_back(contoursCleaned[i]); } } std::vector<std::vector<cv::point> std::vector<std::vector<cv::Point> > contoursDraw (contoursCleaned.size()); for (int i=0; i < contoursArea.size(); i++){ cv::approxPolyDP(Mat(contoursArea[i]), contoursDraw[i], 40, true); } Mat drawing = Mat::zeros( mat.size(), CV_8UC3 ); cv::drawContours(drawing, contoursDraw, -1, cv::Scalar(0,255,0),1);

cv::Scalar(0,255,0),1);