user3396218
user3396218

Reputation: 255

How to draw multiple shapes using Matlab vision toolbox and step function?

I'm trying to insert 2 shapes (circle and rectangle) to an image using these functions. But I'm unable to do that. Here's my code

J = step(shapeInserter, I, bbox); %bbox is rectangle which is already defined
J = step(shapeInserter, I, circle); %circle is circle which is already defined
imwrite(J,'image.jpg','jpg'); % it draws only the circle

I have a long way which is to save the rectange image then load again to draw the circle and resave. Which i wish to avoid as it's really time consuming.

I'm trying to do something like this (similar to the plotting graph function)

hold on
%draw circle
%draw rectangle
hold off
imwrite(J,'image.jpg','jpg');

Please advise, thanks

Upvotes: 4

Views: 3787

Answers (2)

Omid S.
Omid S.

Reputation: 761

I'm a total noob to Matlab Image stuff but here is some very strait forward way of doing it :

   frame=imread( '/home/omido/DeepLearning/matlab_segmentation/track/track/Image2.jpg' );
        result = insertShape(frame, 'FilledRectangle', [100,100,100,100], 'Color', 'green');
result = insertShape(result, 'FilledRectangle',  [200,200,200,200]  , 'Color', 'yellow');
        imshow(result);

and this is the result: enter image description here

and as in the previous answer there are multiple shapes, you can find them here.

Upvotes: 1

hbaderts
hbaderts

Reputation: 14336

The vision.ShapeInserter object has a property Shape, which can either be set to

  • 'Rectangles'
  • 'Circles'
  • 'Lines'
  • 'Polygons'

By default, it is set to 'Rectangles'. To use the same ShapeInserter object to place a circle, you will have to release it first by calling release(shapeInserter); and modifying the Shape property by set(shapeInserter,'Shape','Circles'). Then you can call the step method again to insert the circle.

Here is a small example:

I = imread('cameraman.tif');
rectangle = int32([10,10,50,60]);
circle = int32([200,200,40]);
shapeInserter = vision.ShapeInserter('Fill',true);

J = step(shapeInserter,I,rectangle);

release(shapeInserter);
set(shapeInserter,'Shape','Circles');
K = step(shapeInserter,J,circle);

imshow(K);

Result

Upvotes: 4

Related Questions