Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
rasterizing plot to image

Subject: rasterizing plot to image

From: Siyi Deng

Date: 24 Jul, 2009 17:43:28

Message: 1 of 16

Hi all,

I have 1-d continuous data y = fun(x) and I can easily plot it as plot
(x,y)

Now I want to convert the relationship into an image so that I can use
image(im). im may have lower resolution than the original plot.

I know this shall involve arbitary rounding issues.

I used interp1 to project the x and y onto the natural number grids.
But the resulting image is discontinuous when the slope is large.
Could somebody please help? Thanks!

Subject: rasterizing plot to image

From: Oliver Woodford

Date: 24 Jul, 2009 18:15:19

Message: 2 of 16

Either increase your sampling resolution, or linearly interpolate color between samples by using pcolor.

Siyi Deng <mr.siyi.deng@gmail.com> wrote in message <fa49457c-c8e8-4ba1-816b-2c53024848f5@b25g2000prb.googlegroups.com>...
> Hi all,
>
> I have 1-d continuous data y = fun(x) and I can easily plot it as plot
> (x,y)
>
> Now I want to convert the relationship into an image so that I can use
> image(im). im may have lower resolution than the original plot.
>
> I know this shall involve arbitary rounding issues.
>
> I used interp1 to project the x and y onto the natural number grids.
> But the resulting image is discontinuous when the slope is large.
> Could somebody please help? Thanks!

Subject: rasterizing plot to image

From: Siyi Deng

Date: 24 Jul, 2009 18:28:03

Message: 3 of 16

On Jul 24, 11:15 am, "Oliver Woodford" <o.j.woodford...@cantab.net>
wrote:
> Either increase your sampling resolution, or linearly interpolate color between samples by using pcolor.


Thanks, I'll play around with pcolor.

However, increasing the sampling resolution may work but that will
result in a huge image matrix. Also it does not automatically
guarantee continuous curves.

Subject: rasterizing plot to image

From: us

Date: 24 Jul, 2009 18:30:19

Message: 4 of 16

Siyi Deng <mr.siyi.deng@gmail.com> wrote in message <fa49457c-c8e8-4ba1-816b-2c53024848f5@b25g2000prb.googlegroups.com>...
> Hi all,
>
> I have 1-d continuous data y = fun(x) and I can easily plot it as plot
> (x,y)
>
> Now I want to convert the relationship into an image so that I can use
> image(im). im may have lower resolution than the original plot.
>
> I know this shall involve arbitary rounding issues.
>
> I used interp1 to project the x and y onto the natural number grids.
> But the resulting image is discontinuous when the slope is large.
> Could somebody please help? Thanks!

can you show a few lines of code...

us

Subject: rasterizing plot to image

From: Siyi Deng

Date: 24 Jul, 2009 18:52:49

Message: 5 of 16


> can you show a few lines of code...
>
> us


Hey there, here's the code:

% original data for the plot;
x = 1:1000;
y = sin(2*pi*0.005*x);
figure; plot(x,y)


% rasterize it to an 50 row x 100 col image;
nRow = 50;
nCol = 100;
xNode = linspace(min(x),max(x),nCol);
yNode = linspace(min(y),max(y),nRow);
yInterp = interp1(x,y,xNode,'cubic','extrap');

xi = 1:nCol;
yi = round(interp1(yNode,1:nRow,yInterp,'linear','extrap'));

im = accumarray([yi(:), xi(:)],1,[nRow,nCol]);

imagesc(im);

as you can see, the resulting im is not continuous. Any suggestion of
improvement? thanks!

Subject: rasterizing plot to image

From: us

Date: 24 Jul, 2009 19:53:01

Message: 6 of 16

