views:

902

answers:

3

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

Fill in the matrix by hand, avoid dealing with graphics.

http://en.wikipedia.org/wiki/Line%5Fdrawing%5Falgorithm http://www.cs.unc.edu/~mcmillan/comp136/Lecture6/Lines.html

Matlab does not garbage-collect memory (to my knowing), so perhaps reuse the same matrix, but set it to all zeros when done?

Hamish Grubijan
A: 

Hi

Something like this:

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

Does that work for you ?

Mark

High Performance Mark
Linspace looks very promising, but how do I access the matrix' coordinates calculated with linspace? I tried the last 30mins, but the following code does not work, as I think it should.. (scnr) I assume, it 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);
DerKaiser
+4  A: 

Here's one example of drawing a line directly into a matrix. First, I'll create a matrix of zeros:

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

Then, let's say I want to draw the line y = 30 + x with x spanning from 30 to 180, which should create a line running from the top left corner to the lower right corner of the matrix:

x = linspace(30,180,1000);  %# x values at a higher resolution
y = 30+x;                   %# corresponding y values

The values in these vectors are created with a higher resolution (instead of being just integers) to ensure that the pixels end up being filled appropriately between the points for steeply sloping lines. Next, these values are rounded off and converted from subscripted indices to linear indices using SUB2IND, then used to modify mat:

index = sub2ind(size(mat),round(y),round(x));  %# Indices of the line
mat(index) = 255;  %# Set the values to the max value of 255 for uint8

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

subplot(221);
image(mat);                      %# Show original line image
colormap(gray);                  %# Change colormap
title('Line');
subplot(222);
h = fspecial('gaussian',20,10);  %# Create filter
filteredImg = imfilter(mat,h);   %# Filter image
image(filteredImg);              %# Show filtered line image
title('Filtered line');

alt text

gnovice
Thanks gnovice for your comprehensive answer!
DerKaiser