How to extract lines from each contour in OpenCV for Android? -
i'd examine each canny detected edge , main lines in (to check if seem shape rectangle, example if 2 pairs of lines parallel etc.).
imgproc.houghlinesp want, gives lines whole image, , want know lines come same edges.
i tried findcontours, , looking main lines in each contour approxpolydp, doesn't adapted because there gaps in canny detected edges. gives contours of edges , not edges themselves.
here test image example :
how can set of lines each shape ?
if you're using opencv 3.0.0 can use linesegmentdetector
, , "and" detected lines contours.
i provide sample code below. it's c++ (sorry that), can translate in java. @ least see how use linesegmentdetector
, how extract common lines each contour. you'll see lines on same contour same color.
#include <opencv2/opencv.hpp> using namespace cv; using namespace std; int main() { rng rng(12345); mat3b img = imread("path_to_image"); mat1b gray; cvtcolor(img, gray, color_bgr2gray); mat3b result; cvtcolor(gray, result, color_gray2bgr); // detect lines ptr<linesegmentdetector> detector = createlinesegmentdetector(); vector<vec4i> lines; detector->detect(gray, lines); // draw lines mat1b linemask(gray.size(), uchar(0)); (int = 0; < lines.size(); ++i) { line(linemask, point(lines[i][0], lines[i][1]), point(lines[i][2], lines[i][3]), scalar(255), 2); } // compute edges mat1b edges; canny(gray, edges, 200, 400); // find contours vector<vector<point>> contours; findcontours(edges.clone(), contours, cv_retr_external, cv_chain_approx_none); (int = 0; < contours.size(); ++i) { // draw each contour mat1b contourmask(gray.size(), uchar(0)); drawcontours(contourmask, contours, i, scalar(255), 2); // better use 1 here. 2 visualization purposes // , contour , lines mat1b bor; bitwise_and(contourmask, linemask, bor); // draw common pixels random color vector<point> common; findnonzero(bor, common); vec3b color = vec3b(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255)); (int j = 0; j < common.size(); ++j) { result(common[j]) = color; } } imshow("result", result); waitkey(); return 0; }
Comments
Post a Comment