Thread Subject: comparing images with same number of points

Subject: comparing images with same number of points

From: Juliette Salexa

Date: 27 Aug, 2009 21:05:20

Message: 1 of 13

Hello,

I'm aware that 'image comparison' topics have come up quite a lot ( I searched ) , but I couldn't find a discussion related to what I want to do.

I also couldn't find an image processing forum that looks decent, does anybody know of any ??

Well, I want to calcualte a 'distance' between two images that have the same number of points, and the distance should be small if most of the points are in the same locations and should be large if they are far apart.

For example, in the following four images, if d(X,Y) is the 'distance' between X and Y, d(A,B) should be small, d(C,D) shoudl be small, d(A,C),d(A,D),d(B,C), and d(B,D) should be large:

A: http://bayimg.com/IAdhkAACi
B: http://bayimg.com/iadhMAaci
C: http://bayimg.com/JAdHDaAcI
D: http://bayimg.com/jadheaACi

I looked at the FEX and couldn't find anything that looks like it does this. Does anyone know if there's any software that would do this (preferably in matlab, although I am open to other possibilities) ??

Thanks in advance,
Juliette

Subject: comparing images with same number of points

From: ImageAnalyst

Date: 27 Aug, 2009 21:52:59

Message: 2 of 13

On Aug 27, 5:05 pm, "Juliette Salexa" <juliette.physic...@gmail.com>
wrote:
> Hello,
>
> I'm aware that 'image comparison' topics have come up quite a lot ( I searched ) , but I couldn't find a discussion related to what I want to do.  
>
> I also couldn't find an image processing forum that looks decent, does anybody know of any ??
>
> Well, I want to calcualte a 'distance' between two images that have the same number of points, and the distance should be small if most of the points are in the same locations and should be large if they are far apart.  
>
> For example, in the following four images, if d(X,Y) is the 'distance' between X and Y, d(A,B) should be small, d(C,D) shoudl be small, d(A,C),d(A,D),d(B,C), and d(B,D) should be large:
>
> A:http://bayimg.com/IAdhkAACi
> B:http://bayimg.com/iadhMAaci
> C:http://bayimg.com/JAdHDaAcI
> D:http://bayimg.com/jadheaACi
>
> I looked at the FEX and couldn't find anything that looks like it does this. Does anyone know if there's any software that would do this (preferably in matlab, although I am open to other possibilities) ??
>
> Thanks in advance,
> Juliette

--------------------------------------------------------------------------
What do you get if you just take the sqrt of the sum the squares of
the difference in pixel values - like taking the RMS difference or
PSNR? Does that work for you?

Subject: comparing images with same number of points

From: Juliette Salexa

Date: 27 Aug, 2009 22:29:02

Message: 3 of 13

Thank you ImageAnalyst! =)

RMS difference between pixel values sounds like it will do the job I want,
but how do I get the pixel map of the images and take the differences ??

I googled a bit but couldnt' find anything specific.

let's say I named my figures this way:

image1=figure(1); *some code to make figure 1 the figure that it ends up being*
image2=figure(2); *some code to make figure 2 the figure that it ends up being*
image3=figure(3); *some code to make figure 3 the figure that it ends up being*
image4=figure(4); *some code to make figure 4 the figure that it ends up being*

Now rms(image1,image2) doesn't give me any answer.

Can I find the rms between their pixel values using only features that came with regular matlab ?? Or do I have to use the image processing toolkit or an m-file from the FEX ?? I searched a few keywords like 'pixel' in the FEX and couldn't see anything that would do this, at least at first glance.

Any help would be very appreciated.

Subject: comparing images with same number of points

From: ImageAnalyst

Date: 28 Aug, 2009 02:33:34

Message: 4 of 13

