thanks for reading this in advance.
I'm doing a credit card OCR related project, one step of which requires filling colors to an already contoured image.
Sample images include:
My goal is to fill the white space black:
Although it seems straightforward, I have failed to find a satisfying solution.
I was trying out the findContours
function, but I found it very confusing. After running the following function:
findContours( cvMatImage, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, cv::Point(0, 0) );
Strangely enough, not only the contour points were not correctly extracted, but also, the original input image seemed to have its own color inverted.
Any input regarding how I should complete my goal, or how to correctly use findContours
function is greatly appreciated.
Update:
Thanks a lot for your kind input.
In fact, the image included in my question has already been binarized -- and also I have applied an "adaptive thresholding" procedure to the image.
The original image looks like: https://i.stack.imgur.com/DkIKt.png
CGColorSpaceRef colorSpace = CGImageGetColorSpace(self.CGImage);
CGFloat cols = self.size.width;
CGFloat rows = self.size.height;
Mat cvMat(rows, cols, CV_8UC4); // 8 bits per component, 4 channels
CGContextRef contextRef = CGBitmapContextCreate(cvMat.data, // Pointer to data
cols, // Width of bitmap
rows, // Height of bitmap
8, // Bits per component
cvMat.step[0], // Bytes per row
colorSpace, // Colorspace
kCGImageAlphaNoneSkipLast |
kCGBitmapByteOrderDefault); // Bitmap info flags
CGContextDrawImage(contextRef, CGRectMake(0, 0, cols, rows), self.CGImage);
CGContextRelease(contextRef);
CGColorSpaceRelease(colorSpace);
Mat cvImage_gray, cvImage_threshold;
cvtColor(cvImageOrg, cvImage_gray, CV_RGB2GRAY); //Binarize
adaptiveThreshold(cvImage_gray, cvImage_threshold, 255, ADAPTIVE_THRESH_MEAN_C, THRESH_BINARY, neighbor, threshRed); //Adaptive threshold