How to align images together automatically: Round 1

Hello!
Project changed, so I'm now trying to get these images aligned automatically. I currently have my script to make the image into a binary image, and then select the largest object with a bounding box. I want to get the coordinates of the selected object for the following images to align to it like a reference, but I don't know how.
Thank you!
-Frank
Note: These images are extremely similar to each other, but they show this line moving slightly to the right. (very slightly)
Images:
Code:
A = imread('343A #1.png')
background = imopen(A,strel('rectangle',[258 64]));
A2 = A - background;
A3 = imadjust(A2);
levelA = graythresh(A3);
bw = im2bw(A3, levelA);
bw = im2bw(A3, levelA);
bw = bwareaopen(bw,50);
ccA = bwconncomp(bw, 4);
ccA.NumObjects;
labeledA = labelmatrix(ccA);
whos labeledA;
RGB_labelA = label2rgb(labeledA, @autumn, 'c', 'shuffle');
imagedataA = regionprops(ccA, 'basic');
imagedataA(1).Area;
image_areasA = [imagedataA.Area];
[max_area, idx] = max(image_areasA);
imageA = false(size(bw));
imageA(ccA.PixelIdxList{idx}) = true;
figure, imshow(imageA)
regionprops(ccA, 'boundingbox', 'Centroid');
B = imread('343A #2.png')
background = imopen(B, strel('rectangle',[258 64]));
B2 = B - background;
B3 = imadjust(B2);
levelB = graythresh(B3)
bw = im2bw(B3, levelB);
bw = im2bw(B3, levelB);
bw = bwareaopen(bw,50);
ccB = bwconncomp(bw, 4);
ccB.NumObjects
labeledB = labelmatrix(ccB);
whos labeledB
RGB_labelB = label2rgb(labeledB, @spring, 'c', 'shuffle');
imagedataB = regionprops(ccB, 'basic');
imagedataB(1).Area
image_areasB = [imagedataB.Area];
[max_area, idx] = max(image_areasB);
imageB = false(size(bw));
imageB(ccB.PixelIdxList{idx}) = true;
figure, imshow(imageB)
regionprops(ccB, 'boundingbox');
regionprops(ccB, 'centroid')
regionprops(ccA, 'centroid')
p.s. if you know a better way, please share :) I'm really new to MatLab

 Accepted Answer

Once you know the displacement vector, use my imtranslate function

8 Comments

I'm having some difficulty getting the actual coordinates of the object in order to compare the displacements, unless there's another way to get displacement?
Is the strel function the right direction?
I would probably just get the centroid of the same object in both images and then subtract them. Whatever preprocessing you do to get to this point is correct or incorrect based on what you want; I don't know what that is. Morphological operations are important for cleaning up binary images and you do need a strel for that.
I don't know how to get the centroid of an object :(
Also, is strel able to "get" the object height? or would I have to determine it and input it into
strel('Rectangle', [width length]]); ?
The image is a piece of a cell activity over time. Each pixel line represents the amount of activity, each following row represents another scan of the same piece of cell.
strel just creates a kernel to be used in the convolution and is completely irrelevant to finding a displacement vector. The centroid is an optional output from regionprops.
I've updated what I have so far, and I'm not sure if I'm right.
The images are A, B, C, and D (left to right)
Would regionprops(ccA, 'centroid') evaluate the centroid of image A and store the coordinates somewhere in ccA? I've tried this script and received:
"ans =
2x1 struct array with fields:
Centroid..."
Sorry for my incompetence if this is taking too much of your time >.<
that means there are two objects in ccA. The centroid of the first is: ccA(1).Centroid and the second ccA(2).Centroid
Sorry, I didn't type my full results.
I received:
"
ans =
2x1 struct array with fields:
Centroid
ans =
6x1 struct array with fields:
Centroid
"
Got it working with a current script, thanks for imtranslate!
-Frank

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!