After finding lines, how to find squares in an Image of a chessboard?
2 次查看(过去 30 天)
显示 更早的评论
Hello, I'm trying to detect the squares of a chessboard and name them accordingly (H8, A6, C4 etc.). I have already found the lines in the chessboard. Here is my code:
EDIT: Do you believe that there might be a way to use the lines, so I can find the squares of the chess?
if true
clear all; clc; close all;
I = imread('chess.jpg');
th = [0 0.417]; %threshold for canny
s = sqrt(3); %sigma for canny
Ig = rgb2gray(I);
B = edge(Ig,'Canny',th,s); %Using canny in Graythresh image
%Hough Transformation
[H, theta, rho] = hough(B, 'RhoResolution',0.9,'ThetaResolution',0.09);
peaks = houghpeaks(H,19,'threshold',ceil(0.3*max(H(:)))); %wiki
%Finding Lines
lines = houghlines(B, theta, rho, peaks, 'FillGap', 350, 'MinLength', 20);
figure(2)
imshow(I)
hold on
maxLength = 0;
%Show lines
for i = 1 : length(lines)
xy = [lines(i).point1; lines(i).point2];
% LineSHow
plot( xy(:, 1), xy(:, 2), 'LineWidth', 2, 'Color', 'green' );
% Length of line
lengthOfLines(i) = norm(lines(i).point1 - lines(i).point2);
% Find angle
angles(i) = lines(i).theta;
% Check if this is the longest line
if ( lengthOfLines > maxLength )
maxLength = lengthOfLines;
xyLongest = xy;
end
end
%Below, is a failed try to find the squares, using a suggestion by another post
%about finding squares
BW = im2bw(Ig);
S = regionprops(peaks, 'BoundingBox', 'Centroid');
imshow(I); hold on;
for cnt = 1 : numel(S)
bb = S(cnt).BoundingBox;
rectangle('position',bb,'edgecolor','r','linewidth',2);
end
end
This is the image:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/168703/image.jpeg)
This is the line detection:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/168709/image.png)
回答(1 个)
mohammad nemat
2021-7-13
hi,i ran this code with this picture but i didn't get this result with detected line.
what is this problem and why i can't get this output?
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Convert Image Type 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!