DanBloomberg / leptonica

Leptonica is an open source library containing software that is broadly useful for image processing and image analysis applications. The official github repository for Leptonica is: danbloomberg/leptonica. See leptonica.org for more documentation.
Other
1.74k stars 387 forks source link

Which interface corresponds to the color scale function in Photoshop? #689

Open jueqing1015 opened 1 year ago

jueqing1015 commented 1 year ago

image

DanBloomberg commented 1 year ago

please give all details in English, including a reference that defines the color scale function that you refer to.

jueqing1015 commented 1 year ago

请用英文提供所有详细信息,包括定义您所指色标函数的参考。 @DanBloomberg I'm very sorry, my English is not good, I used the translation software for translation image

DanBloomberg commented 1 year ago

To respond to your query, I need to know the DEFINITION of the "color scale function in Photoshop". I'm asking for a formula or an algorithm.

jueqing1015 commented 1 year ago

为了回答您的问题,我需要知道“Photoshop 中的色阶功能”的定义。 我要的是公式或算法。

Its function is to adjust brightness, contrast, and gamma simultaneously. Note: instead of adjusting these three items separately, you can adjust them simultaneously

DanBloomberg commented 1 year ago

If you want to do this in leptonica, you need to find out exactly what they are doing in photoshop and then check out the appropriate color functions that are in leptonica. For the latter, start with coloring.c, which has this information:

*      Coloring "gray" pixels
 *           PIX             *pixColorGrayRegions()
 *           l_int32          pixColorGray()
 *           PIX             *pixColorGrayMasked()
 *
 *      Adjusting one or more colors to a target color
 *           PIX             *pixSnapColor()
 *           PIX             *pixSnapColorCmap()
 *
 *      Piecewise linear color mapping based on a source/target pair
 *           PIX             *pixLinearMapToTargetColor()
 *           l_int32          pixelLinearMapToTargetColor()
 *
 *      Fractional shift of RGB towards black or white
 *           PIX             *pixShiftByComponent()
 *           l_int32          pixelShiftByComponent()
 *           l_int32          pixelFractionalShift()
 *           PIX             *pixShiftWithInvariantHue()
 *
 *  There are quite a few "coloring" functions in leptonica.
 *  You can find them in these files:
 *       coloring.c
 *       paintcmap.c
 *       pix2.c
 *       blend.c
 *       enhance.c
 *
 *  They fall into the following categories:
 *
 *  (1) Moving either the light or dark pixels toward a
 *      specified color. (pixColorGray, pixColorGrayMasked)
 *  (2) Forcing all pixels whose color is within some delta of a
 *      specified color to move to that color. (pixSnapColor)
 *  (3) Doing a piecewise linear color shift specified by a source
 *      and a target color.  Each component shifts independently.
 *      (pixLinearMapToTargetColor, pixMapWithInvariantHue).
*  (4) Shifting all colors by a given fraction of their distance
 *      from 0 (if shifting down) or from 255 (if shifting up).
 *      This is useful for colorizing either the background or
 *      the foreground of a grayscale image. (pixShiftByComponent)
 *  (5) Shifting all colors by a component-dependent fraction of
 *      their distance from 0 (if shifting down) or from 255 (if
 *      shifting up).  This is useful for modifying the color to
 *      compensate for color shifts in acquisition or printing.
 *      (enhance.c: pixColorShiftRGB, pixMosaicColorShiftRGB).
 *  (6) Repainting selected pixels. (paintcmap.c: pixSetSelectMaskedCmap)
 *  (7) Blending a fraction of a specific color with the existing RGB
 *      color.  (pix2.c: pixBlendInRect())
 *  (8) Changing selected colors in a colormap.
 *      (paintcmap.c: pixSetSelectCmap, pixSetSelectMaskedCmap)
 *  (9) Shifting all the pixels towards black or white depending on
 *      the gray value of a second image.  (blend.c: pixFadeWithGray)
 *  (10) Changing the hue, saturation or brightness, by changing one of
 *      these parameters in HSV color space by a fraction of the distance
 *      toward its end-point, but leaving the other two parameters
 *      invariant.  For example, you can change the brightness by moving
 *      each pixel's v-parameter a specified fraction of the distance
 *      toward 0 (darkening) or toward 255 (brightening), without altering
 *      the hue or saturation.  (enhance.c: pixModifySaturation,
 *      pixModifyHue, pixModifyBrightness)
jueqing1015 commented 1 year ago