On Aug 27, 6:29 pm, "Juliette Salexa" <juliette.physic...@gmail.com>
wrote:
> Thank you ImageAnalyst! =)
>
> RMS difference between pixel values sounds like it will do the job I want,
> but how do I get the pixel map of the images and take the differences ??
>
> I googled a bit but couldnt' find anything specific.
>
> let's say I named my figures this way:
>
> image1=figure(1); *some code to make figure 1 the figure that it ends up being*
> image2=figure(2); *some code to make figure 2 the figure that it ends up being*
> image3=figure(3); *some code to make figure 3 the figure that it ends up being*
> image4=figure(4); *some code to make figure 4 the figure that it ends up being*
>
> Now rms(image1,image2) doesn't give me any answer.
>
> Can I find the rms between their pixel values using only features that came with regular matlab ?? Or do I have to use the image processing toolkit or an m-file from the FEX ?? I searched a few keywords like 'pixel' in the FEX and couldn't see anything that would do this, at least at first glance.
>
> Any help would be very appreciated.

--------------------------------------------------------------------------------
That won't work. Your "images" are just handles to the figure windows
- they're not actually images. To do that you need to call imread()
and give it the filename of an image. It will return the image as an
array. Why don't you look at my demo/tutorial that I just uploaded to
the File Exchange today:
http://www.mathworks.com/matlabcentral/fileexchange/25157
Maybe you might also check out this submission (I haven't done
anything with it other than read the description)
http://www.mathworks.com/matlabcentral/fileexchange/25005
or maybe this one:
http://www.mathworks.com/matlabcentral/fileexchange/24224

Write back if you still have questions.

Subject: comparing images with same number of points

From: Juliette Salexa

Date: 28 Aug, 2009 03:03:04

Message: 5 of 13

Thank you ImageAnalyst,

I ran your demo in R2007b (my IPT is version 6.0) and got errors. Some of them I copied and pasted below, but the longer ones take up a lot of space. I can e-mail you the exact output of error messages if it concerns you. But this is probably because your code was tested on R2009b, so maybe there's some features I don't have.
-----------------------------------------------------------
Running BlobsDemo.m...
??? Error using ==> regionprops>ParseInputs at 867
This measurement is not a string: "49".This measurement is not a string: "47".This measurement is

Error in ==> regionprops at 114
[L, requestedStats] = ParseInputs(officialStats, varargin{:});

Error in ==> BlobsDemo at 73
blobMeasurements = regionprops(labeledImage, originalImage, 'all');
-----------------------------------------------------------

But thank you for the response and for the links to those urls.

1. do we HAVE to load the image from a file even if the image was created in matlab ?? I was worried that during matlab's process of converting the matrix into a .png some information would be lost. Does the .png created by matlab contain the exact same pixel map as the figure that matlab originally displays ??

2. I noticed that the RMS difference between pixels won't work in my case because:
(a) well first off RMS would be unecessary because all my pixels are either black or white so the Euclidean distance is just equal to the square root of the Hamming distance
(b) I expect NONE of the pixels in any of those four images to match, so my distance between each image would be zero, even though images a and b are very similar to each other, c and d are very similar to each other, and {a,b} are very different from {c,d}

I am exploring other techniques now, if anyone has tried something similar and has any suggestions I would greatly appreciate them.

Thanks again!

Subject: comparing images with same number of points

From: ImageAnalyst

Date: 28 Aug, 2009 11:57:41

Message: 6 of 13

Juliette Salexa:
Like it says in the comments, the ability to have the original image
sent in to regionprops, and the MeanIntensity property, were added in
R2008a, it's not in your old version. Why aren't you upgrading after
2 years? Are you not paying for a maintenance contract? There are
instructions in the code to handle earlier version so if you follow
them, you'll change this:

% Get all the blob properties. Can only pass in originalImage in
version R2008a and later.
blobMeasurements = regionprops(labeledImage, originalImage, 'all');
 to
blobMeasurements = regionprops(labeledImage, 'all');

and where it says this:

% Find the mean of each blob. (R2008a has a better way where you can
pass the original image
% directly into regionprops. The way below works for all versions
including earlier versions.)
    thisBlobsPixels = blobMeasurements(k).PixelIdxList; % Get list of
pixels in current blob.
    meanGL = mean(originalImage(thisBlobsPixels)); % Find mean
intensity (in original image!)
meanGL2008a = blobMeasurements(k).MeanIntensity; % Mean again, but
only for version >= R2008a

