error while executing boundingbox method

2 views (last 30 days)
Anurag Nemade
Anurag Nemade on 12 Mar 2022
Answered: Simon Chan on 12 Mar 2022
i am not getting proper region for bounding box while executing following code plz help me
this is my full code of vehicle number plate detection
function varargout = new(varargin)
% NEW MATLAB code for new.fig
% NEW, by itself, creates a new NEW or raises the existing
% singleton*.
%
% H = NEW returns the handle to a new NEW or the handle to
% the existing singleton*.
%
% NEW('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in NEW.M with the given input arguments.
%
% NEW('Property','Value',...) creates a new NEW or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before new_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to new_OpeningFcn via varargin.
%
% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
% instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES
% Edit the above text to modify the response to help new
% Last Modified by GUIDE v2.5 12-Mar-2022 16:14:38
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @new_OpeningFcn, ...
'gui_OutputFcn', @new_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
% --- Executes just before new is made visible.
function new_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to new (see VARARGIN)
% Choose default command line output for new
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes new wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = new_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure
varargout{1} = handles.output;
% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
[filename, pathname] = uigetfile('Resize\*.*', 'Pick a MATLAB code file');
if isequal(filename,0) || isequal(pathname,0)
disp('User pressed cancel')
else
filename=strcat(pathname,filename);
global a
a=imread(filename);
axes(handles.axes1);
imshow(a);
handles.a=a;
guidata(hObject, handles);
end
% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global a
imcrp=imcrop(a,[20 40 150 300]);
imshow(imcrp);
b=rgb2gray(imcrp);
imshow(b);
% --- Executes on button press in pushbutton3.
function pushbutton3_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton3 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global a
imcrp=imcrop(a,[20 40 150 300]);
imshow(imcrp);
threshold=graythresh(a);
a=~im2bw(a,threshold);
axes(handles.axes2);
imshow(a);
a1=bwareaopen(a,2000);
b=a-a1;
imcrp=imcrop(b,[20 40 150 300]);
imshow(imcrp);
axes(handles.axes6);
imshow(b);
% --- Executes on button press in pushbutton6.
function pushbutton6_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton6 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global b imcrp
imcrp=imcrop(b,[20 40 150 300]);
imshow(imcrp);
[L,n]=bwlabel(b);
propied=regionprops(L,'BoundingBox');
hold on
pause(1)
for n=1:size(propied)
c=propied(n).BoundingBox;
rectangle('Position',propied(n).BoundingBox,'EdgeColor','g','LineWidth',2)
end
axes(handles.axes2);
imshow(c);

Answers (1)

Simon Chan
Simon Chan on 12 Mar 2022
Function imshow shows a cropped image 'imcrp' from original image 'b', so the size of the displayed image is smaller.
However, the bounding box is calculated from original image 'b', which is the larger one and hence the coordinates are not matched.
Modify the code as follows:
xstart = 20; % In your case, cropped from x=20
ystart = 40; % In your case, cropped from y=40
imcrp=imcrop(b,[xstart ystart 150 300]);
imshow(imcrp);
[L,n]=bwlabel(b);
propied=regionprops(L,'BoundingBox');
hold on
pause(1)
for n=1:size(propied)
c=propied(n).BoundingBox;
c(1) = c(1) - xstart + 1; % Shifted the bounding box x-coordinates by '-xstart+1'
c(2) = c(2) - ystart + 1; % Shifted the bounding box x-coordinates by '-ystart+1'
rectangle('Position',c,'EdgeColor','g','LineWidth',2)
end

Community Treasure Hunt

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

Start Hunting!