Resampling using only filtered-out pixels

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?".
Post Reply
Radek215
Posts: 3
Joined: 2012-02-15T03:59:49-07:00
Authentication code: 8675308

Resampling using only filtered-out pixels

Post by Radek215 »

Hello,
First of all I would like to say what I am trying to achieve: the goal of my project is to spot the color of rooftop from the aerial photos.
I am dealing with bitmaps which correspond to approx 10x10m area, each centered onto given rooftop. I am applying Lanczos resampler to them to get a single output pixel. The results are quite satisfying but sometimes there are trees/green areas in the input bitmap which result in “greenish” output pixel (which is a bit unrealistic).
I’ve been trying to use ImageMagick to filter out green colour from input bitmaps (since normally there are green rooftops), using –opaque, -fuzz and –fill arguments. The problem is the argument for -fill parameter (replacement color) which always works only in certain cases (e.g. when it’s set to black it darkens the output pixel which is no good when remaining pixels are very bright)

Basically I would like to resample the input bitmap but taking into account only the pixels which are filtered out by –opaque argument (green pixels should be ignored).
Is it possible to achieve it with ImageMagick? Any other suggestions? (sorry for my limited English and knowledge of picture processing)

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

Re: Resampling using only filtered-out pixels

Post by fmw42 »

Can you post a link to an example input and output image along with your command line and IM version and platform. That would help us understand the problem and do some experiments.

Off-hand, I am not aware of any functionality directly in IM to ignore a color during resampling. But there may be workarounds with multiple commands. One needs to see what you are doing to figure out if there are ways to achieve that.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Resampling using only filtered-out pixels

Post by anthony »

To get re-sampling to ignore a color (prevent that color mixing with the other nearby colors), then the best way is to make that color fully-transparent! Fully-transparent colors are ignored to prevent 'halo effects'.
And works very well when no-transparency is present in the image!

Once the resampling is done all transparency effects are simply turned off using -alpha off.

This was closely related to a similar problem with creating a 'correct' Distort Resize...
http://www.imagemagick.org/Usage/distorts/#resize
And the solution was the same (though with a addition 'original image transparency' problem).

You can see details of using transparency as a 'read mask' in...
viewtopic.php?p=68362#p68362

Aside, this problem is also similar to a 'hole fill' problem, where you want to 'ignore' some part of the original image, replacing it with the equivalent result.


Possible in the Future...

In IMv7 there is talk of adding a 'read mask' to images. That is any pixel in the read mask will be ignored by filters, morphology, and convolutions, much like 'transparent' colors are usually (not always) ignored. However this work is still in alpha, and may not be available for some time.

Note a 'read mask' is very different to a 'write or clip mask' which is available, though little used in IMv6.
http://www.imagemagick.org/Usage/masking/#write_masks
A write mask, prevents writing to certain pixels, Or if the mask is negated 'protects' pixels from modification.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Resampling using only filtered-out pixels

Post by fmw42 »

Thanks Anthony. Learned something new today. Here are some simple tests that show that colors can be ignored when made into transparent values.

Image is half white and half black, so scaling down to one pixel (the equivalent of averaging all pixels in the image) should be about 0.5 in mean value.

convert -size 100x100 xc:black xc:white +append tmp1.png
convert tmp1.png -scale 1x1! -format "%[fx:u]" info:
0.499992



Make white transparent, so scaling down should pick out only the black and have mean of 0

convert -size 100x100 xc:black xc:white +append -transparent white tmp2.png
convert tmp2.png -scale 1x1! -format "%[fx:u]" info:
0



Make black transparent, so scaling down should pick out only the white and have mean of 1

convert -size 100x100 xc:black xc:white +append -transparent black tmp2.png
convert tmp2.png -scale 1x1! -format "%[fx:u]" info:
1

This allows one to get the mean of some arbitrary region by using a mask to make the rest transparent. A much simpler concept than what I did at http://www.fmwconcepts.com/imagemagick/ ... shape_mean
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Resampling using only filtered-out pixels

Post by anthony »

Remember color mean at one point also produces a mean of the hidden transparency colors.
But that was fixed last time it came up, as fully transparent colors are technically 'undefined'.

The 'ignore transparency' (transparency as a read mask) was solved early in IMv6, and was known as a 'halo' bug.
see IMv6 Examples, Resize Halo Bug
http://www.imagemagick.org/Usage/bugs/resize_halo/



Note these old bug pages will disappear (as being irrelevant) when I fork IMv6 Examples to IMv7 Examples, whcih will need to happen as soon as I start making CLI interface changes for IMv7 (at the moment I am working on CLI scripting command, not the options themselves).
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Radek215
Posts: 3
Joined: 2012-02-15T03:59:49-07:00
Authentication code: 8675308

Re: Resampling using only filtered-out pixels

Post by Radek215 »

thank you for your detailed answer: this project seems to have very helpful developers J

The solution you proposed helps quite a lot (the result textures after filtering-out some of the green pixels before resampling look more realistic then previously).
Yet I have not been able to filter out green areas completely: "-transparent green" removes only part of green pixels and adding too much -fuzz parameter removes other colours which might turn out to be important. Mind that the algorithm should work for the textures which contain green areas while it should not screw up the results for other textures.

I ended up with filtering out green pixels in small steps, sth like this:
Convert in.tga -fuzz 8% -transparent "rgb(50,129,75)" out1.tga
Convert out1.tga -fuzz 8% -transparent "rgb(172,181,98)" out2.tga
Convert out2.tga -fuzz 12% -transparent "rgb(38,92,4)" out3.tga
Convert out3.tga -fuzz 12% -transparent "rgb(22,78,15)" out4.tga

before applying resampling. Ugly and time-consuming but it gives the most realistic effects for most of the textures overall. Apparently it is not so easy to define “green” ;-)
Or maybe you have better idea how to implement it with current version of ImageMagick?

Future feature “read mask” seems like the best solution. I will try to implement it in my program myself by adding lookup table with green pixels and scanning the input pixels against it and adding alpha accordingly before resampling.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Resampling using only filtered-out pixels

Post by fmw42 »

You might get better results by converting your image to HSL and then filtering the Hue for your green and then converting back to RGB. That is one possibility, but untested. Also you can change to other colorspaces such as OHTA as shown in one of my replies to the link below.

Once you have isolate the colors that need to be transparent by whatever means you find, you can make a binary mask and put the mask into your original image's alpha channel.

See also
viewtopic.php?f=1&t=20280#p80564
and the following replies to it
Radek215
Posts: 3
Joined: 2012-02-15T03:59:49-07:00
Authentication code: 8675308

Re: Resampling using only filtered-out pixels

Post by Radek215 »

I followed your advice to convert to HSL (which is tbe whole new world for me, I learnt so much): I used hue and luma values to filter out unwanted "green" pixels in my program. The tweaking of cut-off parameters took me some time but the results are really satisfying! Thank you very much :-)

I have another question though (or should I start new topic for it?)
I have an RGB color and I would like to match it to the "closest" (by visual perception) color from predefined list of colors. I know it is very subjective task but anybody knows any algorithm for this? Should I just compare the hues?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Resampling using only filtered-out pixels

Post by fmw42 »

Yes, you should start a new post. But see my scripts, nearestcolor and remap. I am not sure that those are exactly what you want, but should give you some ideas. Perhaps you can explain a bit in more detail. These match according to a color distance metric. There is no visual perception involved.
Post Reply