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};
Related
I am trying to get pixel intensity values from regions of interest in RGB images.
I segmented the image and saved the regions of interest (ROI) using regionprops 'PixelList' in MATLAB, as shown below:
In this example I am using "onion.png" image built in MATLAB. (But in reality I have hundreds of images, and each of them have several ROIs hence why I'm saving the ROIs separately.)
%SEGMENTATION PROGRAM:
a=imread('C:\Program Files\MATLAB\MATLAB Production Server\R2015a\toolbox\images\imdata\onion.png');warning('off', 'Images:initSize:adjustingMag');
figure; imshow(a,[]);
nrows = size(a,1);ncols = size(a,2);
zr=ones(nrows,ncols); %matrix of ones
r=a(:,:,1);g=a(:,:,2);b=a(:,:,3); %get RGB values
rr=double(r);gg=double(g);bb=double(b);% convert to double to avoid uint8 sums
bgd=(rr+bb)./(2*gg); %calculation RGB ratio of background
zr1=bgd>1.15; %matrix containing background as 1 and ROI as 0
% remake binary image for holes command which requires white object to fill % (this step is not relevant for this example, but it is for my data)
zr2=zr1<0.5;
zr3=imfill(zr2, 'holes');
figure;imshow(zr3); pause;
roi=regionprops(zr3,'Centroid','PixelList','Area');
ar=[roi.Area];
% find sort order , largest first
[as, ia]=sort(ar(1,:),'descend');
for w=1:length(roi); xy(w,:)=roi(w).Centroid;end
% use sort index to put cenrtoid list in same order
xy1=xy(ia,:);
%and pixel id list
for w=1:length(roi)
roi2(w).PixelList=roi(ia(w)).PixelList;
end
%extract centriod positions as two colums
%SAVE PIXEL LIST FOR EACH ROI IN A SEPARATE FILE
for ww=1:w;
k=roi2(ww).PixelList;
save('onion_PL','k');
end
How do I use this pixel list to get the intensity values in the original image? More specifically, I need to get the ratio of pixels in Green channel over Red ("grr=rdivide(gg,rr);"), but only in the region of interest labeled with PixelList. Here's my code so far:
%PL is the PixelList output we got above.
a=imread('C:\Program Files\MATLAB\MATLAB Production Server\R2015a\toolbox\images\imdata\onion.png');warning('off', 'Images:initSize:adjustingMag');
PL=dir(['*PL.mat']); %load file PixelList files. "dir" is a variable with directory path containing the pixelist files. In this example, we saved "onion_PL.mat"
for m=1:length(PL);
load(PL(m).name);
ex=[]; %empty matrix to hold the extracted values
for mm=1:length(k);
%INSERT ANSWER HERE
end
This next bit is wrong because it's based on the entire image ("a"), but it contains the calculations that I would like to perform in the ROIs
figure; imshow(a,[]);
pause;
nrows = size(a,1);ncols = size(a,2);
zr=ones(nrows,ncols); %matrix of ones
r=a(:,:,1);g=a(:,:,2);b=a(:,:,3); %get RGB values
rr=double(r);gg=double(g);bb=double(b);% convert to double to avoid uint8 sums
grr=rdivide(gg,rr);
I am brand new to MATLAB, so my code is not the greatest... Any suggestions will be greatly appreciated. Thank you in advance!
The loop you are looking for seems simple:
grr = zeros(nrows, ncols); % Initialize grr with zeros.
for mm = 1:length(k)
x = k(mm, 1); % Get the X (column) coordinate.
y = k(mm, 2); % Get the Y (row) coordinate.
grr(y, x) = gg(y, x) / rr(y, x);
end
A more efficient solution is using sub2ind for converting the x,y coordinates to linear indices:
% Convert k to linear indices.
kInd = sub2ind([nrows, ncols], k(:,2), k(:,1));
% Update only the indices in the PixelList.
grr(kInd) = rdivide(gg(kInd), rr(kInd));
In your given code sample there are 5 PixelLists.
I don't know how do you want to "arrange" the result.
In my code sample, I am saving the 5 results to 5 mat files.
Here is an executable code sample:
close all
%SEGMENTATION PROGRAM:
a=imread('onion.png');warning('off', 'Images:initSize:adjustingMag');
figure; imshow(a,[]);
nrows = size(a,1);ncols = size(a,2);
zr=ones(nrows,ncols); %matrix of ones
r=a(:,:,1);g=a(:,:,2);b=a(:,:,3); %get RGB values
rr=double(r);gg=double(g);bb=double(b);% convert to double to avoid uint8 sums
bgd=(rr+bb)./(2*gg); %calculation RGB ratio of background
zr1=bgd>1.15; %matrix containing background as 1 and ROI as 0
% remake binary image for holes command which requires white object to fill % (this step is not relevant for this example, but it is for my data)
zr2=zr1<0.5;
zr3=imfill(zr2, 'holes');
figure;imshow(zr3); %pause;
roi=regionprops(zr3,'Centroid','PixelList','Area');
ar=[roi.Area];
% find sort order , largest first
[as, ia]=sort(ar(1,:),'descend');
for w=1:length(roi); xy(w,:)=roi(w).Centroid;end
% use sort index to put cenrtoid list in same order
xy1=xy(ia,:);
%and pixel id list
for w=1:length(roi)
roi2(w).PixelList=roi(ia(w)).PixelList;
end
%extract centroid positions as two columns
%SAVE PIXEL LIST FOR EACH ROI IN A SEPARATE FILE
for ww=1:w
k=roi2(ww).PixelList;
%save('onion_PL', 'k');
save(['onion', num2str(ww), '_PL'], 'k'); % Store in multiple files - onion1_PL.mat, onion2_PL.mat, ... onion5_PL.mat
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clear % Use clear for testing - the variables are going to be read from the mat file.
%PL is the PixelList output we got above.
a=imread('onion.png');warning('off', 'Images:initSize:adjustingMag');
nrows = size(a,1);ncols = size(a,2);
zr=ones(nrows,ncols); %matrix of ones
r=a(:,:,1);g=a(:,:,2);b=a(:,:,3); %get RGB values
rr=double(r);gg=double(g);bb=double(b);% convert to double to avoid uint8 sums
grr=rdivide(gg,rr);
PL=dir('*PL.mat'); %load file PixelList files. "dir" is a variable with directory path containing the pixelist files. In this example, we saved "onion_PL.mat"
for m = 1:length(PL)
load(PL(m).name);
ex=[]; %empty matrix to hold the extracted values
%for mm=1:length(k)
%INSERT ANSWER HERE
grr = zeros(nrows, ncols); % Initialize grr with zeros.
for mm = 1:length(k)
x = k(mm, 1); % Get the X (column) coordinate.
y = k(mm, 2); % Get the Y (row) coordinate.
grr(y, x) = gg(y, x) / rr(y, x);
end
% Instead of using a loop, it's more efficient to use sub2ind
if false
% Convert k to linear indices.
kInd = sub2ind([nrows, ncols], k(:,2), k(:,1));
% Update only the indices in the PixelList.
grr(kInd) = rdivide(gg(kInd), rr(kInd));
end
figure;imshow(grr);title(['grr of m=', num2str(m)]) % Show grr for testing.
save(['grr', num2str(m)], 'grr'); % Save grr for testing.
imwrite(imadjust(grr, stretchlim(grr)), ['grr', num2str(m), '.png']); % Store grr as image for testing
end
First two grr matrices as images (used for testing):
grr1.png:
grr2.png:
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)
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)
When using the function regionprops in MATLAB, there is an option to extract the binary image of each connected component. The size of the binary image is reduced to the size of the connected component. I don't want the size of the binary image to reduce. I want the size of the binary image to retain its original size while only showing the selected connected component at its corresponding location in the original image size. How can I extract the connected component in the original image size?
Simply create a blank image that's the same size as your original image and instead of extracting the image per blob, extract the actual pixel locations with reference to the original image for each blob then populate the blank image by setting these locations to binary true in this blank image. Use the PixelIdxList attribute from regionprops to obtain the column major locations of your desired component, then use these to set the output image at these same locations to true.
Assuming your regionprops structure is stored in S and you want to extract out the kth component and the original image is stored in A, do the following:
% Allocate blank image
out = false(size(A, 1), size(A, 2));
% Run regionprops
S = regionprops(A, 'PixelIdxList');
% Determine which object to extract
k = ...; % Fill in ID here
% Obtain the indices
idx = S(k).PixelIdxList;
% Create the mask to be the same size as the original image
out(idx) = true;
imshow(out); % Show the final mask
If you have multiple objects and want to create this mask that is the original size of the image separately per object, you can use a for loop to do that for you:
% Run regionprops
S = regionprops(A, 'PixelIdxList');
% For each blob...
for k = 1 : numel(S)
out = false(size(A, 1), size(A, 2)); % Allocate blank image
% Extract out the kth object's indices
idx = S(k).PixelIdxList;
% Create the mask
out(idx) = true;
% Do your processing with out ...
% ...
end
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,:);.