登录论坛

查看完整版本 : 如何在MATLAB中用画线创建图像矩阵?


poster
2019-12-10, 16:49
我想绘制一条从一个明确定义的点到另一点的线,然后将其转换为图像矩阵,以在其上使用高斯滤波器进行平滑。为此,我使用函数line和getframe绘制一条线并捕获图像中的图形窗口,但是getframe速度很慢且不太可靠。我注意到当计算机锁定时它无法捕获任何东西,执行170次后out of memory不足错误。

我的问题是:


我可以使用getframe的替代品吗?
有没有一种方法可以创建图像矩阵并直接在其中绘制线条?
这是一个最小的代码示例:

figure1=line([30 35] ,[200 60]); F= getframe; hsize=40; sigma=20; h = fspecial('gaussian',hsize,sigma); filteredImg = imfilter(double(F.cdata), h,256); imshow(uint8(filteredImg)); [更新]

高性能Mark关于linspace的想法看起来很有前途,但是我如何访问用linspace计算的矩阵坐标呢?我尝试了以下代码,但是它不起作用,正如我认为的那样。我认为这是一个非常简单且基本的MATLAB内容,但是我无法绕开它:

matrix=zeros(200,60); diagonal=round([linspace(30,200,numSteps); linspace(35,60,numSteps)]); matrix(diagonal(1,:), diagonal(2,:))=1; imshow(matrix);
回答:
这是将一条线直接绘制到矩阵中的一个示例。首先,我们将为空图像创建一个零矩阵:

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 接下来,我们使用linspace (https://www.mathworks.com/help/matlab/ref/linspace.html)计算线像素的行索引和列索引,使用sub2ind (https://www.mathworks.com/help/matlab/ref/sub2ind.html)将它们从下标索引转换为线性索引,然后使用它们修改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); h = fspecial('gaussian', 20, 10); % Create filter filteredImg = imfilter(mat, h); % Filter image image(filteredImg); % Show filtered line image title('Filtered line'); https://i.stack.imgur.com/UoSTG.png (https://i.stack.imgur.com/UoSTG.png)



更多&回答... (https://stackoverflow.com/questions/1940833)