How to compare two images from each other: Round 2

3 views (last 30 days)
Hello again!
Slightly stuck on this issue. I need help on finding a way for MatLab to follow an object from one picture to the next, automatically. I've written a small script from help online that allows me to select the object and determine its coordinates, but is there a way that MatLab can see the object without me manually tracing it?
Thanks!,
-Frank
Images:
Code:
I = imread('rice.png');
%%imshow(I)
background = imopen(I,strel('disk',15));
figure, surf(double(background(1:8:end,1:8:end))),zlim([0 255]);
set(gca,'ydir','reverse');
%%Darkens image
I2 = I - background;
%figure, imshow(I2)
%%Increases contrast
I3 = imadjust(I2);
%figure, imshow(I3);
%%Turns image into binary colors (Black and White)
level = graythresh(I3);
bw = im2bw(I3,level);
bw = bwareaopen(bw, 50);
%%figure, imshow(bw)
cc = bwconncomp(bw, 4);
cc.NumObjects
%%Select grain connected to background
grain = false(size(bw));
grain(cc.PixelIdxList{50}) = true;
figure, imshow(grain);
labeled = labelmatrix(cc);
whos labeled
%%Color Shift
RGB_label = label2rgb(labeled, @autumn, 'c', 'shuffle');
figure, imshow(RGB_label)
graindata = regionprops(cc, 'basic');
graindata(1).Area
%%Grain area
grain_areas = [graindata.Area];
[max_area, idx] = max(grain_areas);
grain = false(size(bw));
grain(cc.PixelIdxList{idx}) = true;
figure, imshow(grain)
%%Select a portion to allocate coordinates of object
[x, y, ~, xi, yi] = roipoly(grain);
BW = roipoly(100, 200, xi, yi);
%%Displays xi, yi coordinates
figure, imshow(BW)
grain2 = imread('Rice3.gif');
[x2,y2,Bw,xi2, yi2] = roipoly(grain2);
  1 Comment
Frank
Frank on 13 May 2011
My goal for this is to track the object and calculate how much it has moved from picture to picture

Sign in to comment.

Accepted Answer

Sean de Wolski
Sean de Wolski on 13 May 2011
For this rice example, what do you know about that grain that makes it different that the rest?
  • Its size (+- a few pixels due to digitization)
  • The area of its boundingbox
  • Eccentricity
These are just three options of:
doc regionprops
that could be used.
  • Basically you would isolate the object you want in the first image.
  • Run the same binarization operation on the second image
  • do a connected components analysis in both, keeping the connected component of interest in the first one. (doc bwconncomp)
  • Get the measurements of all objects in the second image.
  • Whichever one is the most similar is your match.
Note: these are just a few of the things you could do. Looking at your above binary image, binarized, they seem feasible. If you want to compare in non-binary images then we might have to figure out something a little more complicated.
By the way, well written question - this is what allows you get a good answer
  4 Comments
Sean de Wolski
Sean de Wolski on 13 May 2011
If the grain is the only object in the image (like above) then yes. If not it'll calculate the boundingbox for every object in CC. From this you'd have to figure out which one was the one you want.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!