Clear Filters
Clear Filters

solve my problem in my code

1 view (last 30 days)
mandana
mandana on 8 Sep 2018
Commented: Image Analyst on 8 Sep 2018
hi i have a code for clustering an image by calculating color difference between 2pixels in rgb image.but it has no error and no answer who can help me? please its emeregency
clc
clear
close all
npop=1;
num_center_w=100;
tend=20;
t=1;
imrgb=imread('a.jpg;');
image=im2double(imrgb);
mask=fspecial('prewitt');
im_filtered =imfilter(image,mask);
image2=imsubtract(image,im_filtered);
image4 =2* image2;
imageresized=imresize(image4,[512 512]);
S=size(imageresized);
%A=numel(impixel(image2));
%B=nnz(impixel(image2));
%num_zero=A-B;
num_row=S(1,1);
num_col=S(1,2);
num_superpixel=200;
ss= round(sqrt((num_row*num_col)/num_superpixel));
searcharea=ss;
center.activecode=2;
center.corx=[];
center.cory=[];
center.cost=[];
center.color=[];
center.labelw=[];
center.labelc=[];
low=searcharea+1;
max_x=num_row-searcharea;
max_y=num_col-searcharea;
lbx=low;
ubx=max_x;
lby=low;
uby=max_y;
% m=number of centers of evry whale
pop= repmat(center,npop,num_center_w);
i=1;
j=1;
while( i<=npop )
while( j<=num_center_w)
A=randi([low,max_x]);
B=randi([low,max_y]);
pop(i,j).corx=unique(A);
pop(i,j).cory=unique(B);
pop(i,j).color= impixel(imageresized,pop(i).corx,pop(i).cory);
if(( isnan (pop(i,j).color) )|( pop(i,j).color==0 ))
while(( isnan (pop(i,j).color) )|( pop(i,j).color==0 ))
B=randi([low,max_y]);
pop(i,j).corx=unique(A);
pop(i,j).cory=unique(B);
pop(i,j).color= impixel(imageresized,pop(i).corx,pop(i).cory);
end
end
u=1;
uu=1;
while(u<i)
while(uu<j)
if (pop(i,j).color ==pop(u,uu).color )
while( pop(i,j).color ==pop(u,uu).color)
B=randi([low,max_y]);
pop(i,j).corx=unique(A);
pop(i,j).cory=unique(B);
pop(i,j).color= impixel(imageresized,pop(i).corx,pop(i).cory);
uu=uu+1;
end
u=u+1;
end
end
end
end
%pop(i,j).label=strcat('wale',v,'c',w);
pop(i,j).labelw=i;
pop(i,j).labelc=j;
pop(i,j).cost=[];
% pop(i).cost= ClusteringCost(pop(i).corx,pop(i).cory, pop(i).color);
j=j+1;
end
i=i+1;
end
%%calculate distance evry center and evry pixel in search area (2d)
whale.r=[];
whale.g=[];
whale.b=[];
win_color=[];
test.r=[];
test.g=[];
test.b=[];
im_clustred.x=[];
im_clustred.y=[];
im_clustred.r=[];
im_clustred.g=[];
im_clustred.b=[];
im_clustred.seen=zeros(num_row,num_col);
im_clustred.dis=zeros(num_row,num_col);
im_clustred.labelw=[];
im_clustred.labelc=[];
again.x=[];
again.y=[];
again.dis=zeros(num_row,num_col);
again.seen=zeros(num_row,num_col);
again.labelc=[];
again.labelw=[];
i=1;
j=1;
while (i<=npop)
while (j<=num_center_w)
p=(pop(i,j).corx)-(searcharea);
q=(pop(i,j).cory)-(searcharea);
pp=(pop(i,j).corx)+(searcharea);
qq=(pop(i,j).cory)+(searcharea);
whale.r=pop(i,j).color(1);
whale.g=pop(i,j).color(2);
whale.b=pop(i,j).color(3);
while (p<=pp)
while(q<=qq)
win_color= impixel(image2,p,q);
if ( isnan (win_color))
win_color=[0 0 0];
end
test.r=win_color(1);
test.g=win_color(2);
test.b=win_color(3);
dis = sqrt(( test.r - whale.r).^ 2 + ...
(test.g - whale.g).^ 2 + test.b- whale.b).^ 2 ;
if ( isnan (dis))
dis=0;
end
if ((im_clustred.seen(p,q)==1)||(again.seen(p,q)==1))
n= lt(im_clustred.dis(p,q),dis);
nn=lt(again.dis(p,q),dis);
if (n)
docluster=1;
end
if(~n)
docluster=2;
end
if(nn)
docluster=3;
end
if(~nn)
docluster=4;
end
switch docluster
case 1
q=q+1;
case 2
im_clustred.x(p,q)=p;
im_clustred.y(p,q)=q;
im_clustred.r(p,q)= whale.r(i,j);
im_clustred.g(p,q)= whale.g(i,j);
im_clustred.b(p,q)= whale.b(i,j);
im_clustred.labelw(p,q)=i;
im_clustred.labelc(p,q)=j;
im_clustred.dis(p,q)=dis;
im_clustred.seen(p,q)=1;
q=q+1;
case 3
q=q+1;
case 4
again.x(p,q)=p;
again.y(p,q)=q;
again.dis(p,q)=dis;
again.seen(p,q)=1;
again.labelc(p,q)=j;
again.labelw(p,q)=i;
q=q+1;
end
else
u=lt(0,dis);
uu=lt(dis,0.5);
v=lt(dis,1);
if(u && uu)
doingcluster=1;
end
if(v && ~uu)
doingcluster=2;
end
if(~v)
doingcluster=3;
end
switch doingcluster
case 1
im_clustred.x(p,q)=p;
im_clustred.y(p,q)=q;
im_clustred.r(p,q)= whale.r(i,j);
im_clustred.g(p,q)= whale.g(i,j);
im_clustred.b(p,q)= whale.b(i,j);
im_clustred.labelw(p,q)=i;
im_clustred.labelc(p,q)=j;
im_clustred.dis(p,q)=dis;
im_clustred.seen(p,q)=1;
q=q+1;
case 2
again.x(p,q)=p;
again.y(p,q)=q;
again.dis(p,q)=dis;
again.seen(p,q)=1;
again.labelc(p,q)=j;
again.labelw(p,q)=i;
q=q+1;
case 3
q=q+1;
end
end
end
p=p+1;
q=qq-searcharea;
end
j=j+1;
end
i=i+1;
end
while(p<=num_row)
while(q<=num_col)
if(again.seen(p,q)==1)
im_clustred.labelc(p,q)=again.labelc(p,q);
im_clustred.dis(p,q)=again.dis(p,q);
im_clustred.labelw(p,q)= again.labelw(p,q);
end
q=q+1;
end
p=p+1;
end
  2 Comments
Stephen23
Stephen23 on 8 Sep 2018
Edited: Stephen23 on 8 Sep 2018
@mandana: your code is very badly aligned. Badly aligned code is one way that beginners hide mistakes in their code. You should align your code using the default MATLAB editor settings: you can align existing code by selecting the code and pressing ctrl+i.
Pay attention to the syntax warnings/errors in the editor: you have atleast one syntax error, which is much easier to find once the code is aligned properly.
Image Analyst
Image Analyst on 8 Sep 2018
Another common beginner mistake is no comments. In hundreds of lines of code you have one virtually worthless comment. What does this code do? I have no idea and I'm an expert in color science. Something to do with whales, clusters, and pop. Put comments in and make your variables more descriptive -- not i,j,p,q, etc. (a virtual alphabet soup that is very hard to follow). And attach your a.jpg so we can at least run the code even though we don't know what it does. And, by the way, don't use JPG files if you can help it. JPG files usually have compression artifacts that reduce the accuracy of your image analysis.

Sign in to comment.

Answers (0)

Categories

Find more on Image Processing Toolbox in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!