Im trying to write a program to remove a logo from image, clean it before sending it to Ocr program. Here is the input image:
Im totally new to code in Opencv & VC++, I googled and merged below working code from various sources so far:
im = imread(fpath + ".jpg", IMREAD_GRAYSCALE);
// 1. make a copy & approximate the background
bg = im.clone();
// 2. get the structure & apply morphology
kernel2 = getStructuringElement(MORPH_RECT, Size(2 * 5 + 1, 2 * 5 + 1));
morphologyEx(im, bg, CV_MOP_CLOSE, kernel2);
// 3. threshold the difference image
threshold(dif, bw, 0, 255, CV_THRESH_BINARY_INV | CV_THRESH_OTSU);
// 4. threshold the background image so we get dark region
threshold(bg, dark, 0, 255, CV_THRESH_BINARY_INV | CV_THRESH_OTSU);
// 5. extract pixels in the dark region
vector<unsigned char>darkpix(countNonZero(dark));
int index = 0;
for (int r = 0; r < dark.rows; r++)
{
for (int c = 0; c < dark.cols; c++)
{
if (dark.at<unsigned char>(r, c))
{
darkpix[index++] = im.at<unsigned char>(r, c);
}
}
}
// 6. threshold the dark region so we get the darker pixels inside it
threshold(darkpix, darkpix, 0, 255, CV_THRESH_BINARY | CV_THRESH_OTSU);
// paste the extracted darker pixels
index = 0;
for (int r = 0; r < dark.rows; r++)
{
for (int c = 0; c < dark.cols; c++)
{
if (dark.at<unsigned char>(r, c))
{
bw.at<unsigned char>(r, c) = darkpix[index++];
}
}
}
// 7. Clean image to make more readable and clear
adaptiveThreshold(bw, dst, 75, CV_ADAPTIVE_THRESH_MEAN_C, CV_THRESH_BINARY, 3, -15);
image_out = bw - dst;
imshow("Final", image_out);
With the above output image, now Im badly stuck on my last phase with 2 open queries:
- To erase the small black spots (black color)
- And make numbers, for example 0, 2, 6, 8, 9 etc & text NA, more complete, sharp & readable
Please advise & suggest, thanks a lot...