New color thresholding method

Announcements pertaining to ImageMagick, or ImageMagick related software. This list is moderated. No discussions here, instead post to the users group instead.
Locked
User avatar
fmw42
Posts: 26383
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

New color thresholding method

Post by fmw42 »

IM 7.0.9.27 introduces -color-threshold.

It allows one to specify a color range as start-color and stop-color for controlling the image thresholding. All colors between the two colors (inclusively) will become white and the rest of the image will become black. The two colors are specified with a hyphen between them. The -color-threshold may be used in conjunction with -colorspace in order to do the thresholding in other colorspaces (currently limited to sRGB, Gray, HSV, HSL, HCL, HSB, HSW). Start and stop colors may be specified in any colorspace (permitted by ImageMagick), but in general, it is advised to specify them in the colorspace corresponding to that specified by -colorspace. For -colorspace Gray, be sure to use a start value lower than the stop value. Note that Hue is cyclical -- 0 and 360 are the same. However, a range of colors that spans hue=0 may still be use, if the start hue value is higher than the stop hue value.

Again, caution is advised when mixing the colorspace of the start and stop colors and that of the image. A large range in one colorspace may map to only a narrow range in another colorspace. For example, one might naively think that sRGB(0,0,0) to sRGB(255,255,255) would produce a white image, catching all colors, even when using -colorspace HSV. But these two colors are black and white and so map only to a range of grayscale as hsv(0,0%,0%) and hsv(0,0%,100%). So for a color image, they would likely not find too many gray pixels.


Syntax:

Code: Select all

convert input_image <-colorspace XXX> -color-threshold "start-color - stop-color" output_image

The following are some typical examples of use

Input:
Image



1) Pick one sRGB color (somewhere on the image -- in this case the yellow of the flowers) and offset it low and high to form two sRGB colors. The lower values should be the start-color and the higher values should be the stop color.

Pick sRGB(183,132,20)

Offset by +-20. So R=183+-20, G=132+-20, B=40+-20

Code: Select all

magick monet2.jpg -color-threshold 'sRGB(163,112,0)-sRGB(203,152,40)' monet2_thresh_rgb.gif
Image



2) Pick Two RGB colors:

Pick sRGB(159,150,0) and sRGB(205,100,45)

Code: Select all

magick monet2.jpg -color-threshold 'sRGB(159,150,0)-sRGB(205,100,45)' monet2_thresh_rgb2.gif
Image



3) Pick one sRGB color and convert it to HSV and offset it low and high to generate two HSV colors. Then use -colorspace HSV to convert the image to HSV and use the HSV start and stop colors.

Pick sRGB(183,132,20)

Convert to HSV:
magick xc:"srgb(183,132,20)" -colorspace HSV txt:
# ImageMagick pixel enumeration: 1,1,65535,hsv
0,0: (41.227,89.071%,71.7647%) #1D51E405B7B7 hsv(41.227,89.071%,71.7647%)

Use H=41+-20, S=89+-10, V=72+-10

Code: Select all

magick monet2.jpg -colorspace HSV -color-threshold "hsv(21,79%,62%)-hsv(61,99%,85%)" monet2_thresh_hsv.gif
Image



4) Pick two RGB colors. Then use -colorspace HSV to convert the image to HSV, but apply the RGB start and stop colors.

Pick sRGB(158,77,33) and sRGB(213,217,2)

Code: Select all

magick monet2.jpg -colorspace HSV -color-threshold "sRGB(158,77,33)-sRGB(213,217,2)" monet2_thresh_hsv_rgb.gif
Image




5) Pick Two sRGB colors and convert them to Gray. Then convert the image to Gray and use the Gray colors.

magick xc:"sRGB(159,150,0)" -colorspace gray txt:
# ImageMagick pixel enumeration: 1,1,65535,gray
0,0: (36259.1) #8DA38DA38DA3 gray(55.3278%)

magick xc:"sRGB(205,100,45)" -colorspace gray txt:
# ImageMagick pixel enumeration: 1,1,65535,gray
0,0: (30418.2) #76D276D276D2 gray(46.4152%)

Note that start value must be smaller than stop value.

Code: Select all

magick monet2.jpg -colorspace gray -color-threshold 'gray(46.4152%)-gray(55.3278%)' monet2_thresh_gray.gif
Image

Locked