How to detect empty spaces/regions in an image

56 ビュー (過去 30 日間)
Galuf
Galuf 2021 年 11 月 23 日
コメント済み: Galuf 2021 年 11 月 26 日
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);

採用された回答

yanqi liu
yanqi liu 2021 年 11 月 24 日
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 件のコメント
Galuf
Galuf 2021 年 11 月 26 日
Thank you very much, this works perfectly. I'll definitely check out your book. I really appreciate this, thanks so much for your assistance.

サインインしてコメントする。

その他の回答 (0 件)

Community Treasure Hunt

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

Start Hunting!

Translated by