Skip to content

Commit

Permalink
Add pixCropImage() option to aggressively remove side noise
Browse files Browse the repository at this point in the history
* This is from the left and right sides, and is invoked in prog/croppdf
  by using a negative input paramater for edgeclean.
* New prog/misctest2.c to have page cropping and cleaning examples,
  with two sample music notation images.  Existing cropping and cleaning
  examples have been removed from prog/misctest1.c.
  • Loading branch information
DanBloomberg committed May 24, 2024
1 parent 3770899 commit 1e7c3a9
Show file tree
Hide file tree
Showing 14 changed files with 214 additions and 57 deletions.
1 change: 1 addition & 0 deletions prog/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,7 @@ add_prog_target(maketile maketile.c)
add_prog_target(maptest maptest.c)
add_prog_target(messagetest messagetest.c)
add_prog_target(misctest1 misctest1.c)
add_prog_target(misctest2 misctest2.c)
add_prog_target(modifyhuesat modifyhuesat.c)
add_prog_target(morphseq_reg morphseq_reg.c)
add_prog_target(morphtest1 morphtest1.c)
Expand Down
2 changes: 1 addition & 1 deletion prog/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ OTHER_PROGS = adaptmap_dark \
listtest lightcolortest livre_adapt livre_hmt \
livre_makefigs livre_orient \
livre_pageseg livre_seedgen livre_tophat \
maketile maptest messagetest misctest1 \
maketile maptest messagetest misctest1 misctest2 \
modifyhuesat morphtest1 \
numaranktest otsutest1 otsutest2 \
pagesegtest1 pagesegtest2 \
Expand Down
Binary file added prog/boismort.1.tif
Binary file not shown.
Binary file added prog/boismort.15.tif
Binary file not shown.
6 changes: 4 additions & 2 deletions prog/croppdf.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,10 @@
* The %lrclear and %tbclear parameters give the number of background
* pixels to be added to the foreground region.
*
* The %edgeclean parameter is used to remove edge noise, going from
* 0 (default, no removal) to 15 (maximally aggressive removal).
* The %edgeclean parameter is used to remove edge noise:
* -1: aggressively removes left and right side noise
* 0: default, no removal
* 1-15: removal of random noise, where 15 is maximally aggressive
*
* The suggested value for %lradd and %tbadd is 50. Laser printers do not
* print foreground pixels very close to the page edges, and using a
Expand Down
10 changes: 7 additions & 3 deletions prog/makefile.static
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,8 @@ SRC = adaptmap_reg.c adaptnorm_reg.c affine_reg.c \
lightcolortest.c listtest.c \
livre_adapt.c livre_hmt.c livre_makefigs.c livre_orient.c \
livre_pageseg.c livre_seedgen.c livre_tophat.c \
maketile.c maptest.c messagetest.c misctest1.c \
maketile.c maptest.c messagetest.c \
misctest1.c misctest2.c \
modifyhuesat.c morphtest1.c \
numaranktest.c otsutest1.c otsutest2.c \
pagesegtest1.c pagesegtest2.c \
Expand Down Expand Up @@ -516,10 +517,10 @@ findcorners_reg: findcorners_reg.o $(LEPTLIB)
$(CC) -o findcorners_reg findcorners_reg.o $(ALL_LIBS) $(EXTRALIBS)

findpattern1_reg: findpattern1_reg.o $(LEPTLIB)
$(CC) -o findpattern_reg findpattern1_reg.o $(ALL_LIBS) $(EXTRALIBS)
$(CC) -o findpattern1_reg findpattern1_reg.o $(ALL_LIBS) $(EXTRALIBS)

findpattern2_reg: findpattern2_reg.o $(LEPTLIB)
$(CC) -o findpattern_reg findpattern2_reg.o $(ALL_LIBS) $(EXTRALIBS)
$(CC) -o findpattern2_reg findpattern2_reg.o $(ALL_LIBS) $(EXTRALIBS)

flipdetect_reg: flipdetect_reg.o $(LEPTLIB)
$(CC) -o flipdetect_reg flipdetect_reg.o $(ALL_LIBS) $(EXTRALIBS)
Expand Down Expand Up @@ -1037,6 +1038,9 @@ messagetest: messagetest.o $(LEPTLIB)
misctest1: misctest1.o $(LEPTLIB)
$(CC) -o misctest1 misctest1.o $(ALL_LIBS) $(EXTRALIBS)

