leptonica icon indicating copy to clipboard operation
leptonica copied to clipboard

I want to use this control to remove the black edge around the image. What should I do? I'll understand it for the first time

Open jueqing1015 opened this issue 3 years ago • 4 comments

I want to use this control to remove the black edge around the image. What should I do? I'll understand it for the first time https://user-images.githubusercontent.com/65677359/131935495-f0d9951e-0111-4e95-8299-62db3475f3ba.jpg

jueqing1015 avatar Sep 15 '21 14:09 jueqing1015

do you want to fill in the black tears around the edge (with white pixels), so that the text is in a white rectangle with straight edges?

DanBloomberg avatar Sep 16 '21 17:09 DanBloomberg

@DanBloomberg Yes, I need to remove the black areas on the four sides, but the image is RGB However, the background color of the image is not necessarily white, but may also be other colors Just fill the surrounding black areas with white Is there any method? Please give guidance for the first use

jueqing1015 avatar Sep 17 '21 03:09 jueqing1015

Here's how you do it:

    pixa1 = pixaCreate(4);
    pix1 = pixRead("clean-edges.jpg");
    pixaAddPix(pixa1, pix1, L_COPY);

    // Convert to 1 bpp and remove fg components touching border
    // Result is 1 bpp image with border cleaned up
    pix2 = pixConvertTo1(pix1, 180);
    pix3 = pixRemoveBorderConnComps(pix2, 8);
    pixaAddPix(pixa1, pix3, L_INSERT);

    // If you want the result to be the original image (32 bpp) with
    // border components painted white (0xffffffff), get a mask of
    // the border components and paint through the mask onto the
    // original image
    pix4 = pixXor(NULL, pix2, pix3);
    pixaAddPix(pixa1, pix4, L_INSERT);
    pixPaintThroughMask(pix1, pix4, 0, 0, 0xffffffff);
    pixaAddPix(pixa1, pix1, L_INSERT);

    // Show the results
    pix5 = pixaDisplayTiledInColumns(pixa1, 1, 1.0, 20, 2);
    pixDisplay(pix5, 100, 800);
    pixWrite("/tmp/results.jpg", pix5, IFF_JFIF_JPEG);

DanBloomberg avatar Sep 21 '21 15:09 DanBloomberg

here's the final result; from the top:

  • original
  • cleaned up 1 bpp
  • border components mask
  • cleaned up 32 bpp (mask filled to white)

results

DanBloomberg avatar Sep 22 '21 03:09 DanBloomberg