I've written a script that obtains the centroid of an object in my image. I store that value as a reference and calculate the difference from the other images so I can align them to the first. However the coordinates are stored in the variable "displacement" and I only want the x coordinate. Is there a way I can get it automatically?
Code
o = imread('343TESTA.tif');p = size(o);imtool(o);for k = 1:p(1) for l = 1:p(2) if o(k,l) > 160 & o(k,l) > 160 d(k,l) = 1; else d(k,l) = 0; end endendimtool(d);labela = bwlabel(d);imagesc(labela);[labela,num] = bwlabel(d,4);stats = regionprops(labela,'basic');stats(1).Areastats(1).Centroidmax_area = max([stats.Area]);biggrain = find([stats.Area]== max_area);p = stats(biggrain).Centroid%Second Image
a = imread('343TEST.tif');b = size(a);imtool(a);for c = 1:b(1) for l = 1:b(2) if i(c,l) > 160 & a(c,l) > 160 e(c,l) = 1; else e(c,l) = 0; end endendimtool(e);labelb = bwlabel(e);imagesc(labelb);[labelb,num] = bwlabel(e,4);stats = regionprops(labelb,'basic');stats(1).Areastats(1).Centroidmax_area = max([stats.Area]);biggrain = find([stats.Area]== max_area);o = stats(biggrain).Centroid%Calculates the differences in the centroid
displacement = p -o%%IM Translate
a2 = imtranslate(a, [0 -1.0376]); %Note: [y x] for imtranslate, if it's negative make it positive
subplot(1,2,1), imshow(v)subplot(1,2,2), imshow(u2)imwrite(a2, '343TEST11','tif');
Output (example):
Displacement = -1.036 -6.7288
I only want the -1.036 portion
Best Answer