10

我想绘制一条从一个明确定义的点到另一个点的线,然后将其转换为图像矩阵,以在其上使用高斯滤波器进行平滑处理。为此,我使用函数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));

[更新]

High-Performance 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);
4

4 回答 4

17

这是一个直接在矩阵中画线的例子。首先,我们将为空图像创建一个零矩阵:

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将它们从下标索引转换为线性索引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);
h = fspecial('gaussian', 20, 10);  % Create filter
filteredImg = imfilter(mat, h);    % Filter image
image(filteredImg);                % Show filtered line image
title('Filtered line');

在此处输入图像描述

于 2009-12-21T17:00:05.267 回答
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);

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

于 2013-01-14T13:55:22.600 回答
0

你可以在这里查看我的答案。这是实现您所要求的强大方法。我的方法的优点是它不需要额外的参数来控制画线的密度

于 2013-01-13T21:50:57.947 回答
-1

像这样的东西:

[linspace(30,200,numSteps); linspace(35,60,numSteps)]

那对你有用吗 ?

标记

于 2009-12-21T15:53:09.243 回答