complex color conversion problem

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
mikesl
Posts: 2
Joined: 2013-02-05T01:17:58-07:00
Authentication code: 6789

Re: complex color conversion problem

Post by mikesl »

Hello,

I'm trying to use this same technique to remove the green background in this image but have not been able to figure it out.

Image

Here's what I have so far:

Code: Select all

convert s4.jpg -colorspace HSL -channel Hue -separate -background none -fuzz 16% +transparent "gray(48%
)" -alpha extract -compose multiply -morphology Smooth Square:6 s4key.jpg
I have tried a ton of different values for fuzz and gray with no luck. Here's the closest I get:

Code: Select all

convert s4.jpg -colorspace HSL -channel Hue -separate -background none -fuzz 2% +transparent "gray(84)"
 -alpha extract -compose multiply s4key.jpg
You can see I'm using a very low fuzz. The detail in the bicycle wheel is completely gone. When I add smoothing, it gets even worse. What am I missing here? Why would the black spokes as well as her black jacket be included in the mask that's green?

Image

Secondary question, after running separate Hue, how do I figure out the "gray" value without guessing? I have Photoshop but can't seem to figure out the mapping.

Thanks
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: complex color conversion problem

Post by snibgo »

Why would the black spokes as well as her black jacket be included in the mask that's green?
Her jacket isn't black. Look at it closely. Blow it up. Use the eyedropper in a tool such as Gimp.

Her jacket is dark green. Very dark green, but the hue is the same as the backgound.

The shiny bits on the bike are also green.

Put it another way: just run the first part of your command (shown here in Windows script)...

Code: Select all

convert ^
  bike.jpg ^
  -colorspace HSL ^
  -channel Hue -separate ^
s.png
...and you'll see the problem. Hues are represented here as shades of gray, and her jacket merges into the background, as do many parts of the bike.

No, I'm afraid that just looking at hue alone won't work for this image. Hue and lightness, together, would separate the jacket from the background. But distinguishing those spokes will be difficult.
snibgo's IM pages: im.snibgo.com
mikesl
Posts: 2
Joined: 2013-02-05T01:17:58-07:00
Authentication code: 6789

Re: complex color conversion problem

Post by mikesl »

Wow, didn't think of that at all. Thanks.

How would I change the command to additionally look at lightness? I tried following the example but can't seem to run it on windows. Getting error with clone image not found. Seems like a syntax error.

Code: Select all

               convert ... -coorspace HSL -channel Hue,Lightness -separate +channel ^
               \( -clone 0 -background none -fuzz 5% +transparent grey64 \) ^
               \( -clone 1 -background none -fuzz 10% -transparent black \) ^ 
              ...
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: complex color conversion problem

Post by Bonzo »

On windows you do not need to escape the parenthases so use ( ) not \( \)
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: complex color conversion problem

Post by fmw42 »

One other process you might add is to use part of the background image at the top to get the horizontal color profile to remove the color and variation from that part of the image. You could use this as a preprocess or as a separate process to combine masks.

Try this with and without the -threshold. Here I convert to grayscale, then use the top 55 rows of the image, average it down to one row, expand it back to the full size of the image and divide to remove the background. The optionally threshold. It will not help with the shadow around the object, but will remove some left right color variation.


convert ZkC6by8l.jpg -set colorspace RGB -colorspace gray \
\( -clone 0 -crop 640x55+0+0 +repage -scale 640x1! -scale 640x480! \) \
+swap -compose divide -composite -auto-level -threshold 40% show:


Also you can close up some of the white areas in your result by using morphologic open

convert PaJISN7l.jpg -morphology open diamond:2 show:
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: complex color conversion problem

Post by anthony »

Seperating with both hue and saturation should work. The jacket will not only be dark but also have little saturation.

The original example shows how you can combine saturation with hue, but it can also do lightness too.

The other way is convert teh image to HSL (or HCL the newer hue colorspace) and select a few background samples to remove using -transparency.

A third way is to select multiple background points and use -sparse-color to generate a 'background image with no foreground' This image can then be used to differentiate between background (and shadowed background) and foreground pixels using techniques shown in...
Masking and Background Removal
http://www.imagemagick.org/Usage/masking/#bg_remove

Note this last method may keep green reflections.

There are lots of ways to skin a cat, and what method you use depends
on what you want that skin for, and how messy you like the results!
-- Anthony Thyssen
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: complex color conversion problem

Post by snibgo »

One weakness of HSB etc is that the Hue channel is all over the place at low saturations. The Lab colorspace doesn't have this problem.

