views:

1309

answers:

3

Hi,

I have generated a plot like

figure; hold;
axis([0 10 0 10]);
fill([ 1 1 5 5], [5 1 1 5],'b')

and now I want to have this plot as an matrix so that I can i.e. filter the blog with a gaussian. Googleing I found this thread Rasterizing Plot to Image at Matlab Central. I tried it, but I could only get it to work for line or function plots. Do you have an idea?

Thank you

A: 

Hi

What are the desired characteristics of your target matrix ? And what sort of images do you want to rasterise ?

You see, for the only example you have given us it's almost trivial to define a matrix representing your image ...

1. figmat = ones(10,10,3) % create a 10x10 raster where each entry is a triple for RGB, setting them all to 1 colours the whole raster white
2. figmat(2:5,2:5,1:2) = 0 % sets RG components in the coloured area to 0, leaving only blue

Your matrix is a raster to start with. Now, you can use the built-in function image to visualise your matrix. Have a look at the documentation for that function. And note that my suggestion does not meet the spec for use with image() and colormap().

Regards

Mark

High Performance Mark
A: 

If you're interested in plotting or filtering blobs, you could create the matrix directly, and display it using imagesc.

In your case

%{
Set up the image : make an 11x11 array of zeros (zeros=background), since your axes limits went from 0 to 10, and a matrix can only start at element 1 
%}
m = zeros(11);
%{ 
write the blob into the image. Remember that we started at 1 instead of 0
%}
m(2:6,2:6)=1; 

% display the blob with imagesc to get the axes right
figure, imagesc(0:10,0:10,m)
% flip axes so that they're positioned like plot axes
axis xy
% use grayscale colormap so that you see better what's going on in the image
colormap gray

You can now filter m and display the result. If you'd rather have the image black on white than white on black, use 1-m instead of m in imagesc

EDIT: As an alternative way to create a matrix from a blob, you can use inpolygon, if you know the corners of your blob. In this case:

% create coordinates of your matrix
[xCoord,yCoord] = meshgrid(0:10,0:10);
xCoord = xCoord(:);yCoord = yCoord(:); % make vectors

% check whether the pixels are inside (or on) the polygon
[inPoly,onPoly] = inpolygon(xCoord,yCoord,[5,1,1,5],[1,1,5,5]);
% now m from above is either
m = inPoly;
% or, if you want to accept pixels whose center is on the polygon lines
m = inPoly | onPoly; % this is what we had above

% and reshape to get a 2d array from our vector. Then display as above
m = reshape(m,11,11);
Jonas
+1  A: 

You can use GETFRAME function. It returns movie frame structure, which is actually rasterized figure. Field cdata will contain your matrix.

F=getframe;
figure(2)
imagesc(F.cdata);
yuk

related questions