How do I remove solid colours if I don't know the colours in advance?

Discuss digital image processing techniques and algorithms. We encourage its application to ImageMagick but you can discuss any software solutions here.
Post Reply
dgpeters
Posts: 18
Joined: 2013-09-10T15:47:00-07:00
Authentication code: 6789

How do I remove solid colours if I don't know the colours in advance?

Post by dgpeters »

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?
User avatar
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?

Post by fmw42 »

Personally I would not consider a region constant unless all its eight neighbors were the same.
snibgo
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?

Post by snibgo »

dgpeters wrote:I'm flexible on the definition of solid colour ...
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."

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%
The output is black where is pixel is equal to the four neighbours; otherwise white.

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
dgpeters
Posts: 18
Joined: 2013-09-10T15:47:00-07:00
Authentication code: 6789

Re: How do I remove solid colours if I don't know the colours in advance?

Post by dgpeters »

I appreciate the effort! If I can make this work I will use it a lot; I'll let you know.
dgpeters
Posts: 18
Joined: 2013-09-10T15:47:00-07:00
Authentication code: 6789

Re: How do I remove solid colours if I don't know the colours in advance?

Post by dgpeters »

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:

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."
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.
snibgo
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?

Post by snibgo »

dgpeters wrote:convert: profile 'icc': 'RGB ': RGB color space not permitted on grayscale PNG `test2.png' @ warning/png.c/MagickPNGWarningHandler/1744
That warning is just a warning, not an error, and should not prevent the command from working.

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
dgpeters
Posts: 18
Joined: 2013-09-10T15:47:00-07:00
Authentication code: 6789

Re: How do I remove solid colours if I don't know the colours in advance?

Post by dgpeters »

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
You were right, you can see above that I was missing a dash on my first argument (-alpha). Sigh. but I've added the dash in and now I get a different error:

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
Here's the current line which generates the error:

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
Here is how I set up my environment:

Code: Select all

export MAGICK_HOME="/Applications/ImageMagick-7.0.7"
export PATH="$MAGICK_HOME/bin:$PATH"
export DYLD_LIBRARY_PATH="$MAGICK_HOME/lib/"
And the last piece of info you may need is that my working directory is my desktop and it contains the following relevant files: (I placed z's where my real name went: (the png file was from my first attempt before I changed my screenshot to the tiff format)

Code: Select all

zzzzz-iMac:Desktop zzzzzz$ ls x* t*
test.png	test.tiff	x.sh
User avatar
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?

Post by fmw42 »

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
dgpeters
Posts: 18
Joined: 2013-09-10T15:47:00-07:00
Authentication code: 6789

Re: How do I remove solid colours if I don't know the colours in advance?

Post by dgpeters »

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"?
snibgo
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?

Post by snibgo »

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.
snibgo's IM pages: im.snibgo.com
Post Reply