car detection using RCNN by taking live images from a camera and finding distance from that car

3 views (last 30 days)
i am trying to find distance from a detected car and displaying it in the image but i am getting this error while displaying the distance in the image "The number of columns in POSITION must be 4 for the 'rectangle' shape, or 3 for the 'circle' shape."
My code for is given below , i am stuck at this error kindly help me
load ('webcamsSceneReconstruction.mat');
data = load('fasterRCNNVehicleTrainingData.mat');
trainingData = data.vehicleTrainingData;
trainingData.imageFilename = fullfile(toolboxdir('vision'),'visiondata', trainingData.imageFilename);
%forming layers
layers = data.layers
options = trainingOptions('sgdm','InitialLearnRate', 1e-6, ...
'MaxEpochs', 1, ...
'CheckpointPath', tempdir);
% Training detector.
detector = trainFasterRCNNObjectDetector(trainingData, layers, options)
% Testing the Fast R-CNN detector on a test image.
[height width channel]=size(snapshot(a));
while framecount < 5
img = snapshot(a);
im_Left = img(:, 1 : width/2, :);
im_Right = img(:, width/2 +1: width, :);
% Run detector.
[bbox, score, label] = detect(detector, im_Left);
[bbox, score, label]= detect(detector,im_Right);
detectedImg1 = insertShape(im_Left, 'Rectangle', bbox);
detectedImg2= insertShape(im_Right, 'Rectangle',bbox);
center1 = detectedImg1(1:2) + detectedImg1(3:4)/0.005;
center2 = detectedImg2(1:2) + detectedImg2(3:4)/0.005;
point3d = triangulate(center1, center2, stereoParams);
distanceInMeters = norm(point3d)/1000;
distanceAsString = sprintf('%0.2f meters', distanceInMeters);
leftimage = insertObjectAnnotation(leftimage,'rectangle',center1,distanceAsString);
rightimage = insertObjectAnnotation(rightimage,'rectangle',center2, distanceAsString);
%leftimage = insertShape(detectedImg1,'Rectangle',bboxes,'LineWidth',3);
%rightimage = insertShape(detectedImg2,'Rectangle',bboxes,'LineWidth',3);
leftimage = insertShape(leftimage,'FilledRectangle',detectedImg1);
rightimage = insertShape(rightimage,'FilledRectangle',detectedImg2);
imshowpair(leftimage, rightimage, 'montage');
  • i am getting the error displayed in image*

Answers (1)

MUHAMMED IRFAN on 16 Jun 2018
Edited: MUHAMMED IRFAN on 16 Jun 2018
The issue is with how you called the function insertObjectAnnotation . You better go through its documentation here.
If you are trying to insert a rectangle shape, you need a bounding box which is a 1 * 4 array as [x y width height]. Your's is center1 and center2 which are of size 1 * 2.
Also I'm not clear what you tried to do in the lines
center1 = detectedImg1(1:2) + detectedImg1(3:4)/0.005;
center2 = detectedImg2(1:2) + detectedImg2(3:4)/0.005;
You may want to go through it's logic once again.
  1 Comment
Walter Roberson
Walter Roberson on 16 Jun 2018
Right. Remember that insertShape returns an RGB image, so detectedImag1 would be an RGB image, not the coordinates of anything. The coordinates are from bbox.
Note that you have
[bbox, score, label] = detect(detector, im_Left);
[bbox, score, label]= detect(detector,im_Right);
so you do the detection on the Left image and you throw away all of the results of that, overwriting the output variables with the detection from the right. Then you draw the results from the right on both of the images.
You also have the issue that it is possible for multiple vehicles to be detected. That results in a bbox that has more than one row.
The centers of a bounding boxare at bbox(:,[1 2]) + bbox(:, [3 4])/2

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!