Hi, I am trying to put annotate my object in an image but I am having errors.
조회 수: 2 (최근 30일)
이전 댓글 표시
I am trying to put a rectangle on the object that is being classified. I am using insertObjectAnnotation however I am receiving some errors. My code is as followed:
clear
net = googlenet; % Load the neural net
net.Layers
% while true
labelType =''
[file,path]=uigetfile({'*.jpg;*.jpeg;*.bmp;*.png;*.tif'},'Choose an image');
s=[path,file];
I=imread(s);
% I = imread('car1.jpeg');
sz = net.Layers(1).InputSize ;
inputSize = net.Layers(1).InputSize
size(I)
I = imresize(I,inputSize(1:2));
imshow(I)
hold on
movegui(imshow(I),[500,490]);
[label, score] = classify(net, I); % Classify the picture
% label = classify(net, I); % Classify the picture
if (label == 'convertible')
labelType = 'Vehicle Recognised'
elseif (label == 'moped')
labelType = 'Vehicle Recognised'
elseif (label == 'tank')
labelType = 'Vehicle Recognised'
elseif (label == 'sports car')
labelType = 'Vehicle Recognised'
elseif (label == 'moving van')
labelType = 'Vehicle Recognised'
elseif (label == 'trailer truck')
labelType = 'Vehicle Recognised'
elseif (label == 'garbage truck')
labelType = 'Vehicle Recognised'
elseif (label == 'trailer truck')
labelType = 'Vehicle Recognised'
elseif (label == 'minivan')
labelType = 'Vehicle Recognised'
elseif (label == 'beach wagon')
labelType = 'Vehicle Recognised'
elseif (label == 'ambulance')
labelType = 'Vehicle Recognised'
elseif (label =='cab')
labelType = 'Vehicle recognised';
else
labelType = 'No Vehicle Recognised'
end
label_str = cell(3,1);
conf_val = [85.212 98.76 78.342];
for ii=1:3
label_str{ii} = ['Confidence: ' num2str(conf_val(ii),'%0.2f') '%'];
end
position = [23 373 60 66;35 185 77 81;77 107 59 26];
PositionWhereRectangleShouldBe = [100 100 50 50];
I = insertObjectAnnotation(I, 'rectangle', PositionWhereRectangleShouldBe, label_str, score);
figure
imshow(I);
% imshow(I); % Show the picture
% message = strcat(labelType,'-', char(label))
title({char(message),num2str((score),3)}); % Show the label
title(char(message)); % Show the label
I=imread(s);
BW = im2bw(I,0.2);
se = strel('rectangle', [2 20]);
BW_opened = imclose(BW,se);
% figure, imshow(BW_opened,[])
s=regionprops(BW_opened,'Area','BoundingBox');
[hh,ii] = sort([s.Area],'descend');
out = imcrop(I,s(ii(2)).BoundingBox);
figure,imshow(out);
movegui(imshow(out),[300,400]);
out_path = 'C:\Users\Student\Desktop\Vehicle number plate recognition\test images'; % Give path here
fullFileName = fullfile(out_path, 'test_image.jpg');
imwrite(out, fullFileName);
load imgfildata;
picture=imread(fullFileName);
[~,cc]=size(picture);
picture=imresize(picture,[300 500]);
if size(picture,3)==3
picture=rgb2gray(picture);
end
se=strel('rectangle',[5,5]);
a=imerode(picture,se);
figure,imshow(a);
movegui(imshow(a),[800,350]);
b=imdilate(a,se);
threshold = graythresh(picture);
picture =~im2bw(picture,threshold);
percentageBlack = (1-nnz(picture)/numel(picture))*100
picture = bwareaopen(picture,50);
if (percentageBlack < 50)
picture = imcomplement(picture);
end
[L,Ne]=bwlabel(picture);
propied=regionprops(L,'BoundingBox');
hold on
pause(1)
for n=1:size(propied,1)
rectangle('Position',propied(n).BoundingBox,'EdgeColor','g','LineWidth',1)
end
hold off
% figure
final_output=[];
t=[];
for n=1:Ne
[r,c] = find(L==n);
n1=picture(min(r):max(r),min(c):max(c));
n1=imresize(n1,[42,24]);
% imshow(n1)
pause(0.2)
x=[ ];
totalLetters=size(imgfile,2);
for k=1:totalLetters
y=corr2(imgfile{1,k},n1);
x=[x y];
end
t=[t max(x)];
if max(x)>.45
z=find(x==max(x));
out=cell2mat(imgfile(2,z));
final_output=[final_output out];
end
end
file = fopen('number_Plate.txt', 'wt');
fprintf(file,'%s\n',final_output);
fclose(file);
winopen('number_Plate.txt')
drawnow;
%end
but I am having an error of:
_Error using insertObjectAnnotation
Expected a string scalar or character vector for the parameter name.
Error in insertObjectAnnotation
Error in insertObjectAnnotation
Error in insertObjectAnnotation
Error in GoogleNet (line 56) I = insertObjectAnnotation(I, 'rectangle', PositionWhereRectangleShouldBe, label_str, score);_
댓글 수: 1
Florian Morsch
2018년 8월 6일
Why did you open a new post on your previous one? Or did you forget your post here: https://de.mathworks.com/matlabcentral/answers/413211-hi-i-am-trying-to-put-a-box-around-the-car-and-have-it-s-confidence-level-placed-on-top-of-the-box?s_tid=mlc_ans_email_ques
PositionWhereRactangleShouldBe was an example, not something you should write into your code. You already have a "position" variable in your code, but didnt use it in the first place. Right now the rectangle will be placed at the random location [100 100] with width and heigth [50 50] which i gave as example, you wont mark your object with it.
답변 (1개)
Image Analyst
2018년 8월 6일
Maybe try using rectangle() or line() or plot() instead if you're having trouble with it and don't need it burned into the image.
댓글 수: 2
Image Analyst
2018년 8월 6일
Apparently conf_val does not have ii elements in it. What is the value of ii, and what is the length of conf_val when you get the error?
참고 항목
카테고리
Help Center 및 File Exchange에서 Computer Vision Toolbox에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!