From c6e13c01e01f96cb721af17f13ff4714eda0bb09 Mon Sep 17 00:00:00 2001 From: MichaelK <32868357+Michael-AngeloK@users.noreply.github.com> Date: Sat, 16 Apr 2022 17:45:19 +0200 Subject: [PATCH] Update: Added HOG feature extraction with visualization --- Test.m | 24 +++++++++++++++++++++--- histogramGrayscale.m | 3 ++- histogramOrientedGradients.m | 18 ++++++++++++++++++ 3 files changed, 41 insertions(+), 4 deletions(-) create mode 100644 histogramOrientedGradients.m diff --git a/Test.m b/Test.m index 561f022..531fef4 100644 --- a/Test.m +++ b/Test.m @@ -1,15 +1,33 @@ %% Example Title % Let's use Edvard Munch's - skrik picture -image = imread("colorHistogram/Edvard_Munch_-_Despair_(1894).jpg"); +img = imread("colorHistogram/Edvard_Munch_-_Despair_(1894).jpg"); % Let's also display the picture to see how it originally looks %imshow(image); %% edgeMap - Test of implementation -edgeImage = edgeMap(image); +%edgeImage = edgeMap(img); -imshow(edgeImage); +%imshow(edgeImage); +%% HOG - Test of implementation +% "hog" contains feature vector based on histogram of oriented gradients +% "visual" contains visualization object that can be used in plot method +[hog, visual] = histogramOrientedGradients(img); +figure, +imshow(img), +hold on, +plot(visual), +title("Hog features of image"); + +% Using Cellsize this time +[hog2, visual2] = histogramOrientedGradients(img, 32); + +figure, +imshow(img), +hold on, +plot(visual2), +title("Hog features using Cellsize"); %% Section 2 Title % Description of second code block b = 2; diff --git a/histogramGrayscale.m b/histogramGrayscale.m index 45ec9c9..0f55deb 100644 --- a/histogramGrayscale.m +++ b/histogramGrayscale.m @@ -1,6 +1,7 @@ %Returns histogram in grayscale function [out] = histogramGrayscale(image) - %image ideally is already grayscaled rbg2gray() if needed + %Converts image into grayscale + image = rgb2gray(image); [x,y] = size(image); out = zeros(1,255); diff --git a/histogramOrientedGradients.m b/histogramOrientedGradients.m new file mode 100644 index 0000000..fb2b8b2 --- /dev/null +++ b/histogramOrientedGradients.m @@ -0,0 +1,18 @@ +% Returns histogram of oriented gradients (HOG) feauture vector of an +% image and visualization object that can be used with plot method. +% Useful for training a classifier. +% +% NB! *Requires Computer Vision Toolbox Add-on* +% +function [featureVector, hogVisualization] = histogramOrientedGradients(varargin) + if nargin==1 + img=varargin{1}; + [featureVector, hogVisualization] = extractHOGFeatures(img); + elseif nargin==2 + img=varargin{1}; + size=varargin{2}; + [featureVector, hogVisualization] = extractHOGFeatures(img, "Cellsize",[size size]); + else + error('histogramOrientedGradients only accepts up to 2 input arguments!') + end +end \ No newline at end of file -- GitLab