how can i detect the checkerboard pieces in an image?
8 views (last 30 days)
Show older comments
Ahmad toufaily
on 27 Oct 2015
Answered: Image Analyst
on 28 Oct 2015
hey guys! i am working on my final project which is a checkers playing robot(actually turkish checker). i was able to detect the checkerboard using hough transform and canny edge detector now i would like to find out what board cells may hold pieces and which may not. i think about xoring the "no pixels on" image with the image with the checker pieces on it to find out which squares have pieces on them, but i don't know how to do it .may someone help me please :) you may find my code and the images
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/151069/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/151071/image.png)
if true
I = imread('FrontTop2.png');
BW = edge(rgb2gray(I),'canny');
%http://www.mathworks.com/help/images/edge-detection.html#buh9ylp-13
[H, theta, rho] = hough(BW);
figure; image(theta,rho,imadjust(mat2gray(H)),'CDataMapping','scaled');
hold on; colormap(gray(256));
%plot(theta(P(:,2)),rho(P(:,1)),'o','color','r');
%http://www.mathworks.com/help/images/ref/hough.html
%find peaks
numpeaks = 19;
thresh = ceil(0.1 * max(H(:)));
P = houghpeaks(H,numpeaks,'threshold',thresh);
% Extract image lines
lines = houghlines(BW,theta,rho,P,'FillGap',50,'MinLength',60);
figure; imshow(I);
figure; imshow(BW);
figure; imshow(I); hold on; n = size(I,2);
for k = 1:length(lines)
% Overlay kth line
x = [lines(k).point1(1) lines(k).point2(1)];
y = [lines(k).point1(2) lines(k).point2(2)];
line = @(z) ((y(2) - y(1)) / (x(2) - x(1))) * (z - x(1)) + y(1);
plot([1 n],line([1 n]),'Color','r');
end
%# shearing transformation slopes = vertcat(lines.point2) - vertcat(lines.point1); slopes = slopes(:,2) ./ slopes(:,1); TFORM = maketform('affine', [1 -slopes(1) 0 ; 0 1 0 ; 0 0 1]); II = imtransform(I, TFORM);
%# show image with lines overlayed, and the aligned/rotated image figure subplot(121), imshow(I), hold on for k = 1:length(lines) xy = [lines(k).point1; lines(k).point2]; plot(xy(:,1), xy(:,2), 'g.-', 'LineWidth',2); end, hold off subplot(122), imshow(II);
end
0 Comments
Accepted Answer
Image Analyst
on 28 Oct 2015
Just convert both to gray level and subtract
grayImage = rgb2gray(thisImage);
refImage = rgb2gray(refImage);
diffImage = abs(double(grayImage)-double(refImage));
binaryImage = diffImage > someThreshold;
% Fill holes.
binaryImage = imfill(binaryImage, 'holes');
% Get rid of small noise specks.
binaryImage = bwareaopen(binaryImage, 1000);
0 Comments
More Answers (0)
See Also
Categories
Find more on Image Processing and Computer Vision 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!