Siyi Deng <mr.siyi.deng@gmail.com> wrote in message <28889311-c79a-43f5-9594-a3d3a6225e9f@p10g2000prm.googlegroups.com>...
>
> > can you show a few lines of code...
> >
> > us
>
>
> Hey there, here's the code:
>
> % original data for the plot;
> x = 1:1000;
> y = sin(2*pi*0.005*x);
> figure; plot(x,y)
>
>
> % rasterize it to an 50 row x 100 col image;
> nRow = 50;
> nCol = 100;
> xNode = linspace(min(x),max(x),nCol);
> yNode = linspace(min(y),max(y),nRow);
> yInterp = interp1(x,y,xNode,'cubic','extrap');
>
> xi = 1:nCol;
> yi = round(interp1(yNode,1:nRow,yInterp,'linear','extrap'));
>
> im = accumarray([yi(:), xi(:)],1,[nRow,nCol]);
>
> imagesc(im);
>
> as you can see, the resulting im is not continuous. Any suggestion of
> improvement? thanks!

one of the possible solutions

% the data
     x=1:1000;
     y=sin(2*pi*0.005*x);
     nr=50;
     nc=100;
% the engine
     xx=round(linspace(min(x),max(x),nc));
     yy=linspace(min(y),max(y),nr);
     [nx,nx]=histc(x,xx);
     [ny,ny]=histc(y,yy);
     ac=accumarray([ny.',nx.'],1,[nr,nc])~=0;
% the result
     imagesc(ac);
     axis image;
     colormap(gray(2));
     set(gca,...
          'xtick',[],...
          'ytick',[]);

us

Subject: rasterizing plot to image

From: Siyi Deng

Date: 24 Jul, 2009 20:08:22

Message: 7 of 16

On Jul 24, 12:53 pm, "us " <u...@neurol.unizh.ch> wrote:
> Siyi Deng <mr.siyi.d...@gmail.com> wrote in message <28889311-c79a-43f5-9594-a3d3a6225...@p10g2000prm.googlegroups.com>...
>
> > > can you show a few lines of code...
>
> > > us
>
> > Hey there, here's the code:
>
> > % original data for the plot;
> > x = 1:1000;
> > y = sin(2*pi*0.005*x);
> > figure; plot(x,y)
>
> > % rasterize it to an 50 row x 100 col image;
> > nRow = 50;
> > nCol = 100;
> > xNode = linspace(min(x),max(x),nCol);
> > yNode = linspace(min(y),max(y),nRow);
> > yInterp = interp1(x,y,xNode,'cubic','extrap');
>
> > xi = 1:nCol;
> > yi = round(interp1(yNode,1:nRow,yInterp,'linear','extrap'));
>
> > im = accumarray([yi(:), xi(:)],1,[nRow,nCol]);
>
> > imagesc(im);
>
> > as you can see, the resulting im is not continuous. Any suggestion of
> > improvement? thanks!
>
> one of the possible solutions
>
> % the data
>      x=1:1000;
>      y=sin(2*pi*0.005*x);
>      nr=50;
>      nc=100;
> % the engine
>      xx=round(linspace(min(x),max(x),nc));
>      yy=linspace(min(y),max(y),nr);
>      [nx,nx]=histc(x,xx);
>      [ny,ny]=histc(y,yy);
>      ac=accumarray([ny.',nx.'],1,[nr,nc])~=0;
> % the result
>      imagesc(ac);
>      axis image;
>      colormap(gray(2));
>      set(gca,...
>           'xtick',[],...
>           'ytick',[]);
>
> us- Hide quoted text -
>
> - Show quoted text -

That's really awesome, thank you us!

Subject: rasterizing plot to image

From: us

Date: 24 Jul, 2009 20:16:02

Message: 8 of 16

Siyi Deng
> That's really awesome, thank you us!

well, a partial ...thank you... that must be...

% replace
     nr=50;
% with
     nr=100;
% and you realize that we've not yet done the job completely...
% the next step shall be much more tedious...

us

Subject: rasterizing plot to image

From: Siyi Deng

Date: 24 Jul, 2009 20:24:43

Message: 9 of 16

On Jul 24, 1:16 pm, "us " <u...@neurol.unizh.ch> wrote:
> Siyi Deng
>
> > That's really awesome, thank you us!
>
> well, a partial ...thank you... that must be...
>

lol yeah I just figured that out. I think ideally I want all
connections look like