misctest2: misctest2.o $(LEPTLIB)
$(CC) -o misctest2 misctest2.o $(ALL_LIBS) $(EXTRALIBS)

modifyhuesat: modifyhuesat.o $(LEPTLIB)
$(CC) -o modifyhuesat modifyhuesat.o $(ALL_LIBS) $(EXTRALIBS)

Expand Down
32 changes: 0 additions & 32 deletions prog/misctest1.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@
* * Show sampled scaling with and without source indexing shift
* * Display differences in images with pixDisplayDiff()
* * Demonstrate read of cmap+alpha png, and I/O of rgba pnm, bmp, webp
* * Demonstrate image cropping function
* * Demonstrate image cleaning function
*/

#ifdef HAVE_CONFIG_H
Expand Down Expand Up @@ -414,35 +412,5 @@ PIXCMAP *cmap, *cmapg;
#endif /* HAVE_LIBWEBP */
pixDestroy(&pix1);

/* Page cropping */
pix1 = pixRead("tel_3.tif");
pix2 = pixCropImage(pix1, 30, 30, 4, 25, 25, 1.15,
"/tmp/lept/misc/cropdebug.pdf", NULL);
pixDestroy(&pix1);
pixDestroy(&pix2);

/* Page cleaning */
pixa1 = pixaCreate(3);
pix1 = pixRead("tel_3.tif");
pix2 = pixRotate(pix1, 0.02, L_ROTATE_SAMPLING, L_BRING_IN_WHITE, 0, 0);
pix3 = pixCleanImage(pix2, 1, 0, 1, 0);
pixaAddPix(pixa1, pix3, L_INSERT);
pixDisplay(pix3, 800, 800);
pixDestroy(&pix1);
pixDestroy(&pix2);
pix1 = pixRead("w91frag.jpg");
pixaAddPix(pixa1, pixScale(pix1, 2.5, 2.5), L_INSERT);
pix2 = pixRotate(pix1, 0.02, L_ROTATE_AREA_MAP, L_BRING_IN_WHITE, 0, 0);
pix3 = pixCleanImage(pix2, 1, 0, 1, 0);
pixaAddPix(pixa1, pixScale(pix3, 2.5, 2.5), L_INSERT);
pixDisplay(pix3, 1200, 800);
pixDestroy(&pix1);
pixDestroy(&pix2);
pixDestroy(&pix3);
lept_stderr("Writing /tmp/lept/misc/pageclean.pdf\n");
pixaConvertToPdf(pixa1, 0, 1.0, L_DEFAULT_ENCODE, 50, NULL,
"/tmp/lept/misc/pageclean.pdf");
pixaDestroy(&pixa1);

return 0;
}
94 changes: 94 additions & 0 deletions prog/misctest2.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*====================================================================*
- Copyright (C) 2001 Leptonica. All rights reserved.
-
- Redistribution and use in source and binary forms, with or without
- modification, are permitted provided that the following conditions
- are met:
- 1. Redistributions of source code must retain the above copyright
- notice, this list of conditions and the following disclaimer.
- 2. Redistributions in binary form must reproduce the above
- copyright notice, this list of conditions and the following
- disclaimer in the documentation and/or other materials
- provided with the distribution.
-
- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
- A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ANY
- CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
- EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
- PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
- PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
- OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
- NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
- SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*====================================================================*/

/*
* misctest2.c
* * Page cropping with light filtering
* * Page cropping with removal of fg on left and right sides
* * Demonstrate image cleaning function
*/

#ifdef HAVE_CONFIG_H
#include <config_auto.h>
#endif /* HAVE_CONFIG_H */

#include "allheaders.h"

