Labfans是一个针对大学生、工程师和科研工作者的技术社区。 论坛首页 | 联系我们(Contact Us)
MATLAB爱好者论坛-LabFans.com
返回   MATLAB爱好者论坛-LabFans.com > 其它 > 资料存档
资料存档 资料存档
 
 
主题工具 显示模式
旧 2019-12-10, 16:49   #1
poster
高级会员
 
注册日期: 2019-11-21
帖子: 3,006
声望力: 66
poster 正向着好的方向发展
帖子 如何在MATLAB中用画线创建图像矩阵?

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



更多&回答...
poster 当前离线   回复时引用此帖
 


发帖规则
不可以发表新主题
不可以发表回复
不可以上传附件
不可以编辑自己的帖子

启用 BB 代码
论坛禁用 表情符号
论坛启用 [IMG] 代码
论坛启用 HTML 代码



所有时间均为北京时间。现在的时间是 06:55


Powered by vBulletin
版权所有 ©2000 - 2025,Jelsoft Enterprises Ltd.