Giter VIP home page Giter VIP logo

Comments (4)

DanBloomberg avatar DanBloomberg commented on August 16, 2024

I'd suggest filtering for the red pixels, finding the bounding box of those red pixels, and cropping it out.

A simple way to do this is:
Pix *pix1 = pixRead()
Pix *pix2 = pixMaskOverColorPixels(pix1, 50, 1); // filter for red pixels
Box *box1;
pixClipToForeground(pix2, NULL, &box1); // get bounding box of red pixels
Pix *pix3 = pixClipRectangle(pix1, box1, NULL); // extract that region from input image

from leptonica.

jueqing1015 avatar jueqing1015 commented on August 16, 2024

@DanBloomberg
How to remove the noise in the picture above? I have tried other methods to remove it

from leptonica.

DanBloomberg avatar DanBloomberg commented on August 16, 2024

The image is grayscale. The noise has the characteristic that it is small but black, so thresholding will not help.

Because the noise is small you might think to use morphological filtering. That is a viable approach. However, because the text has thin regions, it would need to be done carefully, and with reconstructive seed-filling into connected components after removal of the noise. See prog/italic_reg.c for how this is done.

Another approach is to locate the connected components and filter out the small ones. This is simple.

Pix *pix4 = pixConvertTo1(pix3, 128);   // convert to 1 bpp
Pix *pix5 = pixSelectBySize(pix4, 2, 2, 8, L_SELECT_IF_EITHER, L_SELECT_IF_GT, 0);  // remove small noise

If you want to also remove the outline from the original red box, do this:

Pix *pix4 = pixConvertTo1(pix3, 128);   // convert to 1 bpp
pixSetOrClearBorder(pix4, 3, 3, 3, 3, PIX_CLR);   // clear the pixels within 3 from the border
Pix *pix5 = pixSelectBySize(pix4, 2, 2, 8, L_SELECT_IF_EITHER, L_SELECT_IF_GT, 0);  // remove small noise

Putting it all together:

Pix *pix1 = pixRead(<your input image>)
Pix *pix2 = pixMaskOverColorPixels(pix1, 50, 1); // filter for red pixels
Box *box1;
pixClipToForeground(pix2, NULL, &box1); // get bounding box of red pixels
Pix *pix3 = pixClipRectangle(pix1, box1, NULL); // extract that region from input image
Pix *pix4 = pixConvertTo1(pix3, 128);    // convert from rgb to 1 bpp
pixSetOrClearBorder(pix4, 3, 3, 3, 3, PIX_CLR);   // clear the pixels within 3 from the border
Pix *pix5 = pixSelectBySize(pix4, 2, 2, 8, L_SELECT_IF_EITHER, L_SELECT_IF_GT, 0);  // remove small noise

Note that the text is much thinner than the original you show above. However, the thinning occurred when I saved your image from github. It was not done by leptonica.

pix5

from leptonica.

DanBloomberg avatar DanBloomberg commented on August 16, 2024

here's the result with the border from the red pixels removed:

pix5

from leptonica.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.