1 0
0 1

or

1 1
0 0

but not

1 1
0 1

Subject: rasterizing plot to image

From: Babak

Date: 5 Sep, 2010 15:13:05

Message: 10 of 16

How about if you have a circle and want to rasterize it?

t = linspace(0,2*pi,40);
x = cos(t);
y = sin(t);

thanks,

Subject: rasterizing plot to image

From: ImageAnalyst

Date: 5 Sep, 2010 15:31:01

Message: 11 of 16

Solid circle (disc) or just the perimeter?

Subject: rasterizing plot to image

From: Babak

Date: 5 Sep, 2010 16:00:21

Message: 12 of 16

ImageAnalyst <imageanalyst@mailinator.com> wrote in message <50e92deb-1de0-444e-9101-e2c075f66164@d8g2000yqf.googlegroups.com>...
> Solid circle (disc) or just the perimeter?

just the perimeter.

Subject: rasterizing plot to image

From: ImageAnalyst

Date: 5 Sep, 2010 16:36:10

Message: 13 of 16

On Sep 5, 12:00 pm, "Babak " <b...@jhu.edu> wrote:
> ImageAnalyst <imageanal...@mailinator.com> wrote in message <50e92deb-1de0-444e-9101-e2c075f66...@d8g2000yqf.googlegroups.com>...
> > Solid circle (disc) or just the perimeter?
>
> just the perimeter.

--------------------------------------------------------
How about this:

clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
imtool close all; % Close all imtool figures.
clear; % Erase all existing variables.
workspace; % Make sure the workspace panel is showing.
fontSize = 14;

% Change the current folder to the folder of this m-file.
% (The line of code below is from Brett Shoelson of The Mathworks.)
if(~isdeployed)
cd(fileparts(which(mfilename)));
end

% Make a big image
imageArray = zeros(400, 400, 'uint8');
[rows columns] = size(imageArray);
subplot(1,2,1);
imshow(imageArray, []);

% Get a few points along the circle.
% Get enough so that we'll hit virtually every pixel around the
perimeter.
t = linspace(0,2*pi, 3*rows);
x = cos(t);
y= sin(t);

% Scale and shift the x and y - they need to be bigger than 1 element.
xp = round(150 * x + rows/2);
yp = round(150 * y + columns/2);
hold on;
plot(xp, yp);
title('Original Image with circle in the overlay', 'FontSize',
fontSize);

% Get linear indices.
linearIndices = sub2ind(size(imageArray), xp, yp);

% burn them into the image.
imageArray(linearIndices) = 255;

subplot(1,2,2);
imshow(imageArray, []);
title('Image with circle burned into image', 'FontSize', fontSize);
set(gcf, 'Position', get(0,'Screensize')); % Enlarge figure to full
screen.
set(gcf,'name','Demo by ImageAnalyst','numbertitle','off')

Subject: rasterizing plot to image

From: Babak

Date: 6 Sep, 2010 14:40:09

Message: 14 of 16

Thanks a lot!

Subject: rasterizing plot to image

From: Yihwa

Date: 28 Mar, 2011 06:06:04

Message: 15 of 16

To get rid of some empty points in the center you could use imdilate from Image Processing Toolbox.

