我想自动化将棋盘图像的方块分类为黑色或白色方块的过程。进一步的步骤是区分它是否为空方格或方块是否包含一块。到目前为止,我接近使用平方中心的平均强度将每个方块分类为白色或黑色,但设置阈值很困难。对于更进一步的步骤(空方格或一块),我尝试std2
,但也很难。
这是我的剧本:
image = imerode(original,strel('disk',3));
image = imadjust(image,[],[],2);
figure,imshow(image),hold on;
for i = 1:length(cells)
TL = cells(i).TL; %Cell's corner top left
TR = cells(i).TR; %Cell's corner top right
BL = cells(i).BL; %Cell's corner bottom left
BR = cells(i).BR; %Cell's corner bottom right
x = [TL(1) TR(1) BR(1) BL(1)];
y = [TL(2) TR(2) BR(2) BL(2)];
bw = poly2mask(x,y,size(image,1),size(image,2));
measurements = regionprops(bw,'BoundingBox');
cropped = imcrop(image, measurements.BoundingBox);
m = regionprops(bw,'Centroid');
x = m.Centroid(1);
y = m.Centroid(2);
w = 25; %width
h = 25; %height
tl = [round(x-w/2) round(y-h/2)];
center = image(tl(1):tl(1)+w,tl(2):tl(2)+h,:);
%stds(i) = std2(center);
avgs(i) = mean2(center);
if(avgs(i) > 55)
str = "W";
else
str = "B";
end
text(x,y,str,'Color','red','FontSize',16);
end
编辑:以下图片是
之后的新结果image = imerode(image,strel('disk',4));
image = image>160;
答案 0 :(得分:2)
您可以在棋盘方法detectCheckerboardPoints
和checkerboard
中使用Matlabs构建来查找棋盘格的大小并构建一个合适大小的棋盘格。由于只能存在两个可能的棋盘格,因此构造两个并检查哪一个与最佳匹配。
img = imread('PNWSv.jpg'); %Load image
%Prepare image
I = rgb2gray(img);
I2 = imerode(I,strel('square',10));
bw = imbinarize(I2,'adaptive');
%Find checkerboard points
[imagePoints,boardSize] = detectCheckerboardPoints(bw);
%Find the size of the checkerboard fields
x = boardSize(2)-1;
y = boardSize(1)-1;
fields = cell(y,x);
for k = 1:length(imagePoints)
[i,j] = ind2sub([y,x],k);
fields{i,j} = imagePoints(k,:);
end
avgDx = mean(mean(diff(cellfun(@(x) x(1),fields),1,2)));
avgDy = mean(mean(diff(cellfun(@(x) x(2),fields),1,1)));
%Construct the two possibilities
ref1 = imresize(imbinarize(checkerboard),[avgDx,avgDy]*8);
ref2 = imcomplement(ref1);
%Check which ones fits the better
c1 = normxcorr2(ref1,I);
c2 = normxcorr2(ref2,I);
if max(c2(:))<max(c1(:))
ref=ref1;
c = c1;
else
ref=ref2;
c = c2;
end
%Plot the checkerboard bounding box on top of the original image
[ypeak, xpeak] = find(c==max(c(:)));
yoffSet = ypeak-size(ref,1);
xoffSet = xpeak-size(ref,2);
imshow(img);
imrect(gca, [xoffSet+1, yoffSet+1, size(ref1,2), size(ref1,1)]);
答案 1 :(得分:1)