Create HoG feature matrix, array - matlab

I want to use HoG to extract features from a set if (16) images. I have done for one image, and it returned a good result. Now I want to do for all other images, and store the features.
.
Please how do I create a matrix/array to store the features for classification.
All my images are in a single folder...
Here is my code so far, pls help:
%% Load Images
imgFolder = fullfile('C:\Users\Engineering\Desktop\Finn\NEW');
imgSet = imageSet(imgFolder);
%% Display Montage of First Note
figure;
montage(imgSet(1).ImageLocation);
title('Images of Single Note');
%% Display Query Image and Database Side-Side
galleryImage = read(imgSet,1);
figure;
for i=1:size(imgSet,2)
imageList = imgSet(i).ImageLocation;
end
subplot(1,2,1);imshow(galleryImage);
subplot(1,2,2);montage(imageList);
diff = zeros(1,9);
%% Split Database into Training & Test Sets
[training,test] = partition(imgSet,[0.8 0.2]);
%% Extract and display Histogram of Oriented Gradient Features for single Note
[hogFeature, visualization]= ...
extractHOGFeatures(read(training,1));
figure;
subplot(2,1,1);imshow(read(training,1));title('Input Note');
subplot(2,1,2);plot(visualization);title('HoG Feature');
%% Extract HOG Features for training set
I need help in this section, please. Thank you

If i understand the question correctly. You wish to have a multidimensional array with Hog Features of all the images. If that is the case, here is a simple solution
accum = [];
for i = 1:training.Count
[hogFeature, visualization]= ...
extractHOGFeatures(read(training,i));
accum = [accum;hogFeature];
end
Now, each row of accum matrix is a set of Hog Features for the corresponding image. Hog Features of n-th image can be accessed by features = accum(n,:);.

Related

How can I merge multiple images into one and save it on matlab?

I need to merge multiple bitmap of same sizes into one image.That image is basically rotated in different angles and needs to be merged into one whole image. I have tried multiple methods but I come with many issues as I am not able to save that image.
I have tried multiple codes but I actually cannot make sense out of it. What I want to achieve is transparent overlay (not sure) that superimposes two images and you can actually see both one image
figure1 = figure;
ax1 = axes('Parent',figure1);
ax2 = axes('Parent',figure1);
set(ax1,'Visible','off');
set(ax2,'Visible','off');
[a,map,alpha] = imread('E:\training data\0.bmp');
I = imshow(a,'Parent',ax2);
set(I,'AlphaData',alpha);
F = imshow('E:\training data\200.bmp','Parent',ax1);
I just want to superimpose multiple images.
This is my data set:
This is what I want to achieve, i want to add all of the rotated images and achieved into one
This is what I get sadly, I have tried everything
The following does kind of what you want. First load the image, then divide it into 6 equal blocks, and add these. To add the pixel values, I first converted the image to doubles, since uint8 only can go up to pixel values of 255. This would mean that you will just see a large bright spot in the image because you are clipping.
Then add all the blocks. You will see in the output, that the car is not always perfect in the center of the block, so depending on what you are trying to achieve you may want to align the blocks using something like xcorr2.
% load image
A = imread('S82CW.jpg');
fig = figure(1); clf
image(A);
% convert A to double and divide in blocks.
A = double(A);
[img_h, img_w, ~] = size(A);
block_h = img_h/2;
block_w = img_w/3;
% split image in blocks
Asplit = mat2cell(A, repelem(block_h,2), repelem(block_w,3), 3);
% check if splitting makes sense
figure(2); clf
for k = 1:numel(Asplit)
subplot(3,2,k)
image(uint8(Asplit{k}))
end
% superimpose all blocks,
A_super = zeros(size(Asplit{1,1}),'like',Asplit{1,1} ); % init array, make sure same datatype
for k = 1:numel(Asplit)
A_super = A_super + Asplit{k};
end
% divide by max value in A and multiply by 255 to make pixel
% values fit in uint8 (0-255)
A_super_unit8 = uint8(A_super/max(A_super,[],'all')*255);
figure(3); clf;
image(A_super_unit8)

