How I can detect the rectangle in this image? It has shadow and glare, how to eliminate them?
2 views (last 30 days)
Show older comments
This is my code and my picture.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/189581/image.png)
close all;
vid =videoinput('winvideo','Logitech HD Webcam C310');
set(vid, 'ReturnedColorSpace', 'RGB');
hImage=image(zeros(480,640,3));
preview(vid,hImage);
anh=getsnapshot(vid);
% Step 1: Read image Read in
figure,
imshow(anh),
title('Original Image');
% Step 2: Convert image from rgb to gray
%GRAY1 = imtophat(anh,strel('disk',3));
GRAY = rgb2gray(anh);
%mask = GRAY > 165; % whatever value works.
%fixedImage = regionfill(GRAY, mask);
GRAY = histeq(GRAY);
%GRAY = histeq(GRAY);
%GRAY = imadjust(GRAY);
figure,
imshow(GRAY),
title('Gray Image');
% Step 3: Threshold the image Convert the image to black and white in order
% to prepare for boundary tracing using bwboundaries.
threshold = graythresh(GRAY);
BW = im2bw(GRAY, threshold);
figure,
imshow(BW),
title('Binary Image');
%Step 3': remove all object containing fewer than 30 pixels
BW = bwareaopen(BW,110);
figure,
imshow(BW),
title('Binary Image remove small pixels');
% Step 4: Invert the Binary Image
BW = ~ BW;
figure,
imshow(BW),
title('Inverted Binary Image');
%Step 4': remove all object containing fewer than 30 pixels
% remove all object containing fewer than 30 pixels
BW = bwareaopen(BW,110);
figure,
imshow(BW),
title('Binary Image remove small pixels');
% Step 5: Find the boundaries Concentrate only on the exterior boundaries.
% Option 'noholes' will accelerate the processing by preventing
% bwboundaries from searching for inner contours.
BW = ~BW;
figure,
imshow(BW),
[B,L] = bwboundaries(BW, 'noholes');
%STATS = regionprops(L, 'all');
stats = regionprops(L,'all');
figure,
imshow(anh),
u = 1;
% loop over the boundaries
for k = 1:length(B)
% obtain (X,Y) boundary coordinates corresponding to label 'k'
boundary = B{k};
% Loc cac khoi co ban
area(k) = stats(k).Area;
areaboundingbox(k) = stats(k).ConvexArea;
%Loai khoi tron
delta_sq = diff(boundary).^2;
perimeter(k) = sum(sqrt(sum(delta_sq,2)));
metric(k) = 4*pi*area(k)/(perimeter(k))^2; % Loai khoi tron
hold on;
W(k) = uint8( ((area(k)/areaboundingbox(k)) >= 0.85) ); %Loc khoi co ban
switch W(k)
case 0
% do nothing
case 1
if metric(k) < 0.91
plot((stats(k).ConvexHull(:,1)),(stats(k).ConvexHull(:,2)),'r--');
plot((stats(k).Centroid(1)),(stats(k).Centroid(2)),'ro');
xc(u) = stats(k).Centroid(1);
yc(u) = stats(k).Centroid(2);
gocquay(u) = stats(k).Orientation;
chieurong(u) = stats(k).MajorAxisLength*0.85; %Chieu rong elip dai hon chieu rong hcn nen them he so giam chieu dai
chieucao(u) = stats(k).MinorAxisLength;
text(stats(k).Centroid(1)-20,stats(k).Centroid(2),num2str(u),'Color','b',...
'FontSize',14,'FontWeight','bold');
u=u+1;
end
end
end
sohcn = u-1;
delete(vid);
delete(hImage);
0 Comments
Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!