gpt4 book ai didi

matlab - 如何区分黑棋盘格和白棋盘格

转载 作者:太空宇宙 更新时间:2023-11-03 20:27:28 25 4
gpt4 key购买 nike

我想自动化将棋盘图像的方 block 分类为黑色或白色方 block 的过程。进一步的步骤是区分它是一个空方 block 还是包含一个方 block 。到目前为止,我接近于使用正方形中心的平均强度将每个正方形分类为白色或黑色,但很难设置阈值。对于更进一步的步骤(空方 block 或一 block ),我尝试使用 std2 但也很困难。

这是我的原始图像,以及到目前为止我接近的图像 enter image description here enter image description here

这是我的脚本:

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;

enter image description here

最佳答案

您可以使用 Matlabs 内置的棋盘方法 detectCheckerboardPointscheckerboard 来查找棋盘的大小并构建一个合适大小的新棋盘。由于只能存在两种可能的棋盘,因此构建两者并检查哪一个匹配最好。

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)]);

关于matlab - 如何区分黑棋盘格和白棋盘格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48863940/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com