Code covered by the BSD License

color based computer vision

by

amit (view profile)

i need it with detail description

pixellabel.m
```%color based computer vision
%it is divided into three parts illustrated as follows kindly guide me

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%1  Matlab code for color definition
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

clear all
colors = [] ;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Set ranges for H and S.
h = 0.03:0.002:0.13 ;
s = 0.7:0.002:1 ;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Make a column so that every combination
% of H, S and V is possible.
ss = [] ;
for i = 1:length(h)
hh((i-1)*length(s)+1:i*length(s),1) = h(i) ;
ss = [ss ; s'] ;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Define the range for V and make a matrix
% of the same size as hh and ss.
v = repmat([0.64:0.002:1],[length(hh),1],1) ;
colors(:,:,1) = repmat(hh,[1,length(v(1,:))],1) ;
colors(:,:,2) = repmat(ss,[1,length(v(1,:))],1) ;
colors(:,:,3) = v ;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Transform the HSV values to RGB values and
% let the values range from 1 to 256.
colors = hsv2rgb(colors) ;
colors = round(colors*255)+1 ;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Put all defined colors in a 3D-matrix
% named clrs.
clrs = repmat(0,[256,256],256) ;
s = size(colors) ;
for i = 1:s(1)
for j = 1:s(2)
clrs(colors(i,j,1),colors(i,j,2),colors(i,j,3)) = 1 ;
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Manual color definition
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% color = imread('HSV colors.bmp') ;
% figure(1)
% imshow(color)
%
% regions(:,:,n) = roipoly(color) ;
% img = rgb2hsv(color) ;
%
% H = img(:,:,1) ;
% S = img(:,:,2) ;
% V = img(:,:,3) ;
%
% h = H(regions(:,:,n)) ;
% s = S(regions(:,:,n)) ;
% v = repmat([0.64:0.002:1],[length(h),1],1) ;
%
% hh = repmat(h,[1,181],1) ;
% ss = repmat(s,[1,181],1) ;
%
% colors(:,:,1) = repmat(hh,[1,length(v(1,:))],1) ;
% colors(:,:,2) = repmat(ss,[1,length(v(1,:))],1) ;
% colors(:,:,3) = v ;
%
% colors = hsv2rgb(colors) ;
% colors = round(colors*255)+1 ;
%
% clrs = repmat(0,[256,256],256) ;
% s = size(colors) ;
% for i = 1:s(1)
% for j = 1:s(2)
% clrs(colors(i,j,1),colors(i,j,2),colors(i,j,3)) = 1 ;
% end
% end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%2  Matlab code for pixel labeling
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Define the pixel interval.
pix = 1 ;
p = round(pix/2) ;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Import the image for pixel labeling
% (in the final version this is a videoinput).
img = double(img) ;
h = size(img,1) ;
w = size(img,2) ;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Set the size of the segmented image
% and perform pixel labeling.
segmented_image = repmat(0,[floor((h-pix)/pix+1),floor((w-pix)/pix+1)],1) ;
for y = p : pix : floor((h-pix)/pix)*pix+p ;
for x = p : pix : floor((w-pix)/pix)*pix+p ;
c = clrs(img(y,x,1) + 1,img(y,x,2) + 1,img(y,x,3) + 1) ;
segmented_image((y-p)/pix+1,(x-p)/pix+1) = c ;
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%3  Matlab code for object recognition

%Using variance
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
varx = [] ;
vary = [] ;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Calculate the variance in x and y direction.
vx = var(segmented_image(:,:)) ;
vy = var(segmented_image(:,:)') ;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% If labeled pixels are found,
% Calculate the position of the ball.
if length(find(vx))>0 && length(find(vy))>0
s = size(segmented_image) ;
w = s(2) ;
h = s(1) ;
for i = 1 : w ;
varx(i) = (vx(i) > (min(vx(find(vx)))*15)) ;
end
for i = 1 : h ;
vary(i) = (vy(i) > (min(vy(find(vy)))*15)) ;
end
f = find(varx) ;
ff = [] ;
for i = 1 : length(f)-1 ;
if(f(i+1) > f(i)+10) ;
ff = [ff f(i) f(i+1)] ;
end
end
ff = [ff f(length(f))] ;
ff = [f(1) ff];
fx = [] ;
for i = 1 : 2 : length(ff)-1
fx = [fx ff(i+1)-ff(i)] ;
end
m = max(fx) ;
m = find(fx==m) ;
rx = ff(2*m-1) : ff(2*m) ;
xball = zeros(size(varx)) ;
xball(rx) = 1 ;
xbal = mean(rx) ;
f = find(vary) ;
ff = [] ;
for i = 1 : length(f)-1 ;
if(f(i+1) > f(i)+10) ;
ff = [ff f(i) f(i+1)] ;
end
end
ff = [ff f(length(f))] ;
ff = [f(1) ff];
fy = [] ;
for i = 1 : 2 : length(ff)-1
fy = [fy ff(i+1)-ff(i)] ;
end
m = max(fy) ;
m = find(fy==m) ;
ry = ff(2*m-1) : ff(2*m) ;
yball = zeros(size(vary)) ;
yball(ry) = 1 ;
ybal = max(ry) ;
else
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Using mean and standard deviation
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if length(yball) > 0 && length(xball) > 0
[yball,xball] = find(segmented_image(:,:)) ;
ymean = mean2(yball) ;
ystd = std2(yball) ;
yb = round(ymean - 2*ystd) - ((ymean - 2*ystd) < 1 )*((ymean - 2*ystd)+1) : 1 : round(ymean + 2*ystd) - ((ymean + 2*ystd) > h )*((ymean + 2*ystd)-h) ;
xmean = mean2(xball) ;
xstd = std2(xball) ;
xb = round(xmean - 2*xstd) - ((xmean - 2*xstd) < 1 )*((xmean - 2*xstd)+1) : 1 : round(xmean + 2*xstd) - ((xmean + 2*xstd) > w )*((xmean + 2*xstd)-w) ;
[yball,xball] = find(segmented_image(yb,xb)) ;
yball = h - (max(yball) + min(yb))*pix ;
xball = - (0.5*w - ((mean2(xball) + min(xb))*pix)) ;
else
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%```