detect lines in image matlab

Hello I have a work on this image:
My objective is to count all the sperm in this image .for this I'm thinking to detect just the lines so it make my work easy. because I am beginner, in this step I am completely lost there are any algorithms can help me to detect lines?? ( I have seen that there are hough transformation and scan line algorithm) I don't which algorithm can help me and if there are others
Here's a piece of code that might help you getting started.
By looking at the image it seems very difficult to label sperms by looking at straight lines and hence using Hough transform won't help a lot.
In the example below I focused on filtering the image and counting the number of blobs. The code is commented and should be easy to understand.
img = imread('d9S3Z.png');
figure, imshow(img)
% convert to binary image
[X,map] = rgb2ind(img,0.0);
img = ind2gray(X,map); % Convert indexed to grayscale
level = graythresh(img); % Compute an appropriate threshold
% or use your own, e.g. level = 0.46
img_bw = im2bw(img,level);% Convert grayscale to binary
% create mask to remove edge interference
mask = zeros(size(img_bw));
mask(2:end-2,2:end-2) = 1;
img_bw(mask<1) = 1;
%invert image
img_inv =1-img_bw;
% find blobs
img_blobs = bwmorph(img_inv,'majority',10);
figure, imshow(img_blobs);
% count blobs
CC = bwconncomp(img_blobs);
num_sperm = CC.NumObjects # sperm count

How to find the variance of several images in MATLAB

I am relatively new to MATLAB and Image processing, so understand with me. I am working in experiment on image processing, and my current stage, I need to
1) read some images (about 100 images of same dimension)
2) store them in a variable (either cell array, vector or structure)
3) Find the variance of each pixel in each image
4) Form a new matrix to store each computed variance
Here is my code, but I am not sure it solves this problem not withstanding that I get result
clc;
im_File = dir('*.bmp');
files = {im_File.name};
for k=1:numel(files)
im{k} = imread(files{k});
%# Get the number of dimensions for your arrays
dim = ndims(im{k});
all_images = cat(dim+1,im{:});
% Use linear combine to acquire all the images
Linear_comb_im = imlincomb(1,all_images,'uin');
%get the variance of all images
computed_variance = var(double(Linear_comb_im),1,dim+1);
end
So it seems that you have a redundant variable here : both im and all_im basically save the same information. if the dimensions werent the same i would use a cell array, otherwise matlab likes matrices better.
In addition, I am not sure why you are performing the linear combination.
I would do the following:
clc;
im_File = dir('*.bmp');
files = {im_File.name};
for k=1:numel(files)
im(:,:,k) = imread(files{k}); % to save time you should initialize im begore the loop i.e. im = zeros(m,n,numerl(files)), where m,n are the size of the images
end
%get the variance of all images
computed_variance = var(double(im),1,3);
so im here is a 3D matrix containing the images in the 3rd dimension. In order to access the idx image:
im(:,:,idx)

How to add HOG features into matrix (matlab)

