matlab矩阵图像绘制,如何在MATLAB中创建⼀条在其中绘制 的线的图像矩阵?
胡佩莲以下是将线直接绘制到矩阵中的⼀个⽰例.⾸先,我们将为空的图像创建⼀个零的矩阵:
mat = zeros(250, 250, 'uint8'); % A 250-by-250 matrix of type uint8
然后,假设我们要画⼀条从(30,35)到(200,60)的线.我们⾸先计算该⾏将要有多少个像素长度: 生化危机网络版x = [30 200]; % x coordinates (running along matrix columns)
y = [35 60]; % y coordinates (running along matrix rows)
nPoints = max(abs(diff(x)), abs(diff(y)))+1; % Number of points in line
聂党权>bmw族接下来,我们使⽤linspace计算⾏像素的⾏和列索引,使⽤sub2ind将它们从下标索引转换为线性索引,然后使⽤它们来修改mat:
rIndex = round(linspace(y(1), y(2), nPoints)); % Row indices
cIndex = round(linspace(x(1), x(2), nPoints)); % Column indices
index = sub2ind(size(mat), rIndex, cIndex); % Linear indices
冠东集装箱码头mat(index) = 255; % Set the line pixels to the max value of 255 for uint8 types
subplot(1, 2, 1);
image(mat); % Show original line image
colormap(gray); % Change colormap
title('Line');
subplot(1, 2, 2);
三星sghe258h = fspecial('gaussian', 20, 10); % Create filter
filteredImg = imfilter(mat, h); % Filter image
image(filteredImg); % Show filtered line image
title('Filtered line');