how can i generalize this code?
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 19 Aug 2013
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
on 19 Aug 2013
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
on 19 Aug 2013
Image Analyst
on 19 Aug 2013
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
on 19 Aug 2013
Image Analyst
on 20 Aug 2013
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
on 20 Aug 2013
Image Analyst
on 20 Aug 2013
Edited: Image Analyst
on 20 Aug 2013
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
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.
PJM
on 20 Aug 2013
Answers (0)
Categories
Find more on Region and Image Properties 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!