Convert with fuzz not ideal. Any better alternative to use underlying feature as ROI?

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?".
burt46
Posts: 20
Joined: 2016-06-06T11:48:45-07:00
Authentication code: 1151

Convert with fuzz not ideal. Any better alternative to use underlying feature as ROI?

Post by burt46 »

I have aerial images of various river beds that I have to overlay an image to identify the presence of water. I am 80% there to get the solution I need but I think my way of doing this is not the best.

What I would like to do is extract the darker area of the river bed in the aerial image (1.png) then apply this area (ROI) to crop the water image (2.png), making it transparent outside this crop area. Then combine them to overlay the cropped area on the original image (such as 3.png).

I have done this using fuzz, but it’s not quite there. This method does not try to extract the darker areas of the original image and that is why it is not very good. I am just trimming the green which is trimming the green over the darker areas which I want to prevent.

Just to document how I am generating image 3 as attached.

Code: Select all

>convert 2.png –fuzz 40% -transparent green 2a.png
>composite 1.png 2a.png 3.png
Any ideas how to use the darker area of image 1.png to trim image 2.png, or a better method?

1.png
Image

2.png
Image

2a.png
Image

3.png
Image
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Convert with fuzz not ideal. Any better alternative to use underlying feature as ROI?

Post by fmw42 »

Please, always provide your IM version and platform when asking questions, since syntax may differ.

Unix syntax -- try this

Code: Select all

convert 1.png \
\( -clone 0 -fill red -colorize 100% \) \
\( -clone 0 -fuzz 25% -fill black -opaque black -fill white +opaque black -negate \
-morphology open octagon:1 -morphology close octagon:3 \) \
-compose over -composite 1_red.png
Image

For windows users, remove the \ before ( and before ) and replace \ and the end of lines with ^
burt46
Posts: 20
Joined: 2016-06-06T11:48:45-07:00
Authentication code: 1151

Re: Convert with fuzz not ideal. Any better alternative to use underlying feature as ROI?

Post by burt46 »

Ok, i get the same results as you (i am using ImageMagick-6.9.4-Q16 on Windows 10). However, the red should be replaced with the image in 2.png as an overlay. How do i extract the red and trim the 2.png, then composite?

Thanks for the reply and help
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Convert with fuzz not ideal. Any better alternative to use underlying feature as ROI?

Post by fmw42 »

Code: Select all

convert 1.png 2.png \
\( -clone 0 -fuzz 25% -fill black -opaque black -fill white +opaque black -negate \
-morphology open octagon:1 -morphology close octagon:3 \) \
-compose over -composite 1_2.png
Image
burt46
Posts: 20
Joined: 2016-06-06T11:48:45-07:00
Authentication code: 1151

Re: Convert with fuzz not ideal. Any better alternative to use underlying feature as ROI?

Post by burt46 »

I should have picked up that modification, i didnt realise it could be done inline.

I tried to modify your last line so i can composite the final 1_2.png "over" with my original 2a.png (i can make sure it captures all areas this way) but it wont recognise the general -composite concept

Code: Select all

-compose over -composite 1_2.png 2a.png 4.png
It generates three seperate images.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Convert with fuzz not ideal. Any better alternative to use underlying feature as ROI?

Post by fmw42 »

What was your full command line? I am not sure I understand what it is that you want to do. Why do you need 2a.png? My command creates the equivalent of 2a.png internally to put that information over the image. So why do you want to use that rather than or in addition to 2.png? What is missing from my result?

You must specify the input names before the -composite and then one output name after wards. You need to specify your input image right after convert.
burt46
Posts: 20
Joined: 2016-06-06T11:48:45-07:00
Authentication code: 1151

Re: Convert with fuzz not ideal. Any better alternative to use underlying feature as ROI?

Post by burt46 »

I tried your final code for other areas and it did not pick up all the darker areas. If I increase the fuzz it picks up areas outside the riverbed. However, if i use my original code modified to generate 2a.png (the -transparent green), i can see some of these extra areas.

Therefore, the final image is a combination of your code plus the code used to generate 2a.png.

I can do this in two lines, just using

Code: Select all

composite 1_2.png 2a.png final.png
However, i was trying to do this in a single step, including my -transparent green code to generate 2a.png
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Convert with fuzz not ideal. Any better alternative to use underlying feature as ROI?

Post by fmw42 »

Code: Select all

