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 Comments

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.
PJM
PJM on 19 Aug 2013
Edited: PJM on 19 Aug 2013
Thanks for your comment,
Well, main work has about this process: read image->crop image(A,B,C,D)->delete unnecessary information of crop image(ca)->find points in crop image(abc function)->find x,y coordinates in points at original image(since for-loop upper p=[p; array];)->plot image
but when i change the image, i have something problem in cropping(A,B,C,D) (...also in ca) because of different information of image.
i want to generalize my work like function. although the image is changed, my work activates well.
I'm sorry I cannot explain well because of my English skills. :'(
If you still cryptic, leave a comment.
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?
Centroid. function abc gets centroid of spot.
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?
Thanks for your comment.
I have the image processing toolbox. However, I do not wish to use it. First of all, in the case of unique image, weighted centroid of regionprops cannot represent all the points that care necessary. Second, the most important reason, there is a person who do not want the code using toolbox. I apologize for requesting such inconvenient way of making a code.
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???
Thanks for your comment.
Well, without them, accuracy of centroid decreases. Numbers(60,+3,-3) are ...meaningless and temporary. These are available on the first image only. If you change the image, you need to change the numbers to fit the new image, too. For example, when I use the second image, I delete the if-else-end and just need:
ca(ca<mean(mean(ca))+20)=0; % 20 is also temporary number.
And this is another source of trouble to generalize. :(
Image Analyst
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?
PJM
PJM on 20 Aug 2013
Edited: PJM on 20 Aug 2013
Thanks for your comment.
I want the centroid of each and every spot. if-else-end zero process works background pixel information to zero, not spot to zero. Because the number of background pixels are more than spot pixels, so mean of ca is lower than spot pixels.(except <60 case) Zero process makes slightly gap at coordinates of points with not processing. If the process is not exist, background pixel information has an effect on centroid function(abc). And in case of some special spots, the gap increases. As you can see, the process has two cases. else case for normal spot and if(<60) case for particular spot: spots are darker than background like (row,column)=(3,5) or (11,7).
I'm concerned about my humble explanation.
Can't you just threshold, and call regionprops()? It's like 3 or 4 lines of code, once you have the threshold determined.
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)

Sign in to comment.

Answers (0)

Tags

Asked:

PJM
on 19 Aug 2013

Community Treasure Hunt

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

Start Hunting!