How do I remove solid colours if I don't know the colours in advance?
How do I remove solid colours if I don't know the colours in advance?
I have images which contain both gradients and perfectly solid colours. I want to convert the solid colours, by turning them to solid white. I found examples online which would work if the solid colours were known in advance, but I don't know that information in advance. I'm flexible on the definition of solid colour but I would like to define it as "a pixel with at least three of its neighboring eight pixels of EXACTLY the same value." I've looked at the -morphology conversion but don't understand it very well. Could morphology work for me?
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: How do I remove solid colours if I don't know the colours in advance?
Personally I would not consider a region constant unless all its eight neighbors were the same.
-
- Posts: 12159
- Joined: 2010-01-23T23:01:33-07:00
- Authentication code: 1151
- Location: England, UK
Re: How do I remove solid colours if I don't know the colours in advance?
One definition would be "a pixel is a solid colour if and only if it is equal to the neighbours to the north, south, east and west."dgpeters wrote:I'm flexible on the definition of solid colour ...
Windows BAT script. Intermediate values can be negative, so HDRI must be used. It could be modified so that didn't matter.
Code: Select all
%IM32f%convert ^
%INFILE% ^
-alpha off ^
-virtual-pixel Edge ^
( -clone 0 -morphology Convolve 2x1+0+0:1,-1 ) ^
( -clone 0 -morphology Convolve 1x2+0+0:1,-1 ) ^
( -clone 0 -morphology Convolve 2x1+1+0:1,-1 ) ^
( -clone 0 -morphology Convolve 1x2+0+1:1,-1 ) ^
-delete 0 ^
-evaluate Abs 0 ^
-grayscale Brightness ^
-evaluate-sequence Max ^
-threshold 0 ^
%OUTFILE%
This is easily extended to regard all eight neighbours. The options "-evaluate-sequence Max -threshold 0" makes the output zero if and only if all the four neighbour differences are zero. If you prefer a voting scheme, you could first threshold, then negate, add, and threshold again.
snibgo's IM pages: im.snibgo.com
Re: How do I remove solid colours if I don't know the colours in advance?
I appreciate the effort! If I can make this work I will use it a lot; I'll let you know.
Re: How do I remove solid colours if I don't know the colours in advance?
The first thing I had to figure out was that I needed to put the escape character "\" before each parenthesis because I'm on macOS. The second thing I had to figure out was that it doesn't work on PNG files ("convert: profile 'icc': 'RGB ': RGB color space not permitted on grayscale PNG `test2.png' @ warning/png.c/MagickPNGWarningHandler/1744.") so I changed my screenshot file type to TIFF. Now the command actually works (with several error messages, listed below) but it doesn't quite do what I want. What it does is hard to explain, but it seems to convert solid colours to a very light grey, and everything else to white. So it's getting close to working. The errors I get when I execute the command are:
I'm wondering if the approach should be modified so that any pixel which differs from its neighbors is changed to black, otherwise it is set to white.
Even if you can't help me further, I may be able to figure this out after a few weeks; you have given me some incredible assistance.
Code: Select all
"convert: unable to open image 'alpha': No such file or directory @ error/blob.c/OpenBlob/3323.
convert: no decode delegate for this image format `' @ error/constitute.c/ReadImage/509.
convert: unable to open image 'off': No such file or directory @ error/blob.c/OpenBlob/3323.
convert: no decode delegate for this image format `' @ error/constitute.c/ReadImage/509."
Even if you can't help me further, I may be able to figure this out after a few weeks; you have given me some incredible assistance.
-
- Posts: 12159
- Joined: 2010-01-23T23:01:33-07:00
- Authentication code: 1151
- Location: England, UK
Re: How do I remove solid colours if I don't know the colours in advance?
That warning is just a warning, not an error, and should not prevent the command from working.dgpeters wrote:convert: profile 'icc': 'RGB ': RGB color space not permitted on grayscale PNG `test2.png' @ warning/png.c/MagickPNGWarningHandler/1744
The other errors you get are real errors. Please show your command. I suspect a mis-translation from Windows BAT to bash.
EDIT: typo, missed "not". Oops.
snibgo's IM pages: im.snibgo.com
Re: How do I remove solid colours if I don't know the colours in advance?
Code: Select all
#!/bin/sh
convert test.tiff alpha off -virtual-pixel Edge \( -clone 0 -morphology Convolve 2x1+0+0:1,-1 \) \( -clone 0 -morphology Convolve 1x2+0+0:1,-1 \) \( -clone 0 -morphology Convolve 2x1+1+0:1,-1 \) \( -clone 0 -morphology Convolve 1x2+0+1:1,-1 \) -delete 0 -evaluate Abs 0 -grayscale Brightness -evaluate-sequence Max -threshold 0 test2.tiff
Code: Select all
dyld: Library not loaded: /ImageMagick-7.0.7/lib/libMagickCore-7.Q16HDRI.5.dylib
Referenced from: /Applications/ImageMagick-7.0.7/bin/convert
Reason: image not found
./x.sh: line 2: 62305 Abort trap: 6 convert test.tiff -alpha off -virtual-pixel Edge \( -clone 0 -morphology Convolve 2x1+0+0:1,-1 \) \( -clone 0 -morphology Convolve 1x2+0+0:1,-1 \) \( -clone 0 -morphology Convolve 2x1+1+0:1,-1 \) \( -clone 0 -morphology Convolve 1x2+0+1:1,-1 \) -delete 0 -evaluate Abs 0 -grayscale Brightness -evaluate-sequence Max -threshold 0 test2.tiff
Code: Select all
convert test.tiff -alpha off -virtual-pixel Edge \( -clone 0 -morphology Convolve 2x1+0+0:1,-1 \) \( -clone 0 -morphology Convolve 1x2+0+0:1,-1 \) \( -clone 0 -morphology Convolve 2x1+1+0:1,-1 \) \( -clone 0 -morphology Convolve 1x2+0+1:1,-1 \) -delete 0 -evaluate Abs 0 -grayscale Brightness -evaluate-sequence Max -threshold 0 test2.tiff
Code: Select all
export MAGICK_HOME="/Applications/ImageMagick-7.0.7"
export PATH="$MAGICK_HOME/bin:$PATH"
export DYLD_LIBRARY_PATH="$MAGICK_HOME/lib/"
Code: Select all
zzzzz-iMac:Desktop zzzzzz$ ls x* t*
test.png test.tiff x.sh
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: How do I remove solid colours if I don't know the colours in advance?
try
#!/bin/bash
magick test.tiff -alpha off -virtual-pixel Edge \
\( -clone 0 -morphology Convolve 2x1+0+0:1,-1 \) \
\( -clone 0 -morphology Convolve 1x2+0+0:1,-1 \) \
\( -clone 0 -morphology Convolve 2x1+1+0:1,-1 \) \
\( -clone 0 -morphology Convolve 1x2+0+1:1,-1 \) \
-delete 0 \
-evaluate Abs 0 \
-grayscale Brightness \
-evaluate-sequence Max \
-threshold 0 \
test2.tiff
#!/bin/bash
magick test.tiff -alpha off -virtual-pixel Edge \
\( -clone 0 -morphology Convolve 2x1+0+0:1,-1 \) \
\( -clone 0 -morphology Convolve 1x2+0+0:1,-1 \) \
\( -clone 0 -morphology Convolve 2x1+1+0:1,-1 \) \
\( -clone 0 -morphology Convolve 1x2+0+1:1,-1 \) \
-delete 0 \
-evaluate Abs 0 \
-grayscale Brightness \
-evaluate-sequence Max \
-threshold 0 \
test2.tiff
Re: How do I remove solid colours if I don't know the colours in advance?
It's sorta working now. It's giving me exactly what I specified, I believe, but perhaps not exactly what I wanted. For example now if my source image contains a solid black square on a solid white background, the result is an outline of a white square on a black background. I think I understand why. My goal was to isolate solid colours and gradients from each other. And the -morphology parameter is getting me close. But I'm getting a lot of false positive "solids" in my gradients, and my definition of solid may be a bit to tight. I'l work on it. In any case, I think you've answered my question. You get 100%. Now I have to tinker with the morphology options to see if I can make it meet my requirements better. I wouldn't have been able to get this far without your help, thanks.
P.S. I had never seen the "magick" command until now. Is that a synonym for "convert"?
P.S. I had never seen the "magick" command until now. Is that a synonym for "convert"?
-
- Posts: 12159
- Joined: 2010-01-23T23:01:33-07:00
- Authentication code: 1151
- Location: England, UK
Re: How do I remove solid colours if I don't know the colours in advance?
The definition I gave for "solid" means that colours at the edge of a patch aren't solid.
"magick" is the new-fangled name for what used to be "convert". Well, it came with IM v7, a couple of years ago now.
It would be neat if all the documentation was tested against v7 magick, and updated. I plan to do this for my pages, sometime.
"magick" is the new-fangled name for what used to be "convert". Well, it came with IM v7, a couple of years ago now.
It would be neat if all the documentation was tested against v7 magick, and updated. I plan to do this for my pages, sometime.
snibgo's IM pages: im.snibgo.com