you just comment out the meanGL2008a line and use the meanGL line
instead. After that I think it should work.

Anyway..... regarding your other points:
1. No you don't have to get the image array from a file. It's just
any numerical array, no matter how it is created.

2. I was afraid of that, and half expecting it. If all those little
dots in your image are very sparse, then it could well be that very
few of them happen to overlap, and so your differences may be
essentially the same for the two image pairs. In that case what you
want to do, is basically what your brain is doing - you need to ignore
all the little dots and look for broader patterns. If you average
together areas then you would have areas that are denser and less
dense, despite being sparse (assuming you averaged together a big
enough window to include more than one dot). Ok, so the way you do
this is to blur your image and then resize. This has the effect of
block averaging down your image. Use the conv2() function to average
together a window of pixels, say a 9 by 9 window. Now each pixel of
the blurred image is proportional to the sum of the number of dots in
the window centered around the corresponding window in the input
image. You could then do a imresize() to shrink it down by a factor
of 9 if you wanted to speed things up a bit when you subtract them.
But it should be very fast even if you don't. You could also use the
bsxfun() function but I think conv2() is easier to understand and use,
or you could use imfilter(). After you do the blurring, and optional
downsizing, it should work fine. Let me know how it goes.
Good luck,
ImageAnalyst

Subject: comparing images with same number of points

From: Juliette Salexa

Date: 28 Aug, 2009 15:19:03

Message: 7 of 13

Thank you ImageAnalyst!

Changing those two lines did reduce the number of errors significantly!
I still get this one:

??? Reference to non-existent field 'MeanIntensity'.
Error in ==> BlobsDemo at 108
allBlobIntensities = [blobMeasurements.MeanIntensity];

When I look at blobMeasurements in my workspace window, it doesn't seem to have a structure field called MeanIntensity. Maybe this has to do with me having R2007b. I can't upgrade it because R2007b is installed on a workstation owned by my workplace and they only have a licence for R2007b.

Thanks for the suggestions though, they will be very helpful.

The blurring is a good idea. Before I read that message, I was thinking of just partitioning the image into a grid and counting the number of black pixels in each square. Then I'd have a matrix whose elements correspond to the number of black pixels in each square, and I can calculate the Euclidean distance between two such matrices. It seems like this will do the same trick since you said the blurring the image makes each pixel proportional to the number of black pixels in an area.
-----------------------------
There's one thing that I'm not able to do yet though,
I think I'm able to get the pixel map of an image by doing:
h=imread('picture.png') , where h will be a n x m x 3 array, each of the 3 slices corresponding to the R,G or B value of the pixels corresponding to each element.

But how about if I had something like this:

image2=figure(2);
plot(some stuff...)

How can I get the pixel map of image2 the way I got it for 'picture.png' ??
I've tried impixelregion(image2) and impixelinfo(image2) and got error messages both times

Thank you once again.

Subject: comparing images with same number of points

From: ImageAnalyst

Date: 28 Aug, 2009 16:40:56

Message: 8 of 13

Juliette Salexa:
Yes counting the black dots inside some window is essentially the same
as using conv2() or using bsxfun() to sum all the gray levels within
some window. They're all basically the same thing. You get an output
array where the value of each pixel is proportional to the ratio of
white and black dots inside the window. Trust me: you'll find conv2()
is the easiest way to do it.

Again, when you say image2=figure(2), image2 is just the handle to the
window, not an actual image array like you get if you use image2=imread
(filename). I'm not sure why you insist on trying to do it that way.
It won't work for images. Now if you wanted to do a graph of some
kind (line plot, bar chart, pie chart, etc.) then you can do that and
try to save the plot (Oliver Woodford has some good utilities for
doing that in the File Exchange), but this is saving a graphics image,
not an image like a photo.
Good luck,
ImageAnalyst

Subject: comparing images with same number of points

From: Juliette Salexa

Date: 28 Aug, 2009 18:40:20

Message: 9 of 13

Thank you again,

I will take your advice and try conv2().

