how can i generalize this code?
显示 更早的评论
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
2013-8-19
At the moment, it's too cryptic for me to want to dive into. Please add some comments to make it easier for us to follow.
Image Analyst
2013-8-19
You've probably just developed an algorithm that is not robust to different means and standard deviations of the images that you want to use. What is it that you want to measure about the array of spots anyway??? Count, area, mean intensity, what?
PJM
2013-8-19
Image Analyst
2013-8-19
Do you have the Image Processing Toolbox? It seems like you area doing everything the manual, tedious way. It looks like you're taking the weighted centroid (center of mass) of the whole image rather than getting the weighted centroids of each spot individually. Is that what you want?
PJM
2013-8-19
Image Analyst
2013-8-20
Why are you adding or subtracting 3 from the logical indexes:
if mean(mean(ca))<60
ca(ca>mean(mean(ca))-3)=0;
else
ca(ca<mean(mean(ca))+3)=0;
end
What's the purpose of that? You're getting a list of all pixels less than the mean by doing
ca<mean(mean(ca))
this is a list of pixel locations (logical indexes). Then you +3 or -3 from the logical indexes so that you're now pointing to pixels 3 rows above or 3 rows below the indicated ones. Why???
PJM
2013-8-20
Image Analyst
2013-8-20
编辑:Image Analyst
2013-8-20
Nevermind my last comment. OK, so the 60 and the 3 are image dependent, but I still don't know what you are trying to do. I don't know why you're setting some pixels of the image to zero. Basically if the mean is less than 60 you're setting all pixels brighter than 3 darker than the mean to zero, otherwise you're setting all pixels darker than 3 brighter than the mean to zero. Isn't this going to zero out the spots? Do you just want to compute the weighted centroid of the entire image? Or do you want the centroid of each and every spot? Or the centroid of the image without the spots included?
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
2013-8-20
回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Region and Image Properties 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!