如果您想在 leptonica 中执行此操作,则需要准确了解它们在 photoshop 中的作用,然后检查 leptonica 中的适当颜色功能。对于后者,从 开始coloring.c,其中包含以下信息:

*      Coloring "gray" pixels
 *           PIX             *pixColorGrayRegions()
 *           l_int32          pixColorGray()
 *           PIX             *pixColorGrayMasked()
 *
 *      Adjusting one or more colors to a target color
 *           PIX             *pixSnapColor()
 *           PIX             *pixSnapColorCmap()
 *
 *      Piecewise linear color mapping based on a source/target pair
 *           PIX             *pixLinearMapToTargetColor()
 *           l_int32          pixelLinearMapToTargetColor()
 *
 *      Fractional shift of RGB towards black or white
 *           PIX             *pixShiftByComponent()
 *           l_int32          pixelShiftByComponent()
 *           l_int32          pixelFractionalShift()
 *           PIX             *pixShiftWithInvariantHue()
 *
 *  There are quite a few "coloring" functions in leptonica.
 *  You can find them in these files:
 *       coloring.c
 *       paintcmap.c
 *       pix2.c
 *       blend.c
 *       enhance.c
 *
 *  They fall into the following categories:
 *
 *  (1) Moving either the light or dark pixels toward a
 *      specified color. (pixColorGray, pixColorGrayMasked)
 *  (2) Forcing all pixels whose color is within some delta of a
 *      specified color to move to that color. (pixSnapColor)
 *  (3) Doing a piecewise linear color shift specified by a source
 *      and a target color.  Each component shifts independently.
 *      (pixLinearMapToTargetColor, pixMapWithInvariantHue).
*  (4) Shifting all colors by a given fraction of their distance
 *      from 0 (if shifting down) or from 255 (if shifting up).
 *      This is useful for colorizing either the background or
 *      the foreground of a grayscale image. (pixShiftByComponent)
 *  (5) Shifting all colors by a component-dependent fraction of
 *      their distance from 0 (if shifting down) or from 255 (if
 *      shifting up).  This is useful for modifying the color to
 *      compensate for color shifts in acquisition or printing.
 *      (enhance.c: pixColorShiftRGB, pixMosaicColorShiftRGB).
 *  (6) Repainting selected pixels. (paintcmap.c: pixSetSelectMaskedCmap)
 *  (7) Blending a fraction of a specific color with the existing RGB
 *      color.  (pix2.c: pixBlendInRect())
 *  (8) Changing selected colors in a colormap.
 *      (paintcmap.c: pixSetSelectCmap, pixSetSelectMaskedCmap)
 *  (9) Shifting all the pixels towards black or white depending on
 *      the gray value of a second image.  (blend.c: pixFadeWithGray)
 *  (10) Changing the hue, saturation or brightness, by changing one of
 *      these parameters in HSV color space by a fraction of the distance
 *      toward its end-point, but leaving the other two parameters
 *      invariant.  For example, you can change the brightness by moving
 *      each pixel's v-parameter a specified fraction of the distance
 *      toward 0 (darkening) or toward 255 (brightening), without altering
 *      the hue or saturation.  (enhance.c: pixModifySaturation,
 *      pixModifyHue, pixModifyBrightness)

I have found an OPENCV related algorithm. I wonder if it can help me find the relevant interface? https://blog.csdn.net/weixin_48306625/article/details/120138538 The following image is the result I want to process, just fade the background to near white. The text becomes darker WY2@6S}XIO2OHV6SP0TN5BQ

Charltsing commented 10 months ago

你应该使用专门的漂白算法,可以看一下PictureCleaner或者图片魔法师

DanBloomberg commented 10 months ago

In leptonica, you want to use functions in adaptmap.c, such as pixCleanBackgroundToWhite()

For example, this program will clean the source image on the left:

    pixa1 = pixaCreate(2);
    pix1 = pixRead("test-i689-src.jpg");
    pix2 = pixCleanBackgroundToWhite(pix1, NULL, NULL, 0.5, 60, 195);
    pixWrite("test-i689-result.jpg", pix2, IFF_JFIF_JPEG);
    pixaAddPix(pixa1, pix1, L_INSERT);
    pixaAddPix(pixa1, pix2, L_INSERT);
    pix3 = pixaDisplayTiledInColumns(pixa1, 2, 1.0, 40, 3);
    pixWrite("test-i689-compare.jpg", pix3, IFF_JFIF_JPEG);
    pixDisplay(pix3, 0, 0);

I"ve attached the src and result images, plus a comparison image that has both src and result embedded in it.

test-i689-src test-i689-result test-i689-compare