After finding lines, how to find squares in an Image of a chessboard?

8 views (last 30 days)
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:
This is the line detection:

Answers (1)

mohammad nemat
mohammad nemat on 13 Jul 2021
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?

Categories

Find more on Convert Image Type 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!