Code: Select all

"%IMG%convert" ^
  bike.jpg ^
  -colorspace Lab ^
  -separate ^
  -auto-level ^
  b.png
If we magnify b-1.png by 400%, we can admire the 16x16 jpeg artefacts. Horrible! I hate working from jpeg originals. Still, b-1.png and b-2.png look useful. Let's negate one and overlay:

Code: Select all

"%IMG%convert" ^
  ( b-1.png -negate ) ^
  b-2.png ^
  -compose Overlay -composite ^
  -threshold 60%% ^
  bc.png
I can't find a threshold that captures all the detail (spokes, brake cables, mudguard stays) without also catching the backdrop. So let's find the detail with a different method, using standard deviation:

Code: Select all

"%IMG682%convert" ^
  bike.jpg ^
  -modulate 100,0,100 ^
  ( -clone 0 -statistic StandardDeviation 2x2   -auto-level ) ^
  ( -clone 0 -statistic StandardDeviation 5x5   -auto-level ) ^
  ( -clone 0 -statistic StandardDeviation 10x10 -auto-level ) ^
  -delete 0 ^
  ( -clone 0 -clone 1 -compose Screen -composite ) ^
  -compose Screen -composite ^
  -negate ^
  bikeSDa.png
... and combine these two masks ...

Code: Select all

"%IMG%convert" ^
  bikeSDa.png ^
  bc.png ^
  -compose Multiply -composite ^
  bcm.png
Yeah, that's not too bad. It shows the 16x16 jpeg artefacts, and suffers an edge problem. It doesn't find the white frame near the bottom bracket (near the pedals), where the frame has reflected the backdrop and blends into it.

If we use this mask to overlay the girl+bike against a red background...

Code: Select all

"%IMG%convert" ^
  -size 640x480 xc:Red ^
  ( bike.jpg bcm.png -compose Copy_Opacity -composite ) ^
  -compose Over -composite ^
  bikeRed.png
... we see small problems at the girl's belt and throat, and the wheel rim. Further work could fix these. I think the bike frame at the bottom bracket needs manual intervention.

Image
https://www.dropbox.com/s/0ap4orgzucpwb34/bikeRed.png

Incidentally, does the cyclist know her chain has fallen off?
snibgo's IM pages: im.snibgo.com
eisaias2000
Posts: 2
Joined: 2013-02-25T07:35:03-07:00
Authentication code: 6789

Re: complex color conversion problem

Post by eisaias2000 »

Hello, I'm working editing image of documents and believe the chroma key feature demonstrated will solve my problem.

I have to turn the background into white, spite of usefull, I couldn't achieve the result with examples exposed.

Follows two sample images and the expected result (done in photoshop).

Can someone show me the way?

Image
Image
Image
Image

This doens't properly edit this last photo. :-(
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: complex color conversion problem

Post by snibgo »

In the second photo, the shirt blends into the background so the result will be very sensitive to parameters. Here is a first attempt (Windows script):

Code: Select all

set SRC=20130220_164414.jpg

"%IMG%convert" ^
  %SRC% ^
  -fuzz 10%% ^
  -trim +repage ^
  -write h0.png ^
  ( +clone -blur 0x2 ) ^
  -compose Difference -composite ^
  -colorspace RGB ^
  -contrast-stretch 10%%,10%% ^
  h1.png

FOR /F "tokens=1,2" %%i IN ('%IM%identify -ping -format "%%w %%h" h1.png') DO (
  set WIDTH=%%i
  set HEIGHT=%%j
)

set /A wminus=%WIDTH%-10
set /A halfHt=%HEIGHT/2

"%IMG%convert" ^
  h1.png ^
  -fuzz 20%% ^
  -fill Red -draw "color %wminus%,%halfHt% floodfill" ^
  h2.png

"%IMG%convert" ^
  h2.png ^
  -fill White +opaque Red ^
  -fill Black -opaque Red ^
  h3.png

"%IMG%convert" ^
  h3.png ^
  -morphology Open Disk:3 ^
  -morphology Erode Disk:3 ^
  -blur 0x2 ^
  h4.png

"%IMG%convert" ^
  h0.png ^
  h4.png ^
  -compose CopyOpacity -composite ^
  h5.png
snibgo's IM pages: im.snibgo.com
eisaias2000
Posts: 2
Joined: 2013-02-25T07:35:03-07:00
Authentication code: 6789

Re: complex color conversion problem

Post by eisaias2000 »

Thank you a lot!
Post Reply