1

I have an "inverted-pendulum" video which I try to find the mid point of moving part. I am using Computer Vision Toolbox

enter image description here

I change the mid point's color using detected coordinates. Assume that X is the frame's row number for the detected mid point and the Y is the col number.

while ~isDone(hVideoFileReader)

    frame = step(hVideoFileReader);
    ...
    frame(X-3:X+3, Y-3:Y+3, 1) = 1; % # R=1 make the defined region red
    frame(X-3:X+3, Y-3:Y+3, 2) = 0; % # G=0
    frame(X-3:X+3, Y-3:Y+3, 3) = 0; % # B=0

    step(hVideoPlayer, frame);

end

Then I easily have a red square. But I want to add a red filled circle on the detected point, instead of a square. How can I do that?

Amro
  • 123,847
  • 25
  • 243
  • 454
zkanoca
  • 9,664
  • 9
  • 50
  • 94

3 Answers3

4

You can use the insertShape function. Example:

img = imread('peppers.png');
img = insertShape(img, 'FilledCircle', [150 280 35], ...
    'LineWidth',5, 'Color','blue');
imshow(img)

The position parameter is specified as [x y radius]

image


EDIT:

Here is an alternative where we manually draw the circular shape (with transparency):

% some RGB image
img = imread('peppers.png');
[imgH,imgW,~] = size(img);

% circle parameters
r = 35;                    % radius
c = [150 280];             % center
t = linspace(0, 2*pi, 50); % approximate circle with 50 points

% create a circular mask
BW = poly2mask(r*cos(t)+c(1), r*sin(t)+c(2), imgH, imgW);

% overlay filled circular shape by using the mask
% to fill the image with the desired color (for all three channels R,G,B)
clr = [0 0 255];            % blue color
a = 0.5;                    % blending factor
z = false(size(BW));
mask = cat(3,BW,z,z); img(mask) = a*clr(1) + (1-a)*img(mask);
mask = cat(3,z,BW,z); img(mask) = a*clr(2) + (1-a)*img(mask);
mask = cat(3,z,z,BW); img(mask) = a*clr(3) + (1-a)*img(mask);

% show result
imshow(img)

I'm using the poly2mask function from Image Processing Toolbox to create the circle mask (idea from this post). If you don't have access to this function, here is an alternative:

[X,Y] = ndgrid((1:imgH)-c(2), (1:imgW)-c(1));
BW = (X.^2 + Y.^2) < r^2;

That way you get a solution using core MATLAB functions only (no toolboxes!)

Community
  • 1
  • 1
Amro
  • 123,847
  • 25
  • 243
  • 454
1

If you have an older version of MATLAB with the Computer Vision System Toolbox installed, you can use vision.ShapeInserter system object.

Dima
  • 38,860
  • 14
  • 75
  • 115
  • 1
    I knew it was [there](http://www.mathworks.com/help/releases/R2012a/toolbox/vision/ref/vision.shapeinserterclass.html) :) It was even available back when it was part of the Simulink toolbox "Video and Image Processing Blockset" (which later became the current CVST toolbox as of R2011a): http://www.mathworks.com/help/vision/ref/drawshapes.html – Amro Jun 15 '14 at 15:13
1

Thanks @Dima, I have created a shapeInserter object.

greenColor = uint8([0 255 0]); 
hFilledCircle = vision.ShapeInserter('Shape','Circles',...
                              'BorderColor','Custom',...
                              'CustomBorderColor', greenColor ,...
                              'Fill', true, ...
                              'FillColor', 'Custom',...
                              'CustomFillColor', greenColor );
...

fc = int32([Y X 7;]);

frame = step(hFilledCircle, frame, fc);

I then applied it to detected point.

enter image description here

zkanoca
  • 9,664
  • 9
  • 50
  • 94