如何在MATLAB中创建一个在其中绘制线条的图像矩阵? [英] How do I create an image matrix with a line drawn in it in MATLAB?

查看:160
本文介绍了如何在MATLAB中创建一个在其中绘制线条的图像矩阵?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



我的问题是:




  • 是否可以替换 getframe 可以使用吗?

  • 有没有办法创建图像矩阵并在其中直接画线?



这是一个最小代码示例:

  figure1 = line([30 35],[200 60]); 
F = getframe;
hsize = 40;西格玛= 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)]);
矩阵(对角线(1,:),对角线(2,:))= 1;
imshow(矩阵);


解决方案

以下是直接在矩阵中绘制线条的一个示例。首先,我们将为空图像创建一个零矩阵:

  mat = zeros(250,250,'uint8' ); %一个250×250的uint8矩阵

然后,假设我们要绘制一条线从(30,35)(200,60)。我们首先计算该行必须有多少像素:

  x = [30 200]; %x坐标(沿矩阵列运行)
y = [35 60]; %y坐标(沿矩阵行运行)
nPoints = max(abs(diff(x)),abs(diff(y)))+ 1; %行中的点数

接下来,我们使用


I want to plot a line from one well-defined point to another and then turn it into an image matrix to use a Gaussian filter on it for smoothing. For this I use the functions line and getframe to plot a line and capture the figure window in an image, but getframe is very slow and not very reliable. I noticed that it does not capture anything when the computer is locked and I got an out of memory error after 170 executions.

My questions are:

  • Is there a substitute to getframe that I can use?
  • Is there a way to create the image matrix and draw the line directly in it?

Here is a minimal code sample:

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));

[update]

High-Performance Mark's idea with linspace looks very promising, but how do I access the matrix coordinates calculated with linspace? I tried the following code, but it does not work as I think it should. I assume it is a very simple and basic MATLAB thing, but I just can not wrap my head around it:

matrix=zeros(200,60);
diagonal=round([linspace(30,200,numSteps); linspace(35,60,numSteps)]);
matrix(diagonal(1,:), diagonal(2,:))=1;
imshow(matrix);

解决方案

Here's one example of drawing a line directly into a matrix. First, we'll create a matrix of zeros for an empty image:

mat = zeros(250, 250, 'uint8');  % A 250-by-250 matrix of type uint8

Then, let's say we want to draw a line running from (30, 35) to (200, 60). We'll first compute how many pixels long the line will have to be:

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

Next, we compute row and column indices for the line pixels using linspace, convert them from subscripted indices to linear indices using sub2ind, then use them to modify 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

You can then visualize the line and the filtered version with the following:

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');

这篇关于如何在MATLAB中创建一个在其中绘制线条的图像矩阵?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