Segemenation image by block color
Please any help to segment this image using clor filter.
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; }
what have you tried so far ?
In fact i need to sperate two block text by : split image based on two backgorund (white and blue).
again, show, what you've tried, then we can try to help you. (we won't write your program)
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.