Help, should i be using compare with PSNR?

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?".
petalpetra
Posts: 7
Joined: 2015-02-22T02:57:09-07:00
Authentication code: 6789

Help, should i be using compare with PSNR?

Post by petalpetra »

Hey, I need help!

I am new to using ImageMagick.

I am wanting to use it to create some values to images for research purposes.

I am documenting the loss of bone by filling in a skeletal diagram (essentially blacking out the missing bones). So far i was using the the compare function with PSNR (so: compare -metric PSNR image1.png image2.png compare.png) by comparing the original blank diagram with the blackened out diagram depicting which bones are missing. In this way i get a value representing the difference between the images and essentially measuring or giving a ratio to the loss of bone for a skeleton? I need to do statistical analysis on the difference in bone loss throughout a number of skeletons.

If this makes any sense to anyone, do you think i should be using the PSNR function or should i be doing something differently.. (it seems to mainly be used in image compression comparisons - am I right?)
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Help, should i be using compare with PSNR?

Post by snibgo »

The documentation http://www.imagemagick.org/script/comma ... php#metric describes PSNR as peak signal to noise ratio, which sounds a strange way of measuring what you describe. A more obvious measure would be the proportion (or percentage) of bone loss.

This would be the number of changed pixels, divided by the number of pixels that represent bone (multiplied by 100).

"-metric AE" would give you a count of pixels that have changed. But if these are X-rays taken at different times, probably all the pixels will have changed (some not by much). Using AE with "-fuzz" might solve that.

RMSE would also seem useful here.

In any case, you might need to align images before comparing them.
snibgo's IM pages: im.snibgo.com
petalpetra
Posts: 7
Joined: 2015-02-22T02:57:09-07:00
Authentication code: 6789

Re: Help, should i be using compare with PSNR?

Post by petalpetra »

snibgo wrote:The documentation http://www.imagemagick.org/script/comma ... php#metric describes PSNR as peak signal to noise ratio, which sounds a strange way of measuring what you describe. A more obvious measure would be the proportion (or percentage) of bone loss.

This would be the number of changed pixels, divided by the number of pixels that represent bone (multiplied by 100).

"-metric AE" would give you a count of pixels that have changed. But if these are X-rays taken at different times, probably all the pixels will have changed (some not by much). Using AE with "-fuzz" might solve that.

RMSE would also seem useful here.

In any case, you might need to align images before comparing them.
Thanks very much! I will try the -metric AE story... will look it up now.

I am looking at fragmented skeletal material in a forensic context, so no xrays. To be more specific, I am looking at their condition on scene and then in the morgue in order to document the fragmentation change that occurs in recovery and transportation. I am therefore drawing in the fragmentation/condition of remains on scene and then another diagram once at the morgue and have been looking for a way to compare this numerically.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Help, should i be using compare with PSNR?

Post by snibgo »

Diagrams should be easier than X-rays, especially if the on-scene diagrams are on computer and these same diagrams are edited later at the morgue.

For some purposes, I often use "-compose Difference -composite", which gives an image that is black where the sources are the same, or various colours that are lighter where they are more different. This can be thresholded to give a count.

If you can put a before-and-after example online, someone might have more ideas. You can upload to somewhere like dropbox.com and paste URLs here. You can paint or crop out over any confidential details.
snibgo's IM pages: im.snibgo.com
petalpetra
Posts: 7
Joined: 2015-02-22T02:57:09-07:00
Authentication code: 6789

Re: Help, should i be using compare with PSNR?

Post by petalpetra »

Hey, here is a dropbox link to an example https://www.dropbox.com/sh/dcskqjscbpe7 ... gAS-a?dl=0

Please note that these are not the diagrams i will be using - i am waiting for permission to use a much more anatomically correct one... I was just playing around with these to get use to IM

So there is a plain image and then and image example "at scene" (black1.png) and then an image "at mortuary" (black12.png). I also included an output from IM illustrating the differences between the scene and mortuary - pretty cool so hope the AE measurement is the applicable one to be using?

Thank you so much for your help so far :)

I plan on doing these on a my tablet
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Help, should i be using compare with PSNR?

Post by snibgo »

Yes, that's good.

Code: Select all

compare -metric AE black1.png black12.png x.png

14923
So 14923 pixels have changed colour.

We can roughly count the pixels that represent bone, in any of the images. For example, black.png is the complete skeleton. I turn any non-white pixels black, and find the mean lightness. The mean lightness multiplied by the width and height is the number of white pixels.

Code: Select all

convert black.png -fill black +opaque White -format %[fx:mean*w*h] info:

122903
122903 are exactly white, and this is roughly the number of pixels in bones.

14923/122903 = 0.1214, or 12.14%. So 12.14% of the complete skeleton has been lost in transport.

You could do the complete calculation in a single command. When that does what you want, you could put them in a script for all the images.
snibgo's IM pages: im.snibgo.com
petalpetra
Posts: 7
Joined: 2015-02-22T02:57:09-07:00
Authentication code: 6789

