How to create simple shapes in Matlab and save them as bitmap images.

21 views (last 30 days)
Is there a way for matlab code to generate simple 2d designs of simple shapes. Colour is not important. I need a way to generate basic shapes (e.g a circle -- see attachment 3mm.bmp) where I can control size and dimensions.
Is there also a way to have a gradient in the image (i.e. one side of the circle is white and becomes darker as you go across the circle with the end being black -- see attachment graded.bmp).
Thanks.

Answers (2)

Image Analyst
Image Analyst on 23 Apr 2015
Yes. You can use functions like imrect(), impoly(), roipoly(), rectangle(), imfreehand(), roipolyold(), poly2mask(), rbbox(), etc. To do the gradient is a little trickier but I'd recommend just drawing a ramp in your image and then using your shape as a mask - basically multiply your shape image by your ramp image. I don't have any code to do masked gradient shapes but I have attached some demos of some of the other functions if you need them.

Mike Garrity
Mike Garrity on 23 Apr 2015
Maybe this will be enough to get you started:
%%Create Objects
figure('Position',[100 100 512 512],'Color','white')
axes('Position',[0 0 1 1])
xlim([1 512]);
ylim([1 512]);
h = patch;
axis off
colormap(gray)
%%Set Coordinates
t = linspace(0,2*pi,120);
h.XData = 256 + 128*cos(t);
h.YData = 256 + 128*sin(t);
h.FaceVertexCData = h.XData;
h.FaceColor = 'interp';
%%Save Image
img = getframe(gcf);
imwrite(img.cdata,'circle.png')
You can learn more about how to make different shapes with the patch object here.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!