As for trying to avoid using imread, the reason why was because my figure is being generated in matlab using the plot command, so matlab already has all the information necessary to produce the image.. so it would just slow things down to print the plot to a .png and then read it.... instead I just wanted to get the pixel map right away. [This is because I'm doing this in a loop for thousands of pictures, so each time it prints the image to a .png file and then reads it, it's taking up unecessary time]. But if matlab can't get a pixelmap for a plot until it's saved to a .png (or .jpg, etc..) , then I guess that's what I'll have to do.

Thanks a lot again, I'm learning a lot and it looks like my overall goal will work out eventually.

Thanks

Subject: comparing images with same number of points

From: Juliette Salexa

Date: 28 Aug, 2009 19:27:04

Message: 10 of 13

Hmm...
Okay, I tried the conv2 function but I'm really uneasy about whether or not I'm using it correctly:


>> Jannaschia1=imread('Jannaschia(1-100000).png');

Now to use conv2 I have to take one slice of Jannaschia1 (since conv2 only takes 2D input.... but this is fine since all 3 slices of Jannaschia1 should be the same), and I have to convert it from unit8 to double and add 1:

>> blurred=conv2(double(Jannaschia1(:,:,1))+1,[2 1 0; 1 0 -1; 0 -1 -2],'valid');


Now to make it black and white I need to add the Red and Green slices again:

>> blurred2=zeros(898,1198,3);
>> blurred2(:,:,1)=blurred;
>> blurred2(:,:,2)=blurred;
>> blurred2(:,:,3)=blurred;


Now image() will only take values between 0 and 1 so I did this:

image(abs(blurred2)/(max(max(max(abs(blurred2))))))


I'm not really sure why the second input to conv2() has to be that matrix, but I just got that from the documentation for conv2(), where it says:

"In image processing, the Sobel edge finding operation is a two-dimensional convolution of an input array with the special matrix

s = [1 2 1; 0 0 0; -1 -2 -1]; "

---------------------------------------------

I'm not entirely sure if I blurred my image properly (so that each pixel is the average of the number of pixels around it), but I see at least one problem so far:

The image Jannaschia(1-100000).png consists of a title and some labeling of the axes, and the actual plot is in a box WITHIN the box surrounding the .png file

so what I get is :
http://bayimg.com/oadLfaaCi

While the original was:
http://bayimg.com/IAdhkAACi

making the blurred version suboptimal for comparison purposes.
Also I think the black and white switched during the process somewhere.

This was part of the reason why I didn't want to read images, and I wanted to work with just the original matlab plots themselves, but I'm not sure if there's a way around that.

Any help would be greatly appreciated.

And thanks again, I've already learned so mcuh!

Juliette

Subject: comparing images with same number of points

From: Juliette Salexa

Date: 30 Aug, 2009 17:12:01

Message: 11 of 13

OKay so there's absolutely no way to get the pixelmap of a plot without saving it to an image and then reading it ??

If so, is there a way to write images so that there's absolutely no space between the axes and the end of the image ??

Subject: comparing images with same number of points

From: Steven Lord

Date: 31 Aug, 2009 14:04:13

Message: 12 of 13


"Juliette Salexa" <juliette.physicist@gmail.com> wrote in message
news:h7ebt1$r74$1@fred.mathworks.com...
> OKay so there's absolutely no way to get the pixelmap of a plot without
> saving it to an image and then reading it ??

You can try GETFRAME; that may suit your needs.

> If so, is there a way to write images so that there's absolutely no space
> between the axes and the end of the image ??

Are you looking for AXIS TIGHT or AXIS IMAGE?

http://www.mathworks.com/access/helpdesk/help/techdoc/ref/axis.html

--
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ

Subject: comparing images with same number of points

From: Juliette Salexa

Date: 28 Oct, 2009 17:37:01

Message: 13 of 13

Thank you SO MUCH steven Lord! getframe was exactly what I needed!
I don't even need the second part anymore because I can just use getframe to obtain the pixelmap and therefore don't need to save them images and read them!

Thanks so much once again!

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
compare images Sprinceana 28 Aug, 2009 01:26:21
rssFeed for this Thread

Contact us at files@mathworks.com