ac2 = imclose(ac, [1 1 1 ]')
figure;
imagesc(ac2);

This fills up some vertical gaps.
Unfortunately does not fill all the gaps that exist though.

Hope this helps

Best wishes

Kim


"Siyi Deng" wrote in message <a204032f-a237-464a-b7cf-2e9aa07ddb52@d4g2000prc.googlegroups.com>...
> On Jul 24, 12:53 pm, "us " <u...@neurol.unizh.ch> wrote:
> > Siyi Deng <mr.siyi.d...@gmail.com> wrote in message <28889311-c79a-43f5-9594-a3d3a6225...@p10g2000prm.googlegroups.com>...
> >
> > > > can you show a few lines of code...
> >
> > > > us
> >
> > > Hey there, here's the code:
> >
> > > % original data for the plot;
> > > x = 1:1000;
> > > y = sin(2*pi*0.005*x);
> > > figure; plot(x,y)
> >
> > > % rasterize it to an 50 row x 100 col image;
> > > nRow = 50;
> > > nCol = 100;
> > > xNode = linspace(min(x),max(x),nCol);
> > > yNode = linspace(min(y),max(y),nRow);
> > > yInterp = interp1(x,y,xNode,'cubic','extrap');
> >
> > > xi = 1:nCol;
> > > yi = round(interp1(yNode,1:nRow,yInterp,'linear','extrap'));
> >
> > > im = accumarray([yi(:), xi(:)],1,[nRow,nCol]);
> >
> > > imagesc(im);
> >
> > > as you can see, the resulting im is not continuous. Any suggestion of
> > > improvement? thanks!
> >
> > one of the possible solutions
> >
> > % the data
> >      x=1:1000;
> >      y=sin(2*pi*0.005*x);
> >      nr=50;
> >      nc=100;
> > % the engine
> >      xx=round(linspace(min(x),max(x),nc));
> >      yy=linspace(min(y),max(y),nr);
> >      [nx,nx]=histc(x,xx);
> >      [ny,ny]=histc(y,yy);
> >      ac=accumarray([ny.',nx.'],1,[nr,nc])~=0;
> > % the result
> >      imagesc(ac);
> >      axis image;
> >      colormap(gray(2));
> >      set(gca,...
> >           'xtick',[],...
> >           'ytick',[]);
> >
> > us- Hide quoted text -
> >
> > - Show quoted text -
>
> That's really awesome, thank you us!

Subject: rasterizing plot to image

From: Devanshee Tanna

Date: 7 Sep, 2014 07:53:08

Message: 16 of 16

"Siyi Deng" wrote in message <a204032f-a237-464a-b7cf-2e9aa07ddb52@d4g2000prc.googlegroups.com>...
> On Jul 24, 12:53 pm, "us " <u...@neurol.unizh.ch> wrote:
> > Siyi Deng <mr.siyi.d...@gmail.com> wrote in message <28889311-c79a-43f5-9594-a3d3a6225...@p10g2000prm.googlegroups.com>...
> >
> > > > can you show a few lines of code...
> >
> > > > us
> >
> > > Hey there, here's the code:
> >
> > > % original data for the plot;
> > > x = 1:1000;
> > > y = sin(2*pi*0.005*x);
> > > figure; plot(x,y)
> >
> > > % rasterize it to an 50 row x 100 col image;
> > > nRow = 50;
> > > nCol = 100;
> > > xNode = linspace(min(x),max(x),nCol);
> > > yNode = linspace(min(y),max(y),nRow);
> > > yInterp = interp1(x,y,xNode,'cubic','extrap');
> >
> > > xi = 1:nCol;
> > > yi = round(interp1(yNode,1:nRow,yInterp,'linear','extrap'));
> >
> > > im = accumarray([yi(:), xi(:)],1,[nRow,nCol]);
> >
> > > imagesc(im);
> >
> > > as you can see, the resulting im is not continuous. Any suggestion of
> > > improvement? thanks!
> >
> > one of the possible solutions
> >
> > % the data
> >      x=1:1000;
> >      y=sin(2*pi*0.005*x);
> >      nr=50;
> >      nc=100;
> > % the engine
> >      xx=round(linspace(min(x),max(x),nc));
> >      yy=linspace(min(y),max(y),nr);
> >      [nx,nx]=histc(x,xx);
> >      [ny,ny]=histc(y,yy);
> >      ac=accumarray([ny.',nx.'],1,[nr,nc])~=0;
> > % the result
> >      imagesc(ac);
> >      axis image;
> >      colormap(gray(2));
> >      set(gca,...
> >           'xtick',[],...
> >           'ytick',[]);
> >
> > us- Hide quoted text -
> >
> > - Show quoted text -
>
> That's really awesome, thank you us!


Hey! I am unable to see the quoted text of code that you have written for making the image continuous. Cna you please post it again? Thanks a lot.

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us