How to detect empty spaces/regions in an image

58 views (last 30 days)
Good day. I want to improve some image processing code so it detects empty spaces in an image as described above. The current code detects the number of empty spaces in the image but not the actual spaces/empty regions.
The code takes an image of parked cars as input, processes it and returns the number of free spaces but doesnt detect the actual spaces in the image. I'd like to improve it so it detects the spaces either using a rectangle as a marker or something similar, or even just the outline of the space. Thank you very much.
This is what the current output looks like: Image
And here's the code:
function varargout = untitled1(varargin)
% UNTITLED1 MATLAB code for untitled1.fig
% UNTITLED1, by itself, creates a new UNTITLED1 or raises the existing
% singleton*.
% H = UNTITLED1 returns the handle to a new UNTITLED1 or the handle to
% the existing singleton*.
% UNTITLED1('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in UNTITLED1.M with the given input arguments.
% UNTITLED1('Property','Value',...) creates a new UNTITLED1 or raisesthe
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before untitled1_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to untitled1_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 untitled1
% Last Modified by GUIDE v2.5 10-Apr-2020 15:30:51
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @untitled1_OpeningFcn, ...
'gui_OutputFcn', @untitled1_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 untitled1 is made visible.
function untitled1_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 untitled1 (see VARARGIN)
% Choose default command line output for untitled1
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes untitled1 wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = untitled1_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)
handles.output=hObject;
[a b]=uigetfile({'*.*'});
img=imread([b a]);
grayy=rgb2gray(img);
gr=graythresh(grayy);
handles.bw=im2bw(grayy,gr);
imshow(img,'Parent',handles.axes1);
guidata(hObject,handles);
% --- 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)
handles.output=hObject;
inverse_binary=not(handles.bw);
[handles.L handles.Num_object]=bwlabel(inverse_binary);
set(handles.text2,'string',10-handles.Num_object);
imshow(handles.L, 'parent',handles.axes2);
guidata(hObject,handles);

Accepted Answer

yanqi liu
yanqi liu on 24 Nov 2021
yes,sir,may be use some process to get the background white space
clc; clear all; close all;
img = imread('https://gcdn.pbrd.co/images/zu7C62hgoku3.png');
rect = [40 74 289 220];
img2 = imcrop(img, rect);
grayy = rgb2gray(img2);
% use origin method
gr=graythresh(grayy);
bw=im2bw(grayy,gr);
inverse_binary=not(bw);
% process
inverse_binary2 = imclose(inverse_binary, strel('disk', 5));
inverse_binary2 = imfill(inverse_binary2, 'holes');
bw(inverse_binary2) = 0;
bw = logical(bw);
figure; imshow(bw);
img2r = img2(:,:,1);img2g = img2(:,:,2);img2b = img2(:,:,3);
img2r(bw) = 255;img2g(bw) = 0;img2b(bw) = 0;
res = cat(3,img2r,img2b,img2g);
figure; imshow(res);
  5 Comments
yanqi liu
yanqi liu on 26 Nov 2021
Edited: yanqi liu on 26 Nov 2021
sorry,sir,the simple trick was used before,please check the follow code
clc; clear all; close all;
urls = {'https://www.mathworks.com/matlabcentral/answers/uploaded_files/813394/image.png', ...
'https://www.mathworks.com/matlabcentral/answers/uploaded_files/813399/image.png', ...
'https://www.mathworks.com/matlabcentral/answers/uploaded_files/813404/image.png', ...
'https://www.mathworks.com/matlabcentral/answers/uploaded_files/813409/image.png'};
for k = 1 : length(urls)
img = imread(urls{k});
img2 = img;
grayy = rgb2gray(img2);
% use origin method
gr=graythresh(grayy);
bw=im2bw(grayy,gr);
inverse_binary=not(bw);
% process
inverse_binary2 = imclose(inverse_binary, strel('disk', 5));
inverse_binary2 = imfill(inverse_binary2, 'holes');
bw(inverse_binary2) = 0;
bw = logical(bw);
bw2 = ~bw;
% make 5*5 grid
mask = zeros(size(bw));
sz = size(mask);
rows = round(linspace(3, sz(1)-3, 3));
cols = round(linspace(3, sz(2)-3, 6));
for i = rows
mask(i-2:i+2,:) = 1;
end
for i = cols
mask(:, i-2:i+2) = 1;
end
mask = ~logical(mask);
mask = imerode(mask, strel('square', 3));
% get rects
stats = regionprops(bw2);
cens = round(cat(1, stats.Centroid));
mask2 = bwselect(mask, cens(:,1), cens(:, 2));
mask(mask2) = 0;
stats = regionprops(logical(mask));
rects = round(cat(1, stats.BoundingBox));
figure; imshow(img2, []); hold on;
for i = 1 : size(rects, 1)
hold on; rectangle('position', rects(i,:), 'EdgeColor', 'r', 'LineWidth', 2)
end
end
Galuf
Galuf on 26 Nov 2021
Thank you very much, this works perfectly. I'll definitely check out your book. I really appreciate this, thanks so much for your assistance.

Sign in to comment.

More Answers (0)

Products


Release

R2019a

Community Treasure Hunt

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

Start Hunting!