how can i generalize this code?
1 view (last 30 days)
Show older comments
And my main work is this,
clear all;
img=imread('CropTest1_crop.jpg','jpg');
Gimg=0.2989*img(:,:,1)+0.5870*img(:,:,2)+0.1140*img(:,:,3);
np=121;
snp=sqrt(np);
height=size(img,1);
width=size(img,2);
H=height/snp;
W=width/snp;
p=[];
for i=1:snp
for j=1:snp
A=round((i-8/10)*H);
B=round((i-2/10)*H);
C=round((j-8/10)*W);
D=round((j-2/10)*W);
if B>height
B=height;
elseif D>width
D=width;
end
ca=Gimg(A:B,C:D);
if mean(mean(ca))<60
ca(ca>mean(mean(ca))-3)=0;
else
ca(ca<mean(mean(ca))+3)=0;
end
[x(j) y(j)]=abc(ca);
end
array=[x; y]';
for j=1:snp
array(j,1)=array(j,1)+round((j-8/10)*W);
array(j,2)=array(j,2)+round((i-8/10)*H);
end
p=[p; array];
end
xp=p(:,1);
yp=p(:,2);
image(img); colormap(gray(256)); axis image;
hold on;
plot(xp,yp,'r*');
disp('Centroiding is done.');
and function of abc in main work is this,
function [meanx meany]=abc(img)
[x,y,z]=size(img);
if (z~=1)
img=0.2989*img(:,:,1)+0.5870*img(:,:,2)+0.1140*img(:,:,3);
end
[rows cols]=size(img);
x=ones(rows,1)*[1:cols];
y=[1:rows]'*ones(1,cols);
area=sum(sum(img));
meanx=sum(sum(double(img).*x))/area;
meany=sum(sum(double(img).*y))/area;
end
if I change the image, my work cannot work accurately.
or different images, how can i generalize my main work specially cropping image?
[you should change the value of variable 'np(number of points)'->1681 when you change the image.]
thank you for your advice and help.
12 Comments
Image Analyst
on 20 Aug 2013
Can't you just threshold, and call regionprops()? It's like 3 or 4 lines of code, once you have the threshold determined.
Answers (0)
See Also
Categories
Find more on Image Segmentation and Analysis in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!