convert 1.png 2.png \
\( -clone 0 -fuzz 25% -fill black -opaque black -fill white +opaque black -negate \
-morphology open octagon:1 -morphology close octagon:3 \) \
-compose over -composite \
2a.png -compose over -composite \
1_2_2a.png
Image
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Convert with fuzz not ideal. Any better alternative to use underlying feature as ROI?

Post by fmw42 »

Here is another approach:

Code: Select all

convert 1.png 2.png \
\( -clone 0 -blur 0x0.6 -statistic minimum 3x3 -threshold 25% -negate \
-morphology open octagon:1 -morphology close octagon:3 \) \
-compose over -composite 1_2_v2.png
Image
burt46
Posts: 20
Joined: 2016-06-06T11:48:45-07:00
Authentication code: 1151

Re: Convert with fuzz not ideal. Any better alternative to use underlying feature as ROI?

Post by burt46 »

Thanks for the input. The last version works the best, particularly when combined with the orignal -transparent green method.

Code: Select all

convert a1.png a2.png ( -clone 0 -blur 0x0.6 -statistic minimum 3x3 -threshold 20% -negate -morphology open octagon:1 -morphology close octagon:3 ) -compose over -composite a3.png
For some small rivers it also works well but it picks up scatter outside the area. Based on using this script i get a3.png as an output. Two questions.

1. How can i remove the green scatter outside my area of interest?
2. Is it possible to somehow show the small tributaries which have a lighter color?

a1.png
Image

a2.png
Image

a3.png
Image
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Convert with fuzz not ideal. Any better alternative to use underlying feature as ROI?

Post by fmw42 »

1. How can i remove the green scatter outside my area of interest?
2. Is it possible to somehow show the small tributaries which have a lighter color?
1) Use a lower threshold value (and/or possibly change the blur amount) and combine that with connected components labeling. See http://magick.imagemagick.org/script/co ... onents.php

2) I doubt your can easily distinguish the small channel color from the rest of the image without getting more of the same effect of introducing false regions.
burt46
Posts: 20
Joined: 2016-06-06T11:48:45-07:00
Authentication code: 1151

Re: Convert with fuzz not ideal. Any better alternative to use underlying feature as ROI?

Post by burt46 »

Ok, this is my last question on this. How can i apply your last code, but to only show the 2.png result with a transparente background (i.e. do not have the 1.png underneath). This is the code that works but i dont need the underlying aerial image.

Code: Select all

convert 1.png 2.png \
\( -clone 0 -blur 0x0.6 -statistic minimum 3x3 -threshold 25% -negate \
-morphology open octagon:1 -morphology close octagon:3 \) \
-compose over -composite 1_2_v2.png
I tried

Code: Select all

convert 2.png \
\( -clone 0 -blur 0x0.6 -statistic minimum 3x3 -threshold 25% -negate \
-morphology open octagon:1 -morphology close octagon:3 \) \
-compose over 1_2_v2.png
but it comes out black.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Convert with fuzz not ideal. Any better alternative to use underlying feature as ROI?

Post by fmw42 »

try

Code: Select all

convert 1.png 2.png \
\( -clone 0 -blur 0x0.6 -statistic minimum 3x3 -threshold 25% -negate \
-morphology open octagon:1 -morphology close octagon:3 \) \
-delete 0 -alpha off -compose copy_opacity -composite 2_trans.png
burt46
Posts: 20
Joined: 2016-06-06T11:48:45-07:00
Authentication code: 1151

Re: Convert with fuzz not ideal. Any better alternative to use underlying feature as ROI?

Post by burt46 »

Ok this works, but not inside a windows batch file. I think its something to do with the brackets. Do i need to change the syntax?

Thanks for the help.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Convert with fuzz not ideal. Any better alternative to use underlying feature as ROI?

Post by fmw42 »

burt46 wrote:Ok this works, but not inside a windows batch file. I think its something to do with the brackets. Do i need to change the syntax?

Thanks for the help.
The above is unix syntax. In windows try:

Code: Select all

convert 1.png 2.png ^
( -clone 0 -blur 0x0.6 -statistic minimum 3x3 -threshold 25% -negate ^
-morphology open octagon:1 -morphology close octagon:3 ) ^
-delete 0 -alpha off -compose copy_opacity -composite 2_trans.png
In a .bat file, double the % to %%

See http://www.imagemagick.org/Usage/windows/
Post Reply