# Find the center of specific picture area

10 views (last 30 days)
Charles P on 8 Sep 2015
Commented: Charles P on 8 Sep 2015
Hi, I'm quite new to Matlab.
I want to identify the center of a specific picture area.
In my case, I have 2 pictures which I determined the difference between each other (Subtraction). Resulting of a white area, marking the difference. Here's my problem, I cannot find roughly the center of my white area. Note that I have no error in the execution, just a problem with this calculus. Can someone help me with this please. Here's a sample of my code:
clear all; close all;
[filename, pathname] = uigetfile({'*.jpg;*.tif;*.PNG;*.gif;*.bmp','All Image Files';...
'*.*','All Files' },'img1',...
'C:\Users\CAP\Desktop\SCHOOL\MATLAB\cible.jpg')
if (length(size(img1))>2)
img1=img1(:,:,1);
end
[filename, pathname] = uigetfile({'*.jpg;*.tif;*.PNG;*.gif;*.bmp','All Image Files';...
'*.*','All Files' },'img2',...
'C:\Users\CAP\Desktop\SCHOOL\MATLAB\cibleref.jpg') image
if (length(size(img2))>2)
img2=img2(:,:,1);
end
%Determination of the difference between the 2 pictures by Subtraction
[ligne colonne]=size(img1);
image_result=uint8(zeros(ligne,colonne));
for i = 1: ligne
for j = 1 : colonne
if img1(i,j)>=img2(i,j)
image_result(i,j)= img1(i,j)-img2(i,j);
else
image_result(i,j)= img2(i,j)-img1(i,j);
end
end
end
%Finding the coordinates of the white zone (difference)
[x,y]=find(image_result>128); %0:black 255:white
x1=(sum(x)/length(x)); %Trying here to figure out the center of my white area
y1=(sum(y)/length(y)); %Trying here to figure out the center of my white area
% Display
figure(1)
subplot(221)
imshow(img1)
subplot(222)
imshow(img2)
subplot(223)
imshow(image_result)
hold on;
plot(x1,y1,'r.','MarkerSize',20)
Top Right: Reference Image
Top Left: Image including difference
Bottom: Result image by subtraction. The Red dot corresponds to the result of my white zone determination. As you can see there is a sightly error in the calculus...

Hamoon on 8 Sep 2015
That's because x1 refers to columns of the image and y1 refers to the rows of the image, so for plot you should write:
plot(y1,x1,'r.','MarkerSize',20)
in plot we first write rows and then columns: plot(rows,columns)
Charles P on 8 Sep 2015
Thank you very much.

Image Analyst on 8 Sep 2015
find() returns [y, x], (which is rows, columns). find() does not return [x, y] like you have. Switch that and you'll have fewer problems. Of course you don't need a double for loop, and can just use regionprops() like in my image segmentation tutorial: http://www.mathworks.com/matlabcentral/fileexchange/?term=authorid%3A31862
Sriram Badri on 8 Sep 2015
If you could suggest a way to solve it, it would be really helpful. N thanks a lot