how can i generalize this code?

1 次查看(过去 30 天)
PJM
PJM 2013-8-19
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 个评论
Image Analyst
Image Analyst 2013-8-20
Can't you just threshold, and call regionprops()? It's like 3 or 4 lines of code, once you have the threshold determined.
PJM
PJM 2013-8-20
Thanks for your continuous attention!
And you can see the code used regionprops in Image Processing Toolbox. You can see the result of the code, too. But it cannot find all spots. Can you find all spots when used regionprops? (Thank you again for my past question. :D)

请先登录,再进行评论。

回答(0 个)

标签

Community Treasure Hunt

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

Start Hunting!

Translated by