c++ - OpenCV copy bounded text area to new image -


i new opencv , using code bound text area in image. after filtering contours , putting bounded rectangle vector<rect> copy these new image.

mat large = img1; mat rgb; // downsample , use processing pyrup(large, rgb); mat small; cvtcolor(rgb, small, cv_bgr2gray); // morphological gradient mat grad; mat morphkernel = getstructuringelement(morph_ellipse, size(2, 2)); morphologyex(small, grad, morph_gradient, morphkernel); // binarize mat bw; threshold(grad, bw, 0.0, 255.0, thresh_binary | thresh_otsu); // connect horizontally oriented regions mat connected; //morphkernel = getstructuringelement(morph_rect, size(7, 1)); //morphologyex(bw, connected, morph_close, morphkernel); // find contours connected = bw; mat mask = mat::zeros(bw.size(), cv_8uc1);  mat mask2;  mat mask3; vector<vector<point>> contours; vector<vec4i> hierarchy; findcontours(connected, contours, hierarchy, cv_retr_ccomp, cv_chain_approx_simple, point(0, 0));  /*drawcontours(mask2, contours, -1, scalar(255), cv_filled); mat crop(img1.rows, img1.cols, cv_8uc3); crop.setto(scalar(0, 255, 0)); img1.copyto(crop, mask2); normalize(mask2.clone(), mask2, 0.0, 255.0, cv_minmax, cv_8uc1); */  vector<rect> rect1; int = 0; //filter contours  (int idx = 0; idx >= 0; idx = hierarchy[idx][0]) {      rect rect = boundingrect(contours[idx]);     mat maskroi(mask, rect);       maskroi = scalar(0, 0, 0);     // fill contour     drawcontours(mask, contours, idx, scalar(255, 255, 255), cv_filled);     // ratio of non-zero pixels in filled region     double r = (double)countnonzero(maskroi) / (rect.width*rect.height);      if (r > .45 /* assume @ least 45% of area filled if contains text */         &&         (rect.height > 10 && rect.width > 10 && rect.height<150 && rect.width<150) /* constraints on region size */         /* these 2 conditions alone not robust. better use         number of significant peaks in horizontal projection third condition */         )     {         //making rectangles on bounded area         rectangle(rgb, rect, scalar(0, 255, 0), 2);         //pushing bounding rectangles in vector new mask         rect1.push_back(rect);           } } 

input output getting after bounded text ares is:

after using code copy bounded area new mask

//copying  bounded rectangles area small new mask2 (int = 0; < rect1.size(); i++){     mask2 = rgb(rect1[i]); } 

but using last bounded text area:

how can or update mask2 rows or cols mapping of bounded text areas rgb mask2.

that's because mask2 equal last rgb(rect1[i]) called. can solve in 2 ways (using copyto):

  1. create mask (black initialized, same size input image), draw (white) rectangles. copy original image black initialized image of same size, using obtained mask.

  2. copy each sub-image directly black initialized image.

starting image, red rectangles detected rectangles:

enter image description here

with first approach you'll mask like:

enter image description here

and, both approaches, final result be:

enter image description here

code first approach:

#include <opencv2/opencv.hpp> #include <vector> using namespace std; using namespace cv;  int main() {     // image     mat3b img = imread("path_to_image");      // rectangles     vector<rect> rects{rect(100, 100, 100, 200), rect(300, 200, 200, 100), rect(500, 400, 80, 130)};      // mask rectangles (black initializeds)     mat1b mask(img.rows, img.cols, uchar(0));      mat3b dbgrects = img.clone();     (int = 0; < rects.size(); ++i)     {         // draw white rectangles on mask         rectangle(mask, rects[i], scalar(255), cv_filled);          // show rectangles         rectangle(dbgrects, rects[i], scalar(0, 0, 255), 2);     }      // black initizlied result     mat3b result(img.rows, img.cols, vec3b(0,0,0));     img.copyto(result, mask);      imshow("rectangles", dbgrects);     imshow("result", result);     waitkey();      return 0; } 

code second approach:

#include <opencv2/opencv.hpp> #include <vector> using namespace std; using namespace cv;  int main() {     // image     mat3b img = imread("path_to_image");      // rectangles     vector<rect> rects{rect(100, 100, 100, 200), rect(300, 200, 200, 100), rect(500, 400, 80, 130)};      // black initizlied result     mat3b result(img.rows, img.cols, vec3b(0, 0, 0));      mat3b dbgrects = img.clone();     (int = 0; < rects.size(); ++i)     {         img(rects[i]).copyto(result(rects[i]));          // show rectangles         rectangle(dbgrects, rects[i], scalar(0, 0, 255), 2);     }      imshow("rectangles", dbgrects);     imshow("result", result);     waitkey();      return 0; } 

Comments

Popular posts from this blog

c++ - llvm function pass ReplaceInstWithInst malloc -

Cross-Compiling Linux Kernel for Raspberry Pi - ${CCPREFIX}gcc -v does not work -

java.lang.NoClassDefFoundError When Creating New Android Project -