krsnik93
krsnik93

Reputation: 186

Matlab won't superimpose rectangle

I'm trying to plot rectangles around blobs in every frame of a video. Method insertShape works but not very well. Method rectangle doesn't seem to do anything and I don't understand where's the problem as it works with a .avi file but won't with .mp4 ...

firstRunFlag = true;

threshold = 0.18;
se_erode = strel('square', 3);
se_dilate = strel('square', 30);

v = VideoReader('Traffic.mp4');    
figure;    
while hasFrame(v)        
    frame_now = readFrame(v);        
    if (firstRunFlag)
        frame_prev = frame_now;
        firstRunFlag = false;
    end

    frame_diff = frame_now - frame_prev;
    frame_diff_grayscale = rgb2gray(frame_diff);

    bw = im2bw(frame_diff_grayscale, threshold);
    bw = imerode(bw, se_erode);
    bw = imdilate(bw, se_dilate);
    bw = imfill(bw,'holes');

    boxes = regionprops(bw, 'BoundingBox');
    imshow(frame_now);

    for i=1:length(boxes)
        this_box = boxes(i).BoundingBox;
        rectangle('Position', [this_box(1), this_box(2), this_box(3), this_box(4)], 'EdgeColor', 'r', 'LineWidth', 2)
    end    
    frame_prev = frame_now;
end

It also seems to be working on a single image, but not when displaying frame after frame...

Upvotes: 1

Views: 85

Answers (1)

Shai
Shai

Reputation: 114866

Sometimes matlab does not update a plot, when there are several operations applied to it (like imshowing consecutive frames and rectanlges). Thus, it is useful to force matlab to update the plot using drawnow command.

Upvotes: 3

Related Questions