使用色彩閾值可以指定一個色彩範圍,並返回一張黑白圖像。介於起始和結束顏色(含)之間的所有顏色都將變為白色,而圖像的其餘像素則變為黑色。兩種顏色之間用連字符分隔。預設情況下,閾值化在 sRGB 色彩空間中進行。使用 -colorspace 選項可以在其他色彩空間中執行閾值化(目前僅限於 sRGB、Gray、HSV、HSL、HCL、HSB 和 HSW)。起始和結束顏色可以使用 ImageMagick 識別的任何色彩空間指定,但一般來說,我們建議您在與 -colorspace 選項相對應的色彩空間中指定它們。對於 Gray 色彩空間,請確保起始值低於結束值。請注意,色相是循環的 -- 0 和 360 是相同的。但是,如果起始色相值高於結束色相值,則跨越色相 = 0 的顏色範圍仍然有用。
混合使用起始和結束顏色的色彩空間與圖像的色彩空間時,請務必小心。一個色彩空間中的大範圍可能會映射到另一個色彩空間中的窄範圍。例如,人們可能會天真地認為,即使在使用 -colorspace HSV 時,sRGB(0,0,0) 到 sRGB(255,255,255) 也會產生白色圖像,捕獲所有顏色。但這兩種顏色是黑色和白色,因此只能映射到 hsv(0,0%,0%) 和 hsv(0,0%,100%) 的灰度範圍。對於彩色圖像,您可能不會找到太多灰色像素。
為了說明色彩閾值化的執行方式,請使用此圖像
convert input-image -colorspace XXX -color-threshold "start - stop" output-image
以下是一些典型的使用範例
![[Color Thresholding]](/script/../image/color-thresholding.jpg)
首先選擇一種 sRGB 顏色(圖像上的某個位置,在本例中為花朵的黃色),並將其向上和向下偏移以形成兩種 sRGB 顏色。較低的值形成起始顏色,較高的值形成結束顏色。從 sRGB(183,132,20) 開始。將其值偏移 +-20 以確定起始顏色 (-) 和結束顏色 (+),即 R=183+-20、G=132+-20 和 B=40+-20
magick monet.jpg -color-threshold 'sRGB(163,112,0)-sRGB(203,152,40)' monet.gif
這將返回黃色花瓣的輪廓
![[Color Thresholding]](/script/../image/color-thresholding-hsv.gif)
現在,選擇兩種 RGB 顏色:sRGB(159,150,0) 和 sRGB(205,100,45):
magick monet.jpg -color-threshold 'sRGB(159,150,0)-sRGB(205,100,45)' monet.gif
![[Color Thresholding]](/script/../image/color-thresholding-rgb.gif)
Next, select one sRGB color and convert it to HSV and offset its values low and high to generate the start and stop HSV colors. Use -colorspace HSV to convert the image to HSV. For our HSV example, we pick sRGB(183,132,20) and 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%)
Now, offset its HSV values as H=41+-20, S=89+-10, and V=72+-10, to create the start color (-) and stop color (+):
magick monet.jpg -colorspace HSV -color-threshold 'hsv(21,79%,62%)-hsv(61,99%,85%)' monet.gif
![[Color Thresholding]](/script/../image/color-thresholding-rgb.gif)
Next, pick two RGB colors. Use -colorspace HSV to convert the image to HSV, and apply the RGB start and stop colors. Choose sRGB(158,77,33) and sRGB(213,217,2):
magick monet.jpg -colorspace HSV -color-threshold "sRGB(158,77,33)-sRGB(213,217,2)" monet.gif
Here is the expected results:
![[Color Thresholding]](/script/../image/color-thresholding-hsv-rgb.gif)
Next, choose two sRGB colors and convert them to gray. Now convert the image to gray and use the gray thresholding 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%)
Notice that the start intensity must be smaller than stop intensity:
magick monet.jpg -colorspace gray -color-threshold 'gray(46.4152%)-gray(55.3278%)' monet.gif
Here is the results of the color thresholding operation:
![[Color Thresholding]](/script/../image/color-thresholding-gray.gif)