Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

How to remove small black spots & make digits more clear, complete & sharp in image? [on hold]

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:

image description

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);

image description

With the above output image, now Im badly stuck on my last phase with 2 open queries:

  1. To erase the small black spots (black color)
  2. And make numbers, for example 0, 2, 6, 8, 9 etc & text NA, more complete, sharp & readable

Please advise & suggest, thanks a lot...

How to remove small black spots & make digits more clear, complete & sharp in image? [on hold]

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:

image description

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);

image description

With the above output image, now Im badly stuck on my last phase with 2 open queries:

  1. To erase the small black spots (black color)
  2. And make numbers, for example 0, 2, 6, 8, 9 etc & text NA, more complete, sharp & readable

Please advise & suggest, thanks a lot...

How to remove small black spots & make digits more clear, complete & sharp in image? [on hold]image?

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:

image description

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);

image description

With the above output image, now Im badly stuck on my last phase with 2 open queries:

  1. To erase the small black spots (black color)
  2. And make numbers, for example 0, 2, 6, 8, 9 etc & text NA, more complete, sharp & readable

Please advise & suggest, thanks a lot...

How to remove small black spots & make digits more clear, complete & sharp in image?

PS: Numbers are Enrollment IDs that are the combination of course + class + other ids etc

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:

image description

Im totally new to code in Opencv & VC++, C++, I googled and merged below working code from various sources so far:far, It reads a b/w image and removes a mark from it.

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);

image description

With the above output image, now Im badly stuck on my last phase with 2 open queries:

  1. To erase the Remove small black spots (black color)color) & noise
  2. And make numbers, for example 0, 2, 6, 8, 9 etc & text NA, more complete, sharp & readable

Please advise & suggest, thanks a lot...

click to hide/show revision 5
None

How to remove small black spots & make digits more clear, complete & sharp in image?

PS: Numbers are Enrollment IDs that are the combination of course + class + other ids etc

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:

image description

Im totally new to code in Opencv & C++, I googled and merged below code from various sources so far, It reads a b/w image and removes a mark from it.

im = imread(fpath + ".jpg", IMREAD_GRAYSCALE);
// 1. make a copy & approximate the background
bg = im.clone();
// get the structure & apply morphology
kernel2 = getStructuringElement(MORPH_RECT, Size(2 * 5 + 1, 2 * 5 + 1));    
morphologyEx(im, bg, CV_MOP_CLOSE, kernel2);
// threshold the difference image
threshold(dif, bw, 0, 255, CV_THRESH_BINARY_INV | CV_THRESH_OTSU);
// threshold the background image so we get dark region
threshold(bg, dark, 0, 255, CV_THRESH_BINARY_INV | CV_THRESH_OTSU);
// 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);
        }
    }
}
// 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++];
        }
    }
}
// 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);

image description

With the above output image, now Im badly stuck with 2 open queries:

  1. Remove small black spots (black color) & noise
  2. And make numbers, for example 0, 2, 6, 8, 9 etc & text NA, more complete, sharp & readable

Please advise & suggest, thanks a lot...

Im very new to Opencv but somehow Im able to load and display image, please refer this image for more clarity on what I mean:

image description

Going ahead with the help of getStructuringElement, morphologyEx & threshold functions, I could try little image processing.

The last sample code I tried before posting this query was boundingRect and findContours & below is what i got in result, it looks like some rectangles are including spots & noise AND I dont know how to move ahead from here... image description

Im badly stuck on below issues: 1. Clean black spots, noise and anything else except ID numbers 2. Make numbers like 0, 2, 6, 8, 9 more thick, complete and sharp

Kindly help out with some code sample, link or article which can help me.

Thanks in advance...