After extracting HOG features of folder of images, I want to add all this results in one matrix. How I can do this? this is my code in matlab:
training_female = 'E:\Training Set\Female Images';
% read all images with specified extention, its jpg in our case
filenames = dir(fullfile(training_female, '*.jpg'));
% count total number of photos present in that folder
total_images = numel(filenames);
for n = 1:total_images
% Specify images names with full path and extension
full_name= fullfile(training_female, filenames(n).name);
% Read images
training_images = imread(full_name);
[featureVector, hogVisualization] = extractHOGFeatures(training_images);
figure (n)
% Show all images
imshow(training_images); hold on;
plot(hogVisualization);
end
By looking at the documentation, calling extractHOGFeatures computes a 1 x N vector given an input image. Because it can be a bit cumbersome to calculate what the output size of this may be, which also depends on what parameters you set up for the HOG detector, it's best to first create an empty matrix and dynamically concatenate the features at each iteration. Usually for performance you pre-allocate a matrix if you want to populate the elements on an iterative basis. Not doing it this way gives a slight ding in performance, but it's the most adaptable given your situation. You may want to adjust the HOG parameters and if we do it the dynamic way, that removes the headache of determining what the total size of the matrix should be.
So do something like this. I've placed %//New tags where I've modified your code:
training_female = 'E:\Training Set\Female Images';
% read all images with specified extention, its jpg in our case
filenames = dir(fullfile(training_female, '*.jpg'));
% count total number of photos present in that folder
total_images = numel(filenames);
featureMatrix = []; %// New - Declare feature matrix
for n = 1:total_images
% Specify images names with full path and extension
full_name= fullfile(training_female, filenames(n).name);
% Read images
training_images = imread(full_name);
[featureVector, hogVisualization] = extractHOGFeatures(training_images);
%// New - Add feature vector to matrix
featureMatrix = [featureMatrix; featureVector];
figure(n);
% Show all images
imshow(training_images); hold on;
plot(hogVisualization);
end
featureMatrix will contain your HOG features where each row is for each image. Therefore, for a particular image i, you can determine the HOG features by:
feature = featureMatrix(i,:);
Caveat
I need to mention that the above code assumes that all images in your directory are the same size. If they're not, then the output vector size for each HOG call is going to be different. If that's the case, you'll want to have a cell array to adapt for the different sizes.
Therefore, do something like this:
training_female = 'E:\Training Set\Female Images';
% read all images with specified extention, its jpg in our case
filenames = dir(fullfile(training_female, '*.jpg'));
% count total number of photos present in that folder
total_images = numel(filenames);
featureMatrix = cell(1,total_images); %// New - Declare feature matrix
for n = 1:total_images
% Specify images names with full path and extension
full_name= fullfile(training_female, filenames(n).name);
% Read images
training_images = imread(full_name);
[featureVector, hogVisualization] = extractHOGFeatures(training_images);
%// New - Add feature vector to matrix
featureMatrix{n} = featureVector;
figure(n);
% Show all images
imshow(training_images); hold on;
plot(hogVisualization);
end
To access a particular image's features, or image i, do:
feature = featureMatrix{i};

How to apply Difference of Gaussian(DoG) approach to extract pores in fingerprint image in MATLAB?

I am working on a fingerprint recognition system, I need to extract pores which are present in the ridges.During my research I read that a blob detection technique can separate the pores from the ridges.However when I am applying a Gaussian Filter, it is smoothing the image. Also the DoG results in another similar image. If this is the right approach then where am I going wrong?
You actually need to apply a gaussian filter with 2 different sets of parameters, then subtract the filters and perform a convolution of the input image with that new filter, i.e. the difference of gaussians.
Here is an example with the coins.png demo image...The code is commented; don't hesitate to play with the parameters to see how that affects the output:
clear
clc
Im = imread('coins.png');
[r,c,~] = size(Im);
%// Initialize 3d array containing 3 images.
FilteredIm = zeros(r,c,3);
%// Try with 3 different kernel sizes
GaussKernel = [7 11 15];
figure;
subplot(2,2,1)
imshow(Im);
title('Original image');
TitleText = cell(1,numel(GaussKernel));
%// Apply Gaussian filter with 3 different kernel sizes/sigma values.
for k = 1:numel(GaussKernel)
%// Kernel sizes change but sigma values stay the same for
%// simplicity...you can play around with them of course.
GaussFilt1 = fspecial('Gaussian', GaussKernel(k), 12);
GaussFilt2 = fspecial('Gaussian', GaussKernel(k), 4);
%// Subtract the filters
DiffGauss = GaussFilt1 - GaussFilt2;
%// Perform convoluton to get filtered image
FilteredIm(:,:,k) = conv2(double(Im), DiffGauss, 'same');
%// Display
subplot(2,2,k+1)
imshow(FilteredIm(:,:,k));
TitleText{k} = sprintf('Filtered- Kernel of %i',GaussKernel(k));
title(TitleText{k});
end
Output:
Now for your application you probably need to play around with the kernel size and sigma parameters provided to fspecial. Good luck!