简体   繁体   中英

How to convert plot data to image matrix (containing indices of colormap) in matlab

I'm experimenting with sparse matrix for which I need some sparse matrix data containing some patterns. The best that can be easily generated are image data eg-

a = 64 * ones (9,1);
b = [64 64 64 0 0 0 64 64 64];
c = [64 64 64 0 64 0 64 64 64];
b = b';
c = c';
M = [a a a b c c  b a a a];

Genrates simple rectangular pattern. What I want is complex pattens like some curves (x^2 or sinx) in matrix (image data) form. Is it possible to convert them into image data???

One option you have is to use Matlab's plotting capabilities to make the picture you want, then to save it into some graphics format such as TIFF. Next load the image file (probably using imread), which gives you an array of pixels. This won't be a sparse array but a full one. I expect you can figure out how to make it sparse, perhaps negate the colours so that white becomes black (and pixels with values (255,255,255) become (0,0,0)) and then use sparse.

You can build it all within matlab, you just have to map the function value to a particular point in your image. Example:

imsize = [250,600]; %sets dimensions of your image
buff = 30; %vertical buffer--must be integer >=1 to avoid indexing error
dm = [-pi,pi]; %sets the domain
stepx = (dm(2) - dm(1))/imsize(2);
f = @(x) sin(x); %whatever function you want

y=f(dm(1):stepx:dm(2)-stepx);
stepy = (max(y)-min(y))/(imsize(1)-2*buff);
j=round(imsize(1)/2)-(round(y/stepy));
graph = full(sparse(j,1:imsize(2),1,imsize(1),imsize(2)));
imshow(im2bw(1-graph,.5));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM