Clear Filters
Clear Filters

Who can refactor this code to open in Matlab2020

1 view (last 30 days)
%% Initialization
redThresh = 0.10; % Threshold for red detection
vidDevice = imaq.VideoDevice('winvideo', 1, 'RGB24_640x480', ... % Acquire input video stream
'ROI', [1 1 640 480], ...
'ReturnedColorSpace', 'rgb');
vidInfo = imaqhwinfo(vidDevice); % Acquire input video property
hblob = vision.BlobAnalysis('AreaOutputPort', false, ... % Set blob analysis handling
'CentroidOutputPort', true, ...
'BoundingBoxOutputPort', true', ...
'MinimumBlobArea', 800, ...
'MaximumBlobArea', 3000, ...
'MaximumCount', 10);
hshapeinsRedBox = vision.ShapeInserter( 'BorderColor', 'Custom', ... % Set Red box handling
'CustomBorderColor', [1 0 0], ...
'Fill', true, ...
'FillColor', 'Custom', ...
'CustomFillColor', [1 0 0], ...
'Opacity', 0.2);
htextins = vision.TextInserter('Text', 'Number of Red Object: %2d', ... % Set text for number of blobs
'Location', [7 2], ...
'Color', [1 0 0], ... % Red Colour
'FontSize', 12);
htextinsCent = vision.TextInserter('Text', '+ X:%4d, Y:%4d', ... % set text for centroid
'LocationSource', 'Input port', ...
'Color', [1 1 0], ... // yellow color
'FontSize', 14);
hVideoIn = vision.VideoPlayer('Name', 'Object Tracking - Avinash.B [2017209043]', ... % Output Video Player
'Position', [100 100 640 480]);
nFrame = 0; % Frame number initialization
%% Processing Loop
while(nFrame < 2000)
rgbFrame = step(vidDevice); % Acquire single frame
rgbFrameMirror = flipdim(rgbFrame,2); % Obtain the mirror image for displaying
rgbgray=rgb2gray(rgbFrameMirror);
diffFrameSub = imsubtract(rgbFrameMirror(:,:,1), rgbgray); % Get red component of the image
diffFrame = medfilt2(diffFrameSub, [3 3]); % Filter out the noise by using median filter
binFrame = im2bw(diffFrame, redThresh); % Convert the image into binary image with the red objects as white
[centroid, bbox] = step(hblob, binFrame); % Get the centroids and bounding boxes of the blobs
centroid = uint16(centroid); % Convert the centroids into Integer for further steps
rgbFrameMirror(1:20,1:165,:) = 0; % put a black region on the output stream
vidIn = step(hshapeinsRedBox, rgbFrameMirror, bbox); % Instert the red box
for object = 1:1:length(bbox(:,1)) % Write the corresponding centroids
centX = centroid(object,1); %Store X Value in centX
centY = centroid(object,2); %Store Y Value in centY
vidIn = step(htextinsCent, vidIn, [centX centY], [centX-6 centY-9]); % Video Insert X,Y Values
end
vidIn = step(htextins, vidIn, uint8(length(bbox(:,1)))); % Count the number of blobs
step(hVideoIn, vidIn); % Output video stream
nFrame = nFrame+1;
end
%% Clearing Memory
release(hVideoIn); % Release all memory and buffer used
release(vidDevice);
clear all;
clc;
  2 Comments
Cris LaPierre
Cris LaPierre on 3 May 2023
What errors are you getting when you run your code in R2020a/b?
Vitaliy Rychak
Vitaliy Rychak on 3 May 2023
Unable to resolve the name vision.TextInserter.
Error in ... (line 37)
htextinsRed = vision.TextInserter('Text', 'Red : %2d', ... % Set text for number of blobs

Sign in to comment.

Answers (1)

Cris LaPierre
Cris LaPierre on 3 May 2023
There does not appear to be a vision.TextInserter function or method. Perhaps you are meaning to use insertText?
  2 Comments
Vitaliy Rychak
Vitaliy Rychak on 3 May 2023
I tried replacing with "insertText". Maybe I did it wrong, because I don't have much experience, I'm just a beginner
Cris LaPierre
Cris LaPierre on 3 May 2023
Edited: Cris LaPierre on 3 May 2023
You won't be able to just swap vision.TextInserter with insertText as the syntax is different. I suggest going through the linked doc page to learn the correct syntax of insertText so you can adapt your code.
You'll also find a couple examples on the linked page.
The main difference I see is that your first input to insertText needs to be an image, which means you probably need to move this code inside your for loop, as your images will be the video frames.

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!