Detecting color and shape from an image
Mostra commenti meno recenti
could any one tell me what's the wrong in this code, why it's not detecting the circle in the image accurately, also why the color is not detecting?
clc
clear all
close all
IM = imread('Circle.JPG');
IM = imresize(IM,[500,500]);
IM_BW = im2bw(IM);
IM_BW = bwareaopen(IM_BW,50);
[y,x] = find(IM_BW~=1); % Find indices and values of nonzero elements
c = mean([x y]); % Average or mean value of array
R = max(sqrt((c(1)-x).^2+(c(2)-y).^2 ));
th = linspace(0,2*pi); % Generate linearly spaced vector
xc = c(1)+R*cos(th);
xc = xc';
yc = c(2)+R*sin(th);
%%Plotting Circle
imshow(IM_BW);
hold on
plot(xc,yc,'r')
hold off
%%Detecting color
if IM(xc, yc, 1) > IM(xc, yc, 2) && IM(xc, yc, 1) > IM(xc, yc, 3)
disp('Red!')
elseif IM(xc, yc, 2) > IM(xc, yc, 3)
disp('Green!')
else
disp('Blue!')
end
1 Commento
Immanuel Koshy
il 13 Lug 2022
Modificato: Immanuel Koshy
il 13 Lug 2022
did u find the issue ?
Risposte (1)
KSSV
il 27 Feb 2018
How about this approach?
IM = imread('Circle.JPG');
IM = imresize(IM,[500,500]);
IM_BW = im2bw(IM);
IM_BW = bwareaopen(IM_BW,50);
[y,x] = find(IM_BW~=1); % Find indices and values of nonzero elements
%%Get Bounding box
x0 = min(x) ; x1 = max(x) ;
y0 = min(y) ; y1 = max(y) ;
% length and breadth
L = abs(x1-x0) ;
B = abs(y1-y0) ;
% center bounding box
C = [x0+B/2 y0+L/2] ;
%%Get distances of the points from center of bounding box
data = repmat(C,[length(x),1])-[x,y] ;
dist = sqrt(data(:,1).^2+data(:,2).^2);
%%Classify the points into circles
nbins = 4 ; % number of circles you want
[N,edges,bin] = histcounts(dist,nbins) ;
% plot the classified circle points for check
figure(1)
imshow(IM)
hold on
for i = 1:nbins
plot((x(bin==i)),y(bin==i),'.','color',rand(1,3)) ;
end
%%Circle's radii and center
Circ = cell(nbins,1) ;
for i = 1:nbins
[xc1,yc1,R] = circfit(x(bin==i),y(bin==i)) ;
Circ{i} = [xc1 yc1 R] ;
end
figure(2)
imshow(IM)
hold on
th = linspace(0,2*pi) ;
for i = 1:nbins
xc = Circ{i}(1)+Circ{i}(3)*cos(th) ;
yc = Circ{i}(2)+Circ{i}(3)*sin(th) ;
plot(xc,yc,'color',rand(1,3),'linewidth',3) ;
end
1 Commento
odai kiwan
il 27 Feb 2018
Categorie
Scopri di più su Image Processing Toolbox in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!