Matlab 图像矩阵中的棋盘距离

Matlab 图像矩阵中的棋盘距离,matlab,image-processing,distance,Matlab,Image Processing,Distance,给定一个图像矩阵,如何获得棋盘距离像素A小于D的像素的位置。我需要对所有像素执行此操作 使用MATLAB函数bwdist我无法提供期望的结果。解决办法是什么 [D,idx] = bwdist(Img,'chessboard'); 给定图像、像素和最大距离: % Test image Image = zeros(20,30); % Maximum chessboard distance from image maxDist = 7; % The pixel from which to mea

给定一个图像矩阵,如何获得棋盘距离像素A小于D的像素的位置。我需要对所有像素执行此操作

使用MATLAB函数
bwdist
我无法提供期望的结果。解决办法是什么

[D,idx] = bwdist(Img,'chessboard');

给定图像、像素和最大距离:

% Test image
Image = zeros(20,30);

% Maximum chessboard distance from image
maxDist = 7;

% The pixel from which to measure distance
pix = [4,19];
要查找与
pix
的棋盘距离的像素 小于
maxDist
且在图像边界中:

选项1:使用
bwdist

% Create a binary image with all pixels zero except 'pix'
bw = zeros(size(Image));
bw(pix(1), pix(2)) = 1;

% Get the chessboard distance transform
[D,idx] = bwdist(bw,'chessboard');

% Get the linear index of 'pix' 
pixInd = sub2ind(size(bw), pix(1), pix(2));

% Find linear indices of pixels who's chessboard distance from pixel are 
% less than 'maxDist'
pointsInd = find(idx == pixInd & D < maxDist);

% Remove 'pix'
pointsInd(pointsInd == pixInd) = [];

% Get the pairs of (x,y) of the pixels
[pointsX, pointsY] = ind2sub(size(bw), pointsInd);
显示结果:

% Get linear indices of pixels
pointsInd = sub2ind(size(Image), pointsX, pointsY);

% To display the result, create a binary image with all found pixels 
% colored white
bwPoints = zeros(size(Image));
bwPoints(pointsInd) = 1;

% Show points
imshow(bwPoints, 'InitialMagnification', 2000)

% Show pixel grid lines
hold on
[rows, cols] = size(bwPoints);
for row = 0.5 : 1 : (rows + 0.5)
    line([0.5, cols+0.5], [row, row], 'Color', 'r', 'LineWidth', 0.5);
end
for col = 0.5 : 1 : (cols + 0.5)
    line([col, col], [0.5, rows+0.5], 'Color', 'r', 'LineWidth', 0.5);
end

效率和在所有图像像素上循环运行:

% Get linear indices of pixels
pointsInd = sub2ind(size(Image), pointsX, pointsY);

% To display the result, create a binary image with all found pixels 
% colored white
bwPoints = zeros(size(Image));
bwPoints(pointsInd) = 1;

% Show points
imshow(bwPoints, 'InitialMagnification', 2000)

% Show pixel grid lines
hold on
[rows, cols] = size(bwPoints);
for row = 0.5 : 1 : (rows + 0.5)
    line([0.5, cols+0.5], [row, row], 'Color', 'r', 'LineWidth', 0.5);
end
for col = 0.5 : 1 : (cols + 0.5)
    line([col, col], [0.5, rows+0.5], 'Color', 'r', 'LineWidth', 0.5);
end
选项2选项1快得多。我首先写了选项1,因为问题中提到了bwdist。在循环中运行选项2可以通过先计算像素,然后将其移动到每个像素的位置来改进:

% Get the range of x and y indices who's chessboard distance from pixel 
% (0,0) are less than 'maxDist'
xRange = (-(maxDist-1)):(maxDist-1);
yRange = (-(maxDist-1)):(maxDist-1);

% Create a mesgrid to get the pairs of (x,y) of the pixels
[pointsX, pointsY] = meshgrid(xRange, yRange);
pointsX = pointsX(:);
pointsY = pointsY(:);

% Remove pixel (0,0)
pixIndToRemove = (pointsX == 0 & pointsY == 0);
pointsX(pixIndToRemove) = [];
pointsY(pixIndToRemove) = [];

for x=1:size(Image, 1)
    for y=1:size(Image, 2)
        % Get a shifted copy of 'pointsX' and 'pointsY' that is centered
        % around (x, y)
        pointsX1 = pointsX + x;
        pointsY1 = pointsY + y;

        % Remove the the pixels that are out of the image bounds        
        inBounds =...
            pointsX1 >= 1 & pointsX1 <= size(Image, 1) &...
            pointsY1 >= 1 & pointsY1 <= size(Image, 2);

        pointsX1 = pointsX1(inBounds);
        pointsY1 = pointsY1(inBounds);

        % Do stuff with 'pointsX1' and 'pointsY1'
        % ...

    end
end
%获取x和y索引的范围,从像素到棋盘的距离
%(0,0)小于“maxDist”
xRange=(((maxDist-1)):(maxDist-1);
yRange=((maxDist-1)):(maxDist-1);
%创建mesgrid以获取(x,y)像素对
[pointsX,pointsY]=meshgrid(xRange,yRange);
pointsX=pointsX(:);
pointsY=pointsY(:);
%移除像素(0,0)
pixIndToRemove=(pointsX==0&pointsY==0);
点x(pixIndToRemove)=[];
pointsY(pixIndToRemove)=[];
对于x=1:大小(图1)
y=1时:尺寸(图2)
%获取居中的“pointsX”和“pointsY”的移位副本
%大约(x,y)
点sx1=点sx+x;
点SY1=点SY+y;
%删除超出图像边界的像素
内边界=。。。
pointsX1>=1&pointsX1=1&pointsY1
“目标是访问棋盘距离像素A小于D的像素的位置。该过程应
对所有像素执行…”

由于D正在创建一个正方形选择区域,因此只需使用简单的数学即可

例如:如果D为3,则从像素A的[x,y]位置

//# we minus D by 1 since you want less than D (not equal / higher)

Start-X = pixelA.x - (D-1); //from the left
End-X = pixelA.y + (D-1); //to the right

Start-Y = pixelA.y - (D-1); //from the top
End-Y = pixelA.y + (D-1); //to the bottom
这将为您提供一个方形周长,表示您所需的选择区域

请看下面的示例图像:
每个正方形是一个像素。如果“皇冠”图标表示像素AD为3(其中“小于D”表示D的最大长度为2像素),您可以看到上面的伪代码是如何应用的吗


感谢您提供的有效解决方案。看起来还可以,但有一个小问题:目标像素(pix)的位置包含在[pointsX,pointsY]对中,但不应该是这样。@dtr43:我编辑了答案并删除了目标像素。它可以工作,但在示例情况下(对于434*700像素的图像)为了计算所有像素的棋盘距离,选项1(您建议的解决方案)在两个for循环中使用。此外,还有另一个内部循环,用于计算棋盘距离内目标像素和周围像素之间的欧氏距离。此过程非常耗时且效率低下。有什么诀窍可以提高效率吗?@dtr43:Option 2更快。我编辑了答案并添加了一个部分:效率和在所有图像像素上循环运行感谢这个建议;我担心它是否能计算对角线邻域。如果我没有误解你的评论<代码>(1)
向左/向右移动一个像素,同时向上/向下移动一个像素以实现对角线移动<代码>(2)重复步骤(1),直到覆盖所有对角线像素。