如何在MATLAB中使用在其中绘制的线创建图像矩阵?

Fra*_*ter 9 matlab plot image-processing matrix line

我想绘制一条从一个明确定义的点到另一个定义点的线,然后将其转换为图像矩阵,在其上使用高斯滤波器进行平滑.为此,我使用函数linegetframe绘制一条线并捕获图像中的图形窗口,但getframe速度非常慢且不太可靠.我注意到它在计算机锁定时没有捕获任何内容,并且out of memory在170次执行后我收到错误.

我的问题是:

  • 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));
Run Code Online (Sandbox Code Playgroud)

[更新]

高性能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);
Run Code Online (Sandbox Code Playgroud)

gno*_*ice 16

这是将直线绘制成矩阵的一个示例.首先,我们将为空图像创建一个零矩阵:

mat = zeros(250, 250, 'uint8');  % A 250-by-250 matrix of type uint8
Run Code Online (Sandbox Code Playgroud)

那么,让我们说,我们要吸取运行线(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
Run Code Online (Sandbox Code Playgroud)

接下来,我们计算行像素的行和列索引,使用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
Run Code Online (Sandbox Code Playgroud)

然后,您可以使用以下内容可视化该行和已过滤的版本:

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');
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述


Nav*_*van 5

如果您有计算机视觉系统工具箱,则可以使用 ShapeInserter 对象。这可用于在图像上绘制线条、圆形、矩形和多边形。

mat = zeros(250,250,'uint8');
shapeInserter = vision.ShapeInserter('Shape', 'Lines', 'BorderColor', 'White');
y = step(shapeInserter, mat, int32([30 60 180 210]));
imshow(y);
Run Code Online (Sandbox Code Playgroud)

http://www.mathworks.com/help/vision/ref/vision.shapeinserterclass.html