convert -normalize , ignore edges without crop

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
pet
Posts: 13
Joined: 2013-08-13T10:11:30-07:00
Authentication code: 6789
Location: Switzerland

convert -normalize , ignore edges without crop

Post by pet »

hello all...,

give it a solution tu use [convert -normalize] but ignore any edges of a image ?

for example:
only calculate 85% (yellow part) of the image and ignore 15% of the edges.
the result have to be like my image out.jpg, (but with the edges !!) .

my example image in.jpg , white or black areas (edges) can significantly skew the calculation of color balance if not ignored.
the wrong output is the same like the in-file :?

Image

greetings from Switzerland

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

Re: convert -normalize , ignore edges without crop

Post by snibgo »

"-normalize" builds a histogram and calculates from that what "-level" is required, and applies that "-level". (Internally, it doesn't use "-level", but takes a short cut to do the same thing.)

You want to crop your image for the "-normalize", but apply the "-level" to the entire image.

Sadly, the "-normalize" command can't tell you what "-level" is uses.

I think the best way is as follows:

1. Crop the image.
2. Normalize the crop.
3. Use the technique I show on my "Gain and Bias" page to find what "-level" was applied.
4. Apply that "-level" to the entire image.
snibgo's IM pages: im.snibgo.com
pet
Posts: 13
Joined: 2013-08-13T10:11:30-07:00
Authentication code: 6789
Location: Switzerland

Re: convert -normalize , ignore edges without crop

Post by pet »

many thanks for the help.
i understand the point 1. and 2.
and i think i understand the way from 3. and 4.
on the hompage "Gain and Bias" i can see that i have to read the infos of the image, but i dont understand, how i can do it with (-level)
can you give me a example ?
or is necessary to develop a great script ?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: convert -normalize , ignore edges without crop

Post by snibgo »

Yes, it needs a script. My scripts are for Windows BAT. What do you use? BAT? Bash? Something else?

My scripts are quite complex because they cope with transparency in the input image, and they process the colour channels separately (to adjust colour balance). They can be simplified when the image is entirely opaque, and when you want to apply the same transformation to all three channels. Something like:

Code: Select all

convert in.png -format "%[fx:mean] %[fx:standard_deviation]" info:
Do this for the crop, and for the normalized crop. These four numbers can be used to calculate the numbers for "-level".
snibgo's IM pages: im.snibgo.com
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: convert -normalize , ignore edges without crop

Post by snibgo »

Here is a complete Windows BAT script. Adjust SRC and sCROP as required. The output is written to out.png. The line

Code: Select all

+write x.png ^
... isn't required, and can be removed. It may be useful to check you are cropping the correct area, and that it normalizes well.

"-function Polynomial" takes parameters that are more convenient than "-level", but it has the same effect.

Code: Select all

set SRC=toes.png

set sCROP=-gravity Center -crop 50%%x50%%+0+0 +repage

for /F "usebackq" %%L in (`%IM%convert ^
  %SRC% ^
  %sCROP% ^
  -format "MN0=%%[fx:mean]\nSD0=%%[fx:standard_deviation]\n" ^
  +write info: ^
  -normalize ^
+write x.png ^
  -format "MN1=%%[fx:mean]\nSD1=%%[fx:standard_deviation]\n" ^
  info:`) do set %%L

for /F "usebackq" %%L in (`%IM%identify ^
  -format "GAIN=%%[fx:%SD1%/%SD0%]\nBIAS=%%[fx:%MN1%-%MN0%*%SD1%/%SD0%]"
  xc:`) do set %%L

%IM%convert %SRC% -function Polynomial %GAIN%,%BIAS% out.png
snibgo's IM pages: im.snibgo.com
pet
Posts: 13
Joined: 2013-08-13T10:11:30-07:00
Authentication code: 6789
Location: Switzerland

Re: convert -normalize , ignore edges without crop

Post by pet »

wow... thank for the script-example.
i work with linux (bash) but i think i can translate the windows- to the shell-bash script.
and try a picture :)
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: convert -normalize , ignore edges without crop

Post by snibgo »

Translating to bash is trivial:

Code: Select all

SRC=toes.png

sCROP="-gravity Center -crop 50%%x50%%+0+0 +repage"

declare `convert \
  $SRC \
  $sCROP \
  -format "MN0=%[fx:mean]\nSD0=%[fx:standard_deviation]\n" \
  +write info: \
  -normalize \
+write x.png \
  -format "MN1=%[fx:mean]\nSD1=%[fx:standard_deviation]\n" \
  info:`

declare `identify \
  -format "GAIN=%[fx:$SD1/$SD0]\nBIAS=%[fx:$MN1-$MN0*$SD1/$SD0]" \
  xc:`

convert $SRC -function Polynomial $GAIN,$BIAS g.png
snibgo's IM pages: im.snibgo.com
pet
Posts: 13
Joined: 2013-08-13T10:11:30-07:00
Authentication code: 6789
Location: Switzerland

Re: convert -normalize , ignore edges without crop

Post by pet »

thanks, it works perfect for me !
does exactly what I need :D :D

another question: can you make little 'IM' jobs for me ?
I still have some work where I have no solutions ....
I like to pay for good solutions
or you know who make it


pet
Post Reply