Request Enhancments Similar to -mode

Questions and postings pertaining to the development of ImageMagick, feature enhancements, and ImageMagick internals. ImageMagick source code and algorithms are discussed here. Usage questions which are too arcane for the normal user list should also be posted here.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Request Enhancments Similar to -mode

Post by fmw42 »

I would like to request that -mode widthxheight be replicated to generate other statistical values, in particular -min, -max, -mean and -standard-deviation for a given rectangular widthxheight. I don't know if it is possible to put all the options under one name, such as -stats-filter. If so that would be OK and even better. But perhaps the syntax parser will not currently allow something like -stats-filter widthxheight+type where type is min, max, mean, or standard_deviation. This type of operation could even be expanded to such things as the average of k nearest in value (to the center value) and other such statistical operations.

I realize that -mean can be achieve by -blur radiusx65000 and -min and -max can be achieved via -morphology open/close square:radius, but the latter two are very slow and also I am not really sure how to convert the square:radius to the actual window size. Perhaps it is just radius=half-size. Clarification would be nice from Anthony. None however allow a rectangular area, only square area.

Discussion from others is requested and welcome before any action is taken. Are these worthwhile to others?


Thanks

Fred
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Request Enhancments Similar to -mode

Post by anthony »

-min -max -mean are morphology operators erode, dilate, convolve.
Just specify the appropriate neighbourhood and channels

You can also use ErodeIntensity and DilateIntenasity
to select the color (whole pixel) based in the color intensity. (You can't get a -mean with intensity)

It was median the is very different (middle intensity of all pixels in neighbourhood).
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: Request Enhancments Similar to -mode

Post by fmw42 »

anthony wrote:-min -max -mean are morphology operators erode, dilate, convolve.
Just specify the appropriate neighbourhood and channels

You can also use ErodeIntensity and DilateIntenasity
to select the color (whole pixel) based in the color intensity. (You can't get a -mean with intensity)

It was median the is very different (middle intensity of all pixels in neighbourhood).

Thanks, I accidentally used close and open rather than dilate and erode in my script. Hopefully that will fix it. But I want to be sure that in

-morphology erode/dilate square:radius

is radius just the size/2 (+1?) for a square. If I want the size to be 32, would I use radius of 16 or 17 or something else?
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Request Enhancments Similar to -mode

Post by anthony »

Square:r is a square r*2+1 with the origin centered.

See http://www.imagemagick.org/Usage/morphology/#square

Also look at rectangle!
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: Request Enhancments Similar to -mode

Post by fmw42 »

Thanks, but still very slow compared to using -blur radiusx65000 to get mean. But there ought to be similar functions to get min, max, mean, standard-deviation, mode and other statistical operations on a rectangle that run faster than -morphology erode/dilate square:radius

Fred
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Request Enhancments Similar to -mode

Post by anthony »

The -blur may be used some GPU processing for ultra fast processing. ;-)
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: Request Enhancments Similar to -mode

Post by fmw42 »

anthony wrote:The -blur may be used some GPU processing for ultra fast processing. ;-)

But I need something similar to -blur for getting min and max in a region.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Request Enhancments Similar to -mode

Post by anthony »

Then your choices are either use morphology, or a dedicated (less neighbourhood controlled) MagickCore function for speed.



Aside. I want to make the change to Blur so that by default it understands transparency (no haloing).

To do this I would make -channel sync flag (turned on by default and means synchronize channel handling) perform what is current enabled using -channel RGBA

This will however mean that -channel RGBA (which turns off sync) will blur each channel separately and thus
could produce black transparency halos.

Basically it will bring the operator more in line with other operators, like -auto-level, -morphology, and the mathematical composition methods.
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: Request Enhancments Similar to -mode

Post by fmw42 »

I withdraw my request. Seems for now, that -morphology and -blur achieves most of what I need.

Fred
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: Request Enhancments Similar to -mode

Post by magick »

Too late. -statistic {median, mode, minimum, maximum, nonpeak} has already been added to ImageMagick 6.6.8-6 Beta available by sometime tomorrow.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Request Enhancments Similar to -mode

Post by fmw42 »

magick wrote:Too late. -statistic {median, mode, minimum, maximum, nonpeak} has already been added to ImageMagick 6.6.8-6 Beta available by sometime tomorrow.
Sorry you went to the trouble. I did not think you were going to implement anything until after discussions had finished. But since you have. Thanks.

But what about the dimensions argument? How does that work to specify the type of stats and the widthxheight? What is the syntax? What is non-peak? Also what about average (mean)? Or are you leaving that to -blur radiusx65000? And also standard-deviation? I guess these can come later.

Other variations can be found in the listings for my statsfilt script at http://www.fmwconcepts.com/imagemagick/ ... /index.php, but I don't mean to suggest you necessarily implement any or all. But your implementation opens the door for other noise filtering variations such as in my script. Some of these require further arguments that may not fit into the current IM argument parsing.

Another good one, that I did not implement in my script is called kth nearest neighbor average. If you are so inclined, it averages the k (user supplied) pixels whose values are closest to the value of the center pixel along with the center pixels. However, the extra argument k, may not fit well with your current argument parsing!

Fred
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: Request Enhancments Similar to -mode

Post by magick »

Baby steps. The first release handles a neighborhood of widthxwidth.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Request Enhancments Similar to -mode

Post by fmw42 »

magick wrote:Baby steps. The first release handles a neighborhood of widthxwidth.

Right. Just curious about the syntax.
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: Request Enhancments Similar to -mode

Post by magick »

Ok, we're supporting width x height afterall.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Request Enhancments Similar to -mode

Post by fmw42 »

-statistic seems to be working correctly now in IM 6.6.8.10 Q16. Thanks.

This is not urgent, But at some point when you get time, could you possibly add standard-deviation. Here is one method that I have used. But do it any way you want.

For each channel:
1) Compute sum of graylevel and divide by N to get average
2) compute sum of graylevel^2 and divde by N to get average
3) Compute std = sqrt (variance) = sqrt( ave(g^2) - (ave(g))^2 )

see
http://en.wikipedia.org/wiki/Standard_deviation
http://en.wikipedia.org/wiki/Variance

Thanks

Fred
Post Reply