Re: Help, should i be using compare with PSNR?

Post by petalpetra »

Wow thanks so much for that! That's great! The diagrams I will be using are much more accurate so hopefully the percentage will be too- I wish i had some sort of 3D way of doing the documentation but I think that may be a little above my capabilities, haha.

Your help has been much appreciated - I will keep you on hand in the future ;)
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Help, should i be using compare with PSNR?

Post by snibgo »

A more accurate count of the pixels in bones would come from cropping out the text at the bottom, and eliminating the thin white lines that surround the bones. Then there would be no need to convert non-white pixels into black.

For the percentage, you might want the percentage of the complete skeleton that has been lost, or the percentage of the each found skeleton that has been lost.

The complete skeleton would only need to be processed once.
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Help, should i be using compare with PSNR?

Post by fmw42 »

If you create a mask and put it into the alpha channel of the image, then the statistics should only take into count the opaque values.
petalpetra
Posts: 7
Joined: 2015-02-22T02:57:09-07:00
Authentication code: 6789

Re: Help, should i be using compare with PSNR?

Post by petalpetra »

Yeah I hope that once I receive my actual diagram that it will not have those issues (like the white surrounding the bone!).

Without needing to change anyn non-white pixels into black, what would the command look like to work out %?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Help, should i be using compare with PSNR?

Post by snibgo »

I had forgotten that "distortion" can't be used in an fx: expression, annoyingly. So it has to be two commands, and the syntax will depend on your script language. For example, with Windows BAT syntax:

Code: Select all

for /F "usebackq" %%L in (`%IM%compare ^
  -metric AE black1.png black12.png ^
  x.png`) do set DIFF=%%L

for /F "usebackq" %%L in (`%IM%convert ^
  black.png -fill black +opaque White -format "%%[fx:100*%DIFF%/(mean*w*h)]" ^
  info:`) do set DIFFPC=%%L

echo %DIFFPC%
Remove "-fill black +opaque White" if you don't want that.
snibgo's IM pages: im.snibgo.com
petalpetra
Posts: 7
Joined: 2015-02-22T02:57:09-07:00
Authentication code: 6789

Re: Help, should i be using compare with PSNR?

Post by petalpetra »

Snibgo!

I need your help again :) If you read our previous correspondence, i hope it will refresh your mind as to what I am trying to do with IM.

Since the last comments, I have got my proper image (still not that great) and I am running into some issues. Please find the basic image in the following dropbox link: https://www.dropbox.com/sh/dcskqjscbpe7 ... gAS-a?dl=0

Firstly, how would I go about calculating just the white part of this image, therefore just the bone? That way in the actual diagrams where bone is missing I can calculate a percentage of loss.

Also, is there some sort of function that will differentiate between colour? E.g. all charred bone coloured red but calcined (more burned) bone coloured blue? But still give me an overall comparison value which I can use for statistics.

I have had the opportunity to recently use the diagrams in a case, on scene and then at the mortuary. One e.g. of what i observed was on scene one of the legs was in tact but then at the mortuary in was fractured. I need to be able to draw this in and get statistical values for the differences! i was hoping, seeing that the leg is essentially not missing but just is now unattached, by shading the unattached piece in a different colour this would solve an issue but I'm not sure what sort of output values or image that would create.

I'm not sure ANY of that made sense. I am feeling daunted by my inexperience with Imagemagick and it is the only solution i can think of to measure these differences in pictures statistically.....
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Help, should i be using compare with PSNR?

Post by fmw42 »

since your image is mostly black and white with some anti-aliasing as gray, you can measure white pixel count (effectively giving partial white from the anti-aliased gray) by getting the mean value and multiplying by the width and height

count of skeleton pixels =

Code: Select all

convert yourimage -format "%[fx:mean*w*h]" info:
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Help, should i be using compare with PSNR?

Post by snibgo »

blank.png contains only black and white.

Code: Select all

convert blank.png   -define histogram:unique-colors=true -format "%c"   histogram:info:

    565337: (  0,  0,  0,255) #000000 black
     98236: (255,255,255,255) #FFFFFF white
If I fill in one bone black, another red, and another green:

Code: Select all

convert blankCol.png   -define histogram:unique-colors=true -format "%c"   histogram:info:

    569632: (  0,  0,  0,255) #000000 black
      3865: (  0,255,  0,255) #00FF00 lime
      4963: (255,  0,  0,255) #FF0000 red
     85113: (255,255,255,255) #FFFFFF white
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Help, should i be using compare with PSNR?

Post by fmw42 »

OOPS! I just dragged the image to my desktop and that was grayscale. But when I download it, snibgo is correct, it is binary. So a histogram gives the correct information.

However, my method also gives the same result.

Code: Select all

convert blank.png -format "%[fx:mean*w*h]\n" info:
98236

However, I think you would need to mask out the background to transparent and keep the proper skeleton and missing areas, if you want to identify bone from non-bone within the skeleton. The above gives you a count of white pixels, but you have no way to differentiate between missing bone from background in the image.
Post Reply