Segemenation image by block color

asked 2018-04-23 07:34:17 -0500

aniskraini@yahoo.fr gravatar image

updated 2018-04-24 02:58:57 -0500

Please any help to segment this image using clor filter. image description

int main() {    
Mat large = imread("result.png");

    Mat rgb;    // downsample and use it for processing    pyrDown(large, rgb);  

  Mat small;    

cvtColor(rgb, small, CV_BGR2GRAY);    // morphological gradient 

   Mat grad;

    Mat morphKernel = getStructuringElement(MORPH_ELLIPSE,    Size(3, 3));   

 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(9, 1));   
 morphologyEx(bw, connected, MORPH_CLOSE, morphKernel);   
 // find contours    Mat mask = Mat::zeros(bw.size(), CV_8UC1);   
 vector<vector<Point>> contours;    vector<Vec4i> hierarchy;   
 findContours(connected, contours, hierarchy, CV_RETR_CCOMP,    CV_CHAIN_APPROX_SIMPLE, Point(0, 0));

// filter contours    for(int idx = 0; idx >= 0; idx = hierarchy[idx][0])    {
       Rect rect = boundingRect(contours[idx]);
       Mat maskROI(mask, rect);
       maskROI = Scalar(0, 0, 0);
       // fill the contour
       drawContours(mask, contours, idx, Scalar(255, 255, 255),    CV_FILLED);
       // ratio of non-zero pixels in the filled region
       double r = (double)countNonZero(maskROI)/(rect.width*rect.height);

       if (r > .45 /* assume at least 45% of the area is filled if it    contains text */
           && 
           (rect.height > 8 && rect.width > 8) /* constraints on    region size */
           /* these two conditions alone are not very robust. better to    use something 
           like the number of significant peaks in a horizontal    projection as a third condition */
           )
       {
           rectangle(rgb, rect, Scalar(0, 255, 0), 2);
       }    }    imwrite(OUTPUT_FOLDER_PATH + string("rgb.jpg"), rgb);
    imshow("a", rgb); waitKey();    return 0; }
edit retag flag offensive close merge delete

Comments

what have you tried so far ?

berak gravatar imageberak ( 2018-04-24 01:37:04 -0500 )edit

In fact i need to sperate two block text by : split image based on two backgorund (white and blue).

aniskraini@yahoo.fr gravatar imageaniskraini@yahoo.fr ( 2018-04-24 02:40:09 -0500 )edit
1

again, show, what you've tried, then we can try to help you. (we won't write your program)

berak gravatar imageberak ( 2018-04-24 02:43:35 -0500 )edit
2

Thanks, my goal is to recognize text. The performence of tessercat-ocr depend on the text size. I need to sperate small text from big text by detecting various block text an then i can anlyse each block seperatly. Myn program is attached.

aniskraini@yahoo.fr gravatar imageaniskraini@yahoo.fr ( 2018-04-24 02:54:43 -0500 )edit