| 1 |
|
| 2 |
|
| 3 |
<!DOCTYPE html> |
| 4 |
<html lang="en" data-bs-theme="dark"> |
| 5 |
|
| 6 |
|
| 7 |
<head> |
| •8 |
<meta charset="utf-8" /> |
| 9 |
<meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover" /> |
| 10 |
<title>ImageMagick | Color Thresholding</title> |
| •11 |
<meta name="description" content="ImageMagick supports color thresholding, a technique to segment images by color." /> |
| 12 |
<link rel="canonical" href="index.html" /> |
| 13 |
<meta name="robots" content="index, follow" /> |
| 14 |
<meta name="theme-color" content="#000000" /> |
| 15 |
<meta name="google-site-verification" content="_bMOCDpkx9ZAzBwb2kF3PRHbfUUdFj2uO8Jd1AXArz4" /> |
| •16 |
<link rel="shortcut icon" href="../image/wand.png" type="image/png" /> |
| 17 |
<link rel="apple-touch-icon" href="../image/wand.png" type="image/png" /> |
| 18 |
<link rel="preconnect" href="https://localhost/" /> |
| 19 |
<link rel="preload" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" as="style" crossorigin="anonymous" /> |
| 20 |
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" crossorigin="anonymous" /> |
| 21 |
|
| 22 |
<style> |
| 23 |
html { |
| 24 |
scroll-padding-top: 70px; |
| 25 |
} |
| 26 |
|
| 27 |
a.nav-link:hover, |
| 28 |
a.dropdown-item:hover { |
| 29 |
color: #ffc107 !important; |
| 30 |
text-shadow: 0px 2px 4px rgba(255, 193, 7, 0.8); |
| 31 |
} |
| 32 |
|
| 33 |
.btn-outline-warning { |
| 34 |
color: #cd7f32; |
| 35 |
border-color: #cd7f32; |
| 36 |
} |
| 37 |
|
| 38 |
.navbar-brand { |
| 39 |
color: #d4af37; |
| 40 |
transition: color 0.3s ease, text-shadow 0.3s ease; |
| 41 |
} |
| 42 |
|
| 43 |
.navbar-brand:hover { |
| 44 |
color: #e9c03d; |
| 45 |
text-shadow: 0px 2px 4px rgba(203, 163, 120, 0.6); |
| 46 |
} |
| 47 |
|
| 48 |
:root { |
| 49 |
--bs-code-color: revert; |
| 50 |
} |
| 51 |
</style> |
| 52 |
|
| •53 |
<meta property="og:title" content="ImageMagick | Color Thresholding" /> |
| 54 |
<meta property="og:description" content="ImageMagick supports color thresholding, a technique to segment images by color." /> |
| •55 |
<meta property="og:image" content="https://imagemagick.org/image/logo.png" /> |
| 56 |
<meta property="og:logo" content="https://imagemagick.org/image/logo.png" /> |
| 57 |
<meta property="og:url" content="https://imagemagick.org/color-thresholding/" /> |
| •58 |
<meta property="og:type" content="website" /> |
| 59 |
<meta property="og:site_name" content="ImageMagick" /> |
| 60 |
<meta property="og:locale" content="en_us" /> |
| 61 |
|
| •62 |
<meta name="twitter:card" content="summary_large_image" /> |
| 63 |
$site=strtolower("ImageMagick"); |
| •64 |
<meta name="twitter:site" content="@imagemagick" /> |
| 65 |
<meta name="twitter:creator" content="@imagemagick" /> |
| 66 |
<meta name="twitter:title" content="ImageMagick | Color Thresholding" /> |
| •67 |
<meta name="twitter:description" content="$this- />description" /> |
| 68 |
<meta name="twitter:image" content="https://imagemagick.org/image/logo.png" /> |
| •69 |
<meta name="twitter:image:alt" content="ImageMagick logo and tag line" /> |
| 70 |
|
| 71 |
<script type="application/ld+json"> |
| 72 |
{ |
| 73 |
"@context": "https://schema.org", |
| 74 |
"@type": "SoftwareApplication", |
| 75 |
"name": "ImageMagick", |
| 76 |
"url": "ImageMagick | Color Thresholding", |
| 77 |
"image": "https://imagemagick.org/image/logo.png", |
| 78 |
"description": "$this->description", |
| 79 |
"applicationCategory": "Multimedia", |
| 80 |
"operatingSystem": "Windows, macOS, Linux, Unix", |
| 81 |
"softwareVersion": "7.1.2-27", |
| 82 |
"license": "https://imagemagick.org/license/", |
| 83 |
"creator": { |
| 84 |
"@type": "Organization", |
| 85 |
"name": "ImageMagick Studio LLC", |
| 86 |
"url": "https://imagemagick.org" |
| 87 |
}, |
| 88 |
"keywords": [ |
| 89 |
"".ucfirst($title)."", |
| 90 |
"image processing software", |
| 91 |
"image conversion tool", |
| 92 |
"batch image editing", |
| 93 |
"open-source image editor", |
| 94 |
"ImageMagick command-line", |
| 95 |
"resize images ImageMagick", |
| 96 |
"crop and rotate images", |
| 97 |
"ImageMagick tutorial", |
| 98 |
"ImageMagick automation", |
| 99 |
"ImageMagick for developers", |
| 100 |
"ImageMagick CLI", |
| 101 |
"ImageMagick filters and effects", |
| 102 |
"ImageMagick scripting", |
| 103 |
"ImageMagick API integration" |
| 104 |
], |
| 105 |
"sameAs": [ |
| 106 |
"https://github.com/ImageMagick", |
| 107 |
"https://x.com/imagemagick" |
| 108 |
], |
| 109 |
"offers": { |
| 110 |
"@type": "Offer", |
| 111 |
"price": "0.00", |
| 112 |
"priceCurrency": "USD" |
| 113 |
} |
| 114 |
} |
| 115 |
</script> |
| 116 |
</head> |
| 117 |
|
| 118 |
<body> |
| 119 |
<script> |
| 120 |
function setTheme(theme) { |
| 121 |
localStorage.setItem('theme', theme); |
| 122 |
updateThemeIcon(theme); |
| 123 |
document.documentElement.setAttribute('data-bs-theme', theme === 'auto' ? getSystemTheme() : theme); |
| 124 |
} |
| 125 |
function getSystemTheme() { |
| 126 |
return window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light'; |
| 127 |
} |
| 128 |
function updateThemeIcon(theme) { |
| 129 |
const iconMap = { |
| 130 |
light: '☀️', |
| 131 |
dark: '🌙', |
| 132 |
auto: '🌓' |
| 133 |
}; |
| 134 |
document.getElementById('currentThemeIcon').textContent = iconMap[theme] || '🌓'; |
| 135 |
} |
| 136 |
document.addEventListener('DOMContentLoaded', () => { |
| 137 |
const savedTheme = localStorage.getItem('theme') || 'auto'; |
| 138 |
setTheme(savedTheme); |
| 139 |
}); |
| 140 |
</script> |
| 141 |
<nav class="navbar navbar-expand-md navbar-dark bg-dark fixed-top"> |
| 142 |
<div class="container-fluid"> |
| 143 |
<a class="navbar-brand fw-bold" href="../index-2.html" title="ImageMagick">ImageMagick</a> |
| 144 |
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#magick-navbars" aria-controls="magick-navbars" aria-expanded="false" aria-label="Toggle navigation"> |
| 145 |
<span class="navbar-toggler-icon"></span> |
| 146 |
</button> |
| 147 |
|
| 148 |
<div class="collapse navbar-collapse" id="magick-navbars"> |
| 149 |
<ul class="navbar-nav me-auto"> |
| 150 |
<li class="nav-item"> |
| •151 |
<a class="nav-link" href="../download/index.html">Download</a> |
| 152 |
</li> |
| 153 |
<li class="nav-item"> |
| •154 |
<a class="nav-link" href="../command-line-tools/index.html">Tools</a> |
| 155 |
</li> |
| 156 |
<li class="nav-item"> |
| •157 |
<a class="nav-link" href="../command-line-processing/index.html">CLI</a> |
| 158 |
</li> |
| 159 |
<li class="nav-item"> |
| •160 |
<a class="nav-link" href="../develop/index.html">Develop</a> |
| 161 |
</li> |
| 162 |
<li class="nav-item"> |
| 163 |
<a class="nav-link" rel="noopener noreferrer" target="_blank" href="https://github.com/ImageMagick/ImageMagick/discussions">Community</a> |
| 164 |
</li> |
| 165 |
</ul> |
| 166 |
<ul class="navbar-nav ms-auto"> |
| 167 |
<li class="nav-item dropdown ms-3"> |
| 168 |
<a class="nav-link dropdown-toggle" href="#" id="themeDropdown" role="button" data-bs-toggle="dropdown" aria-expanded="false"> |
| 169 |
<span id="currentThemeIcon">🌓</span> |
| 170 |
</a> |
| 171 |
<ul class="dropdown-menu dropdown-menu-end" aria-labelledby="themeDropdown"> |
| 172 |
<li><a class="dropdown-item" href="#" onclick="setTheme('light')">☀️ Light</a></li> |
| 173 |
<li><a class="dropdown-item" href="#" onclick="setTheme('dark')">🌙 Dark</a></li> |
| 174 |
<li><a class="dropdown-item" href="#" onclick="setTheme('auto')">🌓 Auto</a></li> |
| 175 |
</ul> |
| 176 |
</li> |
| 177 |
</ul> |
| 178 |
</div> |
| 179 |
</div> |
| 180 |
</nav> |
| 181 |
|
| 182 |
<div class="col-lg-8 mx-auto text-body-secondary pt-5 pt-lg-5"> |
| 183 |
<header class="d-flex align-items-center pb-3 mb-5 border-bottom"> |
| 184 |
<div class="mt-3 ms-3"> |
| 185 |
<a href="../index-2.html" class="text-decoration-none"> |
| 186 |
<h1 class="fs-2 mb-1 display-5">Color Thresholding</h1> |
| 187 |
</a> |
| 188 |
<h2 class="fs-5 text-muted h4">ImageMagick</h2> |
| 189 |
</div> |
| 190 |
</header> |
| 191 |
|
| 192 |
<main role="main" class="container"> |
| 193 |
<div class="row g-5"> |
| 194 |
<div class="col-md-9"> |
| 195 |
<div> |
| 196 |
<p>Use color thresholding to specify a color range and return a black and white image. All colors between the start and stop colors (inclusively) become white and the rest of the image pixels become black. The two colors are separated with a hyphen between them. Thresholding, by default, take place in the sRGB colorspace. Use the <code>-colorspace</code> to perform the thresholding in an alternative colorspaces (currently limited to sRGB, Gray, HSV, HSL, HCL, HSB, and HSW). Start and stop colors can be specified in any colorspace (recognized by ImageMagick), but in general, we advise you specify them in the colorspace corresponding to the <code>-colorspace</code> option. 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 useful, if the start hue value is higher than the stop hue value.</p> |
| 197 |
|
| 198 |
<p>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 <code>sRGB(0,0,0)</code> to <code>sRGB(255,255,255)</code> 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 <code>hsv(0,0%,0%)</code> and <code>hsv(0,0%,100%)</code>. For a color image, you would likely not find too many gray pixels.</p> |
| 199 |
|
| 200 |
<p>To illustrate how color thresholding performs, use this image:</p> |
| 201 |
|
| 202 |
<pre class="p-3 mb-2 text-body-secondary bg-body-tertiary cli"><samp>$ magick <var>input-image</var> -colorspace <var>XXX</var> -color-threshold <var>"start - stop"</var> <var>output-image</var></samp></pre> |
| 203 |
|
| 204 |
<p>The following are some typical examples of use:</p> |
| 205 |
|
| 206 |
<ul><img class="img-fluid img-thumbnail" src="../image/color-thresholding.jpg" alt="[Color Thresholding]" width="265" height="333" name="color-thresholding" /></ul> |
| 207 |
|
| 208 |
<p>Start by choosing 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 form the start color and the higher values form the stop color. Begin with <code>sRGB(183,132,20)</code>. Offset its values by +-20 to identify the start color (-) and the stop color (+); namely, R=183+-20, G=132+-20, and B=40+-20:</p> |
| 209 |
|
| 210 |
<pre class="p-3 mb-2 text-body-secondary bg-body-tertiary cli"><samp>$ magick monet.jpg -color-threshold 'sRGB(163,112,0)-sRGB(203,152,40)' monet.gif</samp></pre> |
| 211 |
|
| 212 |
<p>this returns the outline of the yellow flower petals:</p> |
| 213 |
|
| 214 |
<ul><img class="img-fluid img-thumbnail" src="../image/color-thresholding-hsv.gif" alt="[Color Thresholding]" width="265" height="333" name="color-thresholding" /></ul> |
| 215 |
|
| 216 |
<p>Now, pick two RGB colors: <code>sRGB(159,150,0)</code> and <code>sRGB(205,100,45):</code></p> |
| 217 |
|
| •218 |
<ul><pre class="p-3 mb-2 text-body-secondary bg-body-tertiary cli"><samp>$magick monet.jpg -color-threshold 'sRGB(159,150,0)-sRGB(205,100,45)' monet.gif</samp></pre></ul> |
| 219 |
|
| 220 |
<ul><img class="img-fluid img-thumbnail" src="../image/color-thresholding-rgb.gif" alt="[Color Thresholding]" width="265" height="333" name="color-thresholding" /></ul> |
| 221 |
|
| 222 |
<p>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 <code>sRGB(183,132,20)</code> and convert to HSV:</p> |
| 223 |
|
| 224 |
<pre class="code"> |
| 225 |
magick xc:"srgb(183,132,20)" -colorspace HSV txt: |
| 226 |
# ImageMagick pixel enumeration: 1,1,65535,hsv |
| 227 |
0,0: (41.227,89.071%,71.7647%) #1D51E405B7B7 hsv(41.227,89.071%,71.7647%) |
| 228 |
</pre> |
| 229 |
|
| 230 |
<p>Now, offset its HSV values as H=41+-20, S=89+-10, and V=72+-10, to create the start color (-) and stop color (+):</p> |
| 231 |
|
| 232 |
<pre class="p-3 mb-2 text-body-secondary bg-body-tertiary cli"><samp>$ magick monet.jpg -colorspace HSV -color-threshold 'hsv(21,79%,62%)-hsv(61,99%,85%)' monet.gif</samp></pre> |
| 233 |
|
| 234 |
<ul><img class="img-fluid img-thumbnail" src="../image/color-thresholding-rgb.gif" alt="[Color Thresholding]" width="265" height="333" name="color-thresholding" /></ul> |
| 235 |
|
| 236 |
<p>Next, pick two RGB colors. Use <code>-colorspace HSV</code> to convert the image to HSV, and apply the RGB start and stop colors. Choose <code>sRGB(158,77,33)</code> and <code>sRGB(213,217,2)</code>:</p> |
| 237 |
|
| 238 |
<pre class="p-3 mb-2 text-body-secondary bg-body-tertiary cli"><samp>$ magick monet.jpg -colorspace HSV -color-threshold "sRGB(158,77,33)-sRGB(213,217,2)" monet.gif</samp></pre> |
| 239 |
|
| 240 |
<p>Here is the expected results:</p> |
| 241 |
|
| 242 |
<ul><img class="img-fluid img-thumbnail" src="../image/color-thresholding-hsv-rgb.gif" alt="[Color Thresholding]" width="265" height="333" name="color-thresholding" /></ul> |
| 243 |
|
| 244 |
<p>Next, choose two sRGB colors and convert them to gray. Now convert the image to gray and use the gray thresholding colors.</p> |
| 245 |
|
| 246 |
<pre class="code"> |
| 247 |
magick xc:"sRGB(159,150,0)" -colorspace gray txt: |
| 248 |
# ImageMagick pixel enumeration: 1,1,65535,gray |
| 249 |
0,0: (36259.1) #8DA38DA38DA3 gray(55.3278%) |
| 250 |
|
| 251 |
magick xc:"sRGB(205,100,45)" -colorspace gray txt: |
| 252 |
# ImageMagick pixel enumeration: 1,1,65535,gray |
| 253 |
0,0: (30418.2) #76D276D276D2 gray(46.4152%) |
| 254 |
</pre> |
| 255 |
|
| 256 |
<p>Notice that the start intensity must be smaller than stop intensity:</p> |
| 257 |
|
| 258 |
<pre class="p-3 mb-2 text-body-secondary bg-body-tertiary cli"><samp>$ magick monet.jpg -colorspace gray -color-threshold 'gray(46.4152%)-gray(55.3278%)' monet.gif</samp></pre> |
| 259 |
|
| 260 |
<p>Here is the results of the color thresholding operation:</p> |
| 261 |
|
| 262 |
<ul><img class="img-fluid img-thumbnail" src="../image/color-thresholding-gray.gif" alt="[Color Thresholding]" width="265" height="333" name="Color Thresholding" /></ul> |
| 263 |
</div> |
| 264 |
|
| 265 |
</div> |
| 266 |
<div class="col-md-3"> |
| 267 |
<div class="container text-center my-3"> |
| 268 |
<a href="../image/wizard.png" rel="noopener noreferrer" target="_blank"> |
| •269 |
<img alt="The Tao of the Spiritual Warrior" class="img-fluid img-thumbnail" height="1468" loading="lazy" src="../image/wizard.png" width="1104" /> |
| 270 |
</a> |
| 271 |
<p class="mt-2"><iframe src="https://github.com/sponsors/ImageMagick/button" title="Sponsor ImageMagick" height="35" width="107" style="border: 0;"></iframe></p> |
| 272 |
</div> |
| 273 |
</div> |
| 274 |
</div> |
| 275 |
<a aria-label="About ImageMagick" class="btn btn-outline-warning mt-3" href="../index-2.html">About ImageMagick</a> |
| 276 |
</main> |
| 277 |
<footer class="text-center pt-5 my-5 text-body-secondary border-top"> |
| 278 |
<div class="container-fluid"> |
| 279 |
<a href="../security-policy/index.html">Security</a> • |
| 280 |
<a href="../news/index.html">News</a> |
| •281 |
|
| 282 |
<a href="#"><img class="d-inline" id="wand" alt="And Now a Touch of Magick" width="16" height="16" src="../image/wand.ico"/></a> |
| 283 |
|
| 284 |
<a href="../links/index.html">Related</a> • |
| 285 |
<a href="../sitemap/index.html">Sitemap</a> |
| •286 |
<br /> |
| 287 |
<a href="../support/index.html">Sponsor</a> • |
| 288 |
<a href="../cite/index.html">Cite</a> • |
| 289 |
<a href="http://pgp.mit.edu/pks/lookup?op=get&search=0x89AB63D48277377A">Public Key</a> • |
| 290 |
<a href="https://github.com/ImageMagick/ImageMagick/discussions">Contact Us</a> |
| •291 |
<br /> |
| 292 |
<a href="https://github.com/imagemagick/imagemagick" rel="noopener noreferrer" target="_blank" aria-label="GitHub"><svg xmlns="http://www.w3.org/2000/svg" class="navbar-nav-svg" viewBox="0 0 512 499.36" width="2%" height="2%" role="img" focusable="false"> |
| 293 |
<title>GitHub</title> |
| 294 |
<path fill="currentColor" fill-rule="evenodd" d="M256 0C114.64 0 0 114.61 0 256c0 113.09 73.34 209 175.08 242.9 12.8 2.35 17.47-5.56 17.47-12.34 0-6.08-.22-22.18-.35-43.54-71.2 15.49-86.2-34.34-86.2-34.34-11.64-29.57-28.42-37.45-28.42-37.45-23.27-15.84 1.73-15.55 1.73-15.55 25.69 1.81 39.21 26.38 39.21 26.38 22.84 39.12 59.92 27.82 74.5 21.27 2.33-16.54 8.94-27.82 16.25-34.22-56.84-6.43-116.6-28.43-116.6-126.49 0-27.95 10-50.8 26.35-68.69-2.63-6.48-11.42-32.5 2.51-67.75 0 0 21.49-6.88 70.4 26.24a242.65 242.65 0 0 1 128.18 0c48.87-33.13 70.33-26.24 70.33-26.24 14 35.25 5.18 61.27 2.55 67.75 16.41 17.9 26.31 40.75 26.31 68.69 0 98.35-59.85 120-116.88 126.32 9.19 7.9 17.38 23.53 17.38 47.41 0 34.22-.31 61.83-.31 70.23 0 6.85 4.61 14.81 17.6 12.31C438.72 464.97 512 369.08 512 256.02 512 114.62 397.37 0 256 0z" /> |
| 295 |
</svg></a> • |
| 296 |
<a href="https://twitter.com/imagemagick" rel="noopener noreferrer" target="_blank" aria-label="Twitter"><svg xmlns="http://www.w3.org/2000/svg" class="navbar-nav-svg" viewBox="0 0 300 300" width="2%" height="2%" role="img" focusable="false"> |
| 297 |
<title>Twitter</title> |
| 298 |
<path d="M178.57 127.15 290.27 0h-26.46l-97.03 110.38L89.34 0H0l117.13 166.93L0 300.25h26.46l102.4-116.59 81.8 116.59h89.34M36.01 19.54H76.66l187.13 262.13h-40.66" /> |
| 299 |
</svg></a> |
| •300 |
<br /> |
| 301 |
<small>Copyright © 1999 ImageMagick Studio LLC</small> |
| 302 |
</div> |
| 303 |
</footer> |
| 304 |
<div style="visibility: hidden;"> |
| 305 |
<div class="gcse-search"></div> |
| 306 |
</div> |
| 307 |
</div> |
| 308 |
|
| 309 |
|
| 310 |
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-FKyoEForCGlyvwx9Hj09JcYn3nv7wiPVlz7YYwJrWVcXK/BmnVDxM+D2scQbITxI" crossorigin="anonymous"></script> |
| 311 |
</body> |
| 312 |
|
| 313 |
|
| 314 |
</html> |