int main(int argc,
char **argv)
{
PIX *pix1, *pix2, *pix3;
PIXA *pixa1;

setLeptDebugOK(1);
lept_mkdir("lept/misc");

/* Page cropping with light filtering */
pix1 = pixRead("tel_3.tif");
pix2 = pixCropImage(pix1, 30, 30, 4, 25, 25, 1.15,
"/tmp/lept/misc/crop_tel3.pdf", NULL);
pixDestroy(&pix1);
pixDestroy(&pix2);

/* Page cropping with removal of fg on left and right sides */
pix1 = pixRead("boismort.1.tif");
pix2 = pixCropImage(pix1, 50, 50, -1, 70, 70, 1.1,
"/tmp/lept/misc/crop_bois1.pdf", NULL);
pixDestroy(&pix1);
pixDestroy(&pix2);

/* Page cropping with removal of fg on left and right sides */
pix1 = pixRead("boismort.15.tif");
pix2 = pixCropImage(pix1, 50, 50, -1, 70, 70, 1.1,
"/tmp/lept/misc/crop_bois15.pdf", NULL);
pixDestroy(&pix1);
pixDestroy(&pix2);

/* Page cleaning */
pixa1 = pixaCreate(3);
pix1 = pixRead("tel_3.tif");
pix2 = pixRotate(pix1, 0.02, L_ROTATE_SAMPLING, L_BRING_IN_WHITE, 0, 0);
pix3 = pixCleanImage(pix2, 1, 0, 1, 0);
pixaAddPix(pixa1, pix3, L_INSERT);
pixDisplay(pix3, 800, 800);
pixDestroy(&pix1);
pixDestroy(&pix2);
pix1 = pixRead("w91frag.jpg");
pixaAddPix(pixa1, pixScale(pix1, 2.5, 2.5), L_INSERT);
pix2 = pixRotate(pix1, 0.02, L_ROTATE_AREA_MAP, L_BRING_IN_WHITE, 0, 0);
pix3 = pixCleanImage(pix2, 1, 0, 1, 0);
pixaAddPix(pixa1, pixScale(pix3, 2.5, 2.5), L_INSERT);
pixDisplay(pix3, 1200, 800);
pixDestroy(&pix1);
pixDestroy(&pix2);
pixDestroy(&pix3);
lept_stderr("Writing /tmp/lept/misc/pageclean.pdf\n");
pixaConvertToPdf(pixa1, 0, 1.0, L_DEFAULT_ENCODE, 50, NULL,
"/tmp/lept/misc/pageclean.pdf");
pixaDestroy(&pixa1);

return 0;
}
2 changes: 1 addition & 1 deletion src/allheaders.h
Original file line number Diff line number Diff line change
Expand Up @@ -1377,7 +1377,7 @@ LEPT_DLL extern PIX * pixGenerateHalftoneMask ( PIX *pixs, PIX **ppixtext, l_int
LEPT_DLL extern PIX * pixGenTextlineMask ( PIX *pixs, PIX **ppixvws, l_int32 *ptlfound, PIXA *pixadb );
LEPT_DLL extern PIX * pixGenTextblockMask ( PIX *pixs, PIX *pixvws, PIXA *pixadb );
LEPT_DLL extern PIX * pixCropImage ( PIX *pixs, l_int32 lr_clear, l_int32 tb_clear, l_int32 edgeclean, l_int32 lr_border, l_int32 tb_border, l_float32 maxwiden, const char *debugfile, BOX **pcropbox );
LEPT_DLL extern PIX * pixRescaleForCropping ( PIX *pixs, l_int32 w, l_int32 h, l_int32 lr_border, l_int32 tb_border, l_float32 maxwiden, PIX **ppixsc );
LEPT_DLL extern l_int32 pixMaxCompAfterVClosing ( PIX *pixs, BOX **pbox );
LEPT_DLL extern PIX * pixCleanImage ( PIX *pixs, l_int32 contrast, l_int32 rotation, l_int32 scale, l_int32 opensize );
LEPT_DLL extern BOX * pixFindPageForeground ( PIX *pixs, l_int32 threshold, l_int32 mindist, l_int32 erasedist, l_int32 showmorph, PIXAC *pixac );
LEPT_DLL extern l_ok pixSplitIntoCharacters ( PIX *pixs, l_int32 minw, l_int32 minh, BOXA **pboxa, PIXA **ppixa, PIX **ppixdebug );
Expand Down
3 changes: 2 additions & 1 deletion src/morphseq.c
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,8 @@ SARRAY *sa;
}

if (pdfout) {
snprintf(fname, sizeof(fname), "/tmp/lept/seq_output_%d.pdf", L_ABS(dispsep));
snprintf(fname, sizeof(fname), "/tmp/lept/seq_output_%d.pdf",
L_ABS(dispsep));
pixaConvertToPdf(pixa, 0, 1.0, L_FLATE_ENCODE, 0, fname, fname);
pixaDestroy(&pixa);
}
Expand Down
Loading

0 comments on commit 1e7c3a9

Please sign in to comment.