How to spearate the output of vl_hog() into 6*6*31 blocks and use reshape() to convert every block to a row vector?
2 次查看(过去 30 天)
显示 更早的评论
How to spearate the output of vl_hog() into 6*6*31 blocks,
use reshape() to convert every block to a row vector,
save each vector to features_neg, and increase idx by 1
in the following code:
step=feature_params.hog_cell_size;
idx=0;
for i=1:num_images
im=imread([non_face_scn_path '/' image_files(i).name]);
im=single(im);
hog = vl_hog(im, feature_parames.hog_cell_size, 'verbose');
for m=1:step:size(im,1)
for n=1:step:size(im,2)
%%%%% spearate the output of vl_hog() into 6*6*31 blocks,
%%%%% use reshape() to convert every block to a row vector,
%%%%% save each vector to features_neg, and increase idx by 1
%%%%%
features_neg(idx,:)=reshape(hog,1, []);
idx=idx+1
2 个评论
Walter Roberson
2020-11-26
Do you mean you want each block to be 6 x 6 x 31 ? Or do you mean that you want the code to figure out what block size to use such that you can fit 6 blocks down by 6 blocks across by 31 blocks tall ? Does hog start out as 3 dimensional, or does it start out as a vector? What do you want done if the size of the first dimension is not an exact multiple of 6?
For example if hog is input as 512 x 512, then how many blocks of what size are you expecting as output?
Pooyan Mobtahej
2020-11-26
It is face detection using sliding window and i am trying to fill the function for get random negative features. The size is diffrenet and we use the followng code for sliding window and I want to fill the part that I mentioned, I put my code but don't know if it is correct, you can take a look:
% Starter code prepared by James Hays, Brown University
% This function should return negative training examples (non-faces) from
% any images in 'non_face_scn_path'. Images should be converted to
% grayscale, because the positive training data is only available in
% grayscale. For best performance, you should sample random negative
% examples at multiple scales.
function features_neg = get_random_negative_features(non_face_scn_path, feature_params, num_samples)
% 'non_face_scn_path' is a string. This directory contains many images
% which have no faces in them.
% 'feature_params' is a struct, with fields
% feature_params.template_size (probably 36), the number of pixels
% spanned by each train / test template and
% feature_params.hog_cell_size (default 6), the number of pixels in each
% HoG cell. template size should be evenly divisible by hog_cell_size.
% Smaller HoG cell sizes tend to work better, but they make things
% slower because the feature dimensionality increases and more
% importantly the step size of the classifier decreases at test time.
% 'num_samples' is the number of random negatives to be mined, it's not
% important for the function to find exactly 'num_samples' non-face
% features, e.g. you might try to sample some number from each image, but
% some images might be too small to find enough.
% 'features_neg' is N by D matrix where N is the number of non-faces and D
% is the template dimensionality, which would be
% (feature_params.template_size / feature_params.hog_cell_size)^2 * 31
% if you're using the default vl_hog parameters
% Useful functions:
% vl_hog, HOG = VL_HOG(IM, CELLSIZE)
% http://www.vlfeat.org/matlab/vl_hog.html (API)
% http://www.vlfeat.org/overview/hog.html (Tutorial)
% rgb2gray
image_files = dir(fullfile(non_face_scn_path, '*.jpg'));
num_images = length(image_files);
% placeholder to be deleted
features_neg = rand(100, (feature_params.template_size / feature_params.hog_cell_size)^2 * 31);
step=feature_params.hog_cell_size;
idx=0;
for i=1:num_images
im=imread([non_face_scn_path '/' image_files(i).name]);
%%%%% please follow the steps below to compute HOG features for each image.
%%%%% 1. use single() function to convert input image to SINGLE class;
%%%%% 2. call vl_hog() function, two parameters: one is the image, the
%%%%% other is the feature_parames.hog_cell_size;
%%%%%
im=single(im);
hog = vl_hog(im, feature_parames.hog_cell_size, 'verbose');
for m=1:step:size(im,1)
for n=1:step:size(im,2)
%%%%% spearate the output of vl_hog() into 6*6*31 blocks,
%%%%% use reshape() to convert every block to a row vector,
%%%%% save each vector to features_neg, and increase idx by 1
%%%%% Your code here!
features_neg(idx,:)=reshape(hog, [1, 6*6*31]);
idx=idx+1
end
end
disp(['idex is: ' num2str(idx)]);
disp(['case is: ' num2str(i)]);
end
idx
采纳的回答
Walter Roberson
2020-11-26
编辑:Walter Roberson
2020-11-26
If you have a cell array of blocks, then
vector_blocks_cell = cellfun(@(B) reshape(B,1,[]), non_vector_blocks_cell, 'uniform', 0);
10 个评论
Walter Roberson
2020-11-27
If you were going to do that, then you might as well just use
features_neg(idx,:) = hog(:);
which would write the entire content of hog as a column of features_neg . Also, the double-nested loop would not be effective there.
Perhaps you wanted something like
feature_neg(idx,:) = reshape(hog(m:m+step-1, n:n+step-1, :), [], 1);
Pooyan Mobtahej
2020-11-27
thanks so You mean like this?
and what about spearate the output of vl_hog() into 6*6*31 blocks? should I use [1, 6*6*31]?
for m=1:step:size(im,1)
for n=1:step:size(im,2)
%%%%% spearate the output of vl_hog() into 6*6*31 blocks,
%%%%% use reshape() to convert every block to a row vector,
%%%%% save each vector to features_neg, and increase idx by 1
%%%%% Your code here!
feature_neg(idx,:) = reshape(hog(m:m+step-1, n:n+step-1, :), [], 1);
idx=idx+1
Walter Roberson
2020-11-27
This code already separates hog into blocks that are step x step x whatever so if step were 6 then it would produce 6 x 6 x something , and reshape that into a vector that is (6*6*something x 1)
Pooyan Mobtahej
2020-11-27
so my code is correct?
Please also take a look at the following for runing detector and I shoould do the same and let me know if I should do the same here as well?
% Starter code prepared by James Hays for CS 143, Brown University
% This function returns detections on all of the images in a given path.
% You will want to use non-maximum suppression on your detections or your
% performance will be poor (the evaluation counts a duplicate detection as
% wrong). The non-maximum suppression is done on a per-image basis. The
% starter code includes a call to a provided non-max suppression function.
function [bboxes, confidences, image_ids] = ....
run_detector(test_scn_path, w, b, feature_params)
% 'test_scn_path' is a string. This directory contains images which may or
% may not have faces in them. This function should work for the MIT+CMU
% test set but also for any other images (e.g. class photos)
% 'w' and 'b' are the linear classifier parameters
% 'feature_params' is a struct, with fields
% feature_params.template_size (probably 36), the number of pixels
% spanned by each train / test template and
% feature_params.hog_cell_size (default 6), the number of pixels in each
% HoG cell. template size should be evenly divisible by hog_cell_size.
% Smaller HoG cell sizes tend to work better, but they make things
% slower because the feature dimensionality increases and more
% importantly the step size of the classifier decreases at test time.
% 'bboxes' is Nx4. N is the number of detections. bboxes(i,:) is
% [x_min, y_min, x_max, y_max] for detection i.
% Remember 'y' is dimension 1 in Matlab!
% 'confidences' is Nx1. confidences(i) is the real valued confidence of
% detection i.
% 'image_ids' is an Nx1 cell array. image_ids{i} is the image file name
% for detection i. (not the full path, just 'albert.jpg')
% The placeholder version of this code will return random bounding boxes in
% each test image. It will even do non-maximum suppression on the random
% bounding boxes to give you an example of how to call the function.
% Your actual code should convert each test image to HoG feature space with
% a _single_ call to vl_hog for each scale. Then step over the HoG cells,
% taking groups of cells that are the same size as your learned template,
% and classifying them. If the classification is above some confidence,
% keep the detection and then pass all the detections for an image to
% non-maximum suppression. For your initial debugging, you can operate only
% at a single scale and you can skip calling non-maximum suppression.
test_scenes = dir( fullfile( test_scn_path, '*.jpg' ));
%initialize these as empty and incrementally expand them.
bboxes = zeros(0,4);
confidences = zeros(0,1);
image_ids = cell(0,1);
for i = 1:length(test_scenes)
fprintf('Detecting faces in %s\n', test_scenes(i).name)
img = imread( fullfile( test_scn_path, test_scenes(i).name ));
img = single(img)/255;
if(size(img,3) > 1)
img = rgb2gray(img);
end
index=0;
A=feature_params.hog_cell_size;
cur_bboxes=[];
cur_confidences=[];
cur_image_ids={};
%%%%% JZ: get the test image
origimg=img;
%%%%% JZ: change the resolution of the test image
for x=1:-0.01:0.01
img=imresize(origimg,x);
if size(img,1)>=feature_params.template_size && size(img,2)>=feature_params.template_size
%%%%% please follow the steps below to compute HOG features for each image.
%%%%% 1. use single() function to convert input image to SINGLE class;
%%%%% 2. call vl_hog() function, two parameters: one is the image, the
%%%%% other is the feature_parames.hog_cell_size;
%%%%% Your code here!
im=single(im);
hog = vl_hog(im, feature_parames.hog_cell_size, 'verbose');
rowlimit=size(hog,1); %%%%% JZ: hog is the output of vl_hog();
rollimit=size(hog,2);
for m=1:rowlimit
for n=1:rollimit
if m+feature_params.hog_cell_size-1<=size(hog,1) && n+feature_params.hog_cell_size-1<=size(hog,2)
%%%%% JZ: spearate hog into 6*6*31 blocks,
%%%%% use reshape() to convert every block to a row vector,
%%%%% JZ: Your code here!
%%%%% JZ: For each row do classification using confidences = features*w + b;
confidences_ = features*w + b;
if confidences_>2.5
cur_x_min=1+(m-1)*A;
cur_y_min= 1+(n-1)*A;
if (cur_x_min+feature_params.template_size-1<size(img,1)) && (cur_y_min+feature_params.template_size-1<size(img,2))
index=index+1;
cur_bboxes(index,:) = [cur_y_min, cur_x_min, cur_y_min+feature_params.template_size-1, cur_x_min+feature_params.template_size-1]./x;
cur_confidences(index,:) = confidences_;
cur_image_ids(index,:)={test_scenes(i).name};
end
end
end
end
end
end
end
if ~isempty(cur_bboxes)
[is_maximum] = non_max_supr_bbox(cur_bboxes, cur_confidences, size(origimg));
cur_confidences = cur_confidences(is_maximum,:);
cur_bboxes = cur_bboxes( is_maximum,:);
cur_image_ids = cur_image_ids( is_maximum,:);
bboxes = [bboxes; cur_bboxes];
confidences = [confidences; cur_confidences];
image_ids = [image_ids; cur_image_ids];
end
end
Pooyan Mobtahej
2020-11-27
Did I modify the code correctly ? if not please advise me how to do that?
Walter Roberson
2020-11-27
so my code is correct?
I cannot answer that; I am not familiar with the algorithm, and you have not shown vl_hog code.
Did I modify the code correctly
Your comments talk about breaking hog up into blocks, but you do not access hog inside your loops -- you just test to be sure that accessing within hog would fit if you were to access it (which you do not do.)
cur_bboxes(index,:) = [cur_y_min, cur_x_min, cur_y_min+feature_params.template_size-1, cur_x_min+feature_params.template_size-1]./x;
That appears to be a vector of length 4 being divided by a vector of length 100. That is not compatible sizes. And why would you divide by x anyhow? x has values ranging from 0.01 to 1.00 so that would be scaling the values in the vector to larger values. Why ??
Pooyan Mobtahej
2020-11-27
I am trying to modify face detection project which I will attach the project code for you to check! The i shall get positive and negative random featuresusing HOG and then do SVM classification so please check if I am doing write in codes besides the one that I mentioned
Project:
% Sliding window face detection with linear SVM.
% All code by James Hays, except for pieces of evaluation code from Pascal
% VOC toolkit. Images from CMU+MIT face database, CalTech Web Face
% Database, and SUN scene database.
% Code structure:
% project.m <--- You code parts of this
% + get_positive_features.m <--- You code this
% + get_random_negative_features.m <--- You code this
% [classifier training] <--- You code this
% + report_accuracy.m
% + run_detector.m <--- You code this
% + non_max_supr_bbox.m
% + evaluate_all_detections.m
% + VOCap.m
% + visualize_detections_by_image.m
% + visualize_detections_by_image_no_gt.m
% + visualize_detections_by_confidence.m
% Other functions. You don't need to use any of these unless you're trying
% to modify or build a test set:
% Training and Testing data related functions:
% test_scenes/visualize_cmumit_database_landmarks.m
% test_scenes/visualize_cmumit_database_bboxes.m
% test_scenes/cmumit_database_points_to_bboxes.m %This function converts
% from the original MIT+CMU test set landmark points to Pascal VOC
% annotation format (bounding boxes).
% caltech_faces/caltech_database_points_to_crops.m %This function extracts
% training crops from the Caltech Web Face Database. The crops are
% intentionally large to contain most of the head, not just the face. The
% test_scene annotations are likewise scaled to contain most of the head.
% set up paths to VLFeat functions.
% See http://www.vlfeat.org/matlab/matlab.html for VLFeat Matlab documentation
% This should work on 32 and 64 bit versions of Windows, MacOS, and Linux
close all
clear all
%%%%% JZ: You may need to change the directory below to the directory where you installed VLFeat
run('/Users/pooyan/Documents/computer Vision/vlfeat-0.9.21 3/toolbox/vl_setup')
[~,~,~] = mkdir('visualizations');
%%%%% JZ: You don't need to change anything below (line 47-52)
data_path = '/Users/pooyan/Documents/projectcv/data/'; %change if you want to work with a network copy
train_path_pos = fullfile(data_path, 'caltech_faces/Caltech_CropFaces'); %Positive training examples. 36x36 head crops
non_face_scn_path = fullfile(data_path, 'train_non_face_scenes'); %We can mine random or hard negatives from here
test_scn_path = fullfile(data_path,'test_scenes/test_jpg'); %CMU+MIT test scenes
label_path = fullfile(data_path,'test_scenes/ground_truth_bboxes.txt'); %the ground truth face locations in the test set
%test_scn_path = fullfile(data_path,'test_scenes/test_class'); %Bonus scenes
%label_path = fullfile(data_path,'test_scenes/ground_truth_class_bboxes.txt'); %the ground truth face locations in the test set
%The faces are 36x36 pixels, which works fine as a template size. You could
%add other fields to this struct if you want to modify HoG default
%parameters such as the number of orientations, but that does not help
%performance in our limited test.
feature_params = struct('template_size', 36, 'hog_cell_size', 6);
% Step 1. Load positive training crops and random negative examples
%%%%% JZ: Please open get_positive_features.m file and complete it.
features_pos = get_positive_features( train_path_pos, feature_params );
%%%% JZ: you may change num_negative_examples to see the performance.
num_negative_examples = 10000; %Higher will work strictly better, but you should start with 10000 for debugging
%%%% JZ: Please open get_random_negative_features.m and complete it.
features_neg = get_random_negative_features( non_face_scn_path, feature_params, num_negative_examples);
% step 2. Train Classifier
% Use vl_svmtrain on your training features to get a linear classifier
% specified by 'w' and 'b'
% [w b] = vl_svmtrain(X, Y, lambda)
% http://www.vlfeat.org/sandbox/matlab/vl_svmtrain.html
% 'lambda' is an important parameter, try many values. Small values seem to
% work best e.g. 0.0001, but you can try other values
%
% (1) num_examples is a variable that defines the number of positive examples (face) and negative examples (non-face).
% (2) randomly select num_examples positive examples and num_examples negative examples
% (3) F is the training dataset containing positive and negative examples. Totol 2*num_examples examples.
% (4) Label is the class of each examples in F. Positive examples have class 1 and negative examples have class -1.
num_examples = length(features_pos);
selectedcase = randperm(length(features_pos),num_examples);
features_pos=features_pos(selectedcase,:);
selectedcase = randperm(length(features_neg),num_examples);
features_neg=features_neg(selectedcase,:);
Label=[ones(length(features_pos),1); ones(length(features_neg),1)*-1]';
F=[features_pos; features_neg]';
%%%%% YOUR CODE HERE FOR SVMTRAIN!
%X = [features_pos',features_neg'];
%Y = [ones(size(features_pos,1),1);-ones(size(features_neg,1),1)];
lambda=0.0001;
[w b] = vl_svmtrain(F, Labels, lambda);
%% step 3. Examine learned classifier
%%%%% Don't change this step!
% You don't need to modify anything in this section. The section first
% evaluates _training_ error, which isn't ultimately what we care about,
% but it is a good sanity check. Your training error should be very low.
fprintf('Initial classifier performance on train data:\n')
confidences = [features_pos; features_neg]*w + b;
label_vector = [ones(size(features_pos,1),1); -1*ones(size(features_neg,1),1)];
[tp_rate, fp_rate, tn_rate, fn_rate] = report_accuracy( confidences, label_vector );
% Visualize how well separated the positive and negative examples are at
% training time. Sometimes this can idenfity odd biases in your training
% data, especially if you're trying hard negative mining. This
% visualization won't be very meaningful with the placeholder starter code.
non_face_confs = confidences( label_vector < 0);
face_confs = confidences( label_vector > 0);
figure(2);
plot(sort(face_confs), 'g'); hold on
plot(sort(non_face_confs),'r');
plot([0 size(non_face_confs,1)], [0 0], 'b');
hold off;
% Visualize the learned detector. This would be a good thing to include in
% your writeup!
n_hog_cells = sqrt(length(w) / 31); %specific to default HoG parameters
imhog = vl_hog('render', single(reshape(w, [n_hog_cells n_hog_cells 31])), 'verbose') ;
figure(3); imagesc(imhog) ; colormap gray; set(3, 'Color', [.988, .988, .988])
pause(0.1) %let's ui rendering catch up
hog_template_image = frame2im(getframe(3));
% getframe() is unreliable. Depending on the rendering settings, it will
% grab foreground windows instead of the figure in question. It could also
% return a partial image.
imwrite(hog_template_image, 'visualizations/hog_template.png')
%% Step 4. Run detector on test set.
% YOU CODE 'run_detector'. Make sure the outputs are properly structured!
% They will be interpreted in Step 6 to evaluate and visualize your
% results. See run_detector.m for more details.
%%%% JZ: Please open run_detector.m file and complete it.
[bboxes, confidences, image_ids] = run_detector(test_scn_path, w, b, feature_params);
% run_detector will have (at least) two parameters which can heavily
% influence performance -- how much to rescale each step of your multiscale
% detector, and the threshold for a detection. If your recall rate is low
% and your detector still has high precision at its highest recall point,
% you can improve your average precision by reducing the threshold for a
% positive detection.
%% Step 5. Evaluate and Visualize detections
% These functions require ground truth annotations, and thus can only be
% run on the CMU+MIT face test set. Use visualize_detectoins_by_image_no_gt
% for testing on extra images (it is commented out below).
% Don't modify anything in 'evaluate_detections'!
[gt_ids, gt_bboxes, gt_isclaimed, tp, fp, duplicate_detections] = ...
evaluate_detections(bboxes, confidences, image_ids, label_path);
visualize_detections_by_image(bboxes, confidences, image_ids, tp, fp, test_scn_path, label_path)
% visualize_detections_by_image_no_gt(bboxes, confidences, image_ids, test_scn_path)
% visualize_detections_by_confidence(bboxes, confidences, image_ids, test_scn_path, label_path);
% performance to aim for
% random (stater code) 0.001 AP
% single scale ~ 0.2 to 0.4 AP
% multiscale, 6 pixel step ~ 0.83 AP
% multiscale, 4 pixel step ~ 0.89 AP
% multiscale, 3 pixel step ~ 0.92 AP
Positive Feature:
% Starter code prepared by James Hays, Brown University
% This function should return all positive training examples (faces) from
% 36x36 images in 'train_path_pos'. Each face should be converted into a
% HoG template according to 'feature_params'. For improved performance, try
% mirroring or warping the positive training examples.
function features_pos = get_positive_features(train_path_pos, feature_params)
% 'train_path_pos' is a string. This directory contains 36x36 images of
% faces
% 'feature_params' is a struct, with fields
% feature_params.template_size (probably 36), the number of pixels
% spanned by each train / test template and
% feature_params.hog_cell_size (default 6), the number of pixels in each
% HoG cell. template size should be evenly divisible by hog_cell_size.
% Smaller HoG cell sizes tend to work better, but they make things
% slower because the feature dimensionality increases and more
% importantly the step size of the classifier decreases at test time.
% 'features_pos' is N by D matrix where N is the number of faces and D
% is the template dimensionality, which would be
% (feature_params.template_size / feature_params.hog_cell_size)^2 * 31
% if you're using the default vl_hog parameters
% Useful functions:
% vl_hog, HOG = VL_HOG(IM, CELLSIZE)
% http://www.vlfeat.org/matlab/vl_hog.html (API)
% http://www.vlfeat.org/overview/hog.html (Tutorial)
% rgb2gray
image_files = dir( fullfile(train_path_pos, '*.jpg') ); %Caltech Faces stored as .jpg
num_images = length(image_files); % number of images in the dataset
idx=0;
for i=1:num_images
im=imread([train_path_pos '/' image_files(i).name]);
%%%%% please follow the steps below to compute HOG features for each image.
%%%%% 1. use single() function to convert input image to SINGLE class;
%%%%% 2. call vl_hog() function, two parameters: one is the image, the
%%%%% other is the feature_parames.hog_cell_size;
%%%%% 3. use reshape() function to convert the output of vl_hog() to a
%%%%% row vector;
%%%%% 4. add the row vector to features_pos and increase idx by 1;
%%%%% 5. use fliplr() function to flip the input image and repeat steps
%%%%% 2, 3, 4;
%%%%% YOUR CODE HERE!
im=single(im);
hog = vl_hog(im, feature_parames.hog_cell_size, 'verbose') ;
%idx=idx+1;
features_pos(idx,:)=reshape(hog,1,[]);
idx=idx+1;
im=fliplr(im);
hog = vl_hog(im, feature_parames.hog_cell_size, 'verbose') ;
features_pos(idx,:)=reshape(hog,1,[]);
end
Negative:
% Starter code prepared by James Hays, Brown University
% This function should return negative training examples (non-faces) from
% any images in 'non_face_scn_path'. Images should be converted to
% grayscale, because the positive training data is only available in
% grayscale. For best performance, you should sample random negative
% examples at multiple scales.
function features_neg = get_random_negative_features(non_face_scn_path, feature_params, num_samples)
% 'non_face_scn_path' is a string. This directory contains many images
% which have no faces in them.
% 'feature_params' is a struct, with fields
% feature_params.template_size (probably 36), the number of pixels
% spanned by each train / test template and
% feature_params.hog_cell_size (default 6), the number of pixels in each
% HoG cell. template size should be evenly divisible by hog_cell_size.
% Smaller HoG cell sizes tend to work better, but they make things
% slower because the feature dimensionality increases and more
% importantly the step size of the classifier decreases at test time.
% 'num_samples' is the number of random negatives to be mined, it's not
% important for the function to find exactly 'num_samples' non-face
% features, e.g. you might try to sample some number from each image, but
% some images might be too small to find enough.
% 'features_neg' is N by D matrix where N is the number of non-faces and D
% is the template dimensionality, which would be
% (feature_params.template_size / feature_params.hog_cell_size)^2 * 31
% if you're using the default vl_hog parameters
% Useful functions:
% vl_hog, HOG = VL_HOG(IM, CELLSIZE)
% http://www.vlfeat.org/matlab/vl_hog.html (API)
% http://www.vlfeat.org/overview/hog.html (Tutorial)
% rgb2gray
image_files = dir(fullfile(non_face_scn_path, '*.jpg'));
num_images = length(image_files);
% placeholder to be deleted
features_neg = rand(100, (feature_params.template_size / feature_params.hog_cell_size)^2 * 31);
step=feature_params.hog_cell_size;
idx=0;
for i=1:num_images
im=imread([non_face_scn_path '/' image_files(i).name]);
%%%%% please follow the steps below to compute HOG features for each image.
%%%%% 1. use single() function to convert input image to SINGLE class;
%%%%% 2. call vl_hog() function, two parameters: one is the image, the
%%%%% other is the feature_parames.hog_cell_size;
%%%%% Your code here!
im=single(im);
hog = vl_hog(im, feature_parames.hog_cell_size, 'verbose');
for m=1:step:size(im,1)
for n=1:step:size(im,2)
%%%%% spearate the output of vl_hog() into 6*6*31 blocks,
%%%%% use reshape() to convert every block to a row vector,
%%%%% save each vector to features_neg, and increase idx by 1
%%%%% Your code here!
features_neg(idx,:)=reshape(hog, [1, 6*6*31]);
idx=idx+1
end
end
disp(['idex is: ' num2str(idx)]);
disp(['case is: ' num2str(i)]);
end
idx
Walter Roberson
2020-11-28
I have told you multiple times that
features_neg(idx,:)=reshape(hog, [1, 6*6*31]);
is not at all correct.
更多回答(2 个)
Pooyan Mobtahej
2020-11-28
i have modified as follows and got error again :
idx=0;
for i=1:num_images
im=imread([non_face_scn_path '/' image_files(i).name]);
%%%%% JZ: please follow the steps below to compute HOG features for each image.
%%%%% 1. use single() function to convert input image to SINGLE class;
%%%%% 2. call vl_hog() function, two parameters: one is the image, the
%%%%% other is the feature_parames.hog_cell_size;
%%%%% JZ: Your code here!
im=single(im);
hog = vl_hog(im, feature_params.hog_cell_size);
for m=1:step:size(im,1)
for n=1:step:size(im,2)
%%%%% JZ: spearate the output of vl_hog() into 6*6*31 blocks,
%%%%% use reshape() to convert every block to a row vector,
%%%%% save each vector to features_neg, and increase idx by 1
%%%%% JZ: Your code here!
idx=idx+1
% feature_neg(idx,:)=reshape(hog,6,6,[]);
% features_neg(idx,:) = hog(:);
feature_neg(idx,:) = reshape(hog(m-step+1:m, n-step+1:n, :),[],1);
%idx=idx+1
1 个评论
Walter Roberson
2020-11-28
Your m starts at 1 and your n starts at 1. Suppose step is 6. Then you try to index hog as hog(1-6+1:1, 1-6+1:1, :) which would be hog(-4:1, -4:1, :) which would use invalid negative indices.
I told you to use + not - and the order was reversed hog(m:m+step-1, n:n+step-1, :) which would be hog(1:1+6-1, 1:1+6-1,:) which would be hog(1:6, 1:6, :)
for m=1:step:size(im,1)
You need to stop at the point where m+step-1 does not exceed size(im,1) .
m+step-1 <= size(im,1)
so m <= size(im,1) - step + 1
Pooyan Mobtahej
2020-11-28
I revised that
im=single(im);
hog = vl_hog(im, feature_params.hog_cell_size);
for m=1:step:size(im,1)
for n=1:step:size(im,2)
%%%%% JZ: spearate the output of vl_hog() into 6*6*31 blocks,
%%%%% use reshape() to convert every block to a row vector,
%%%%% save each vector to features_neg, and increase idx by 1
%%%%% JZ: Your code here!
idx=idx+1
% feature_neg(idx,:)=reshape(hog,6,6,[]);
% features_neg(idx,:) = hog(:);
feature_neg(idx,:) = reshape(hog(m:m+step-1, n:n+step-1, :),[],1);
%idx=idx+1
end
end
disp(['idex is: ' num2str(idx)]);
disp(['case is: ' num2str(i)]);
end
idx
still this error:
Index in position 2 exceeds array bounds (must not exceed 43).
Error in get_random_negative_features (line 66)
feature_neg(idx,:) = reshape(hog(m:m+step-1, n:n+step-1, :),[],1);
Error in project (line 71)
features_neg = get_random_negative_features( non_face_scn_path, feature_params, num_negative_examples);
2 个评论
Walter Roberson
2020-11-28
As I wrote,
You need to stop at the point where m+step-1 does not exceed size(im,1) .
And I posted the logic that shows you how to calculate the upper bound.
Pooyan Mobtahej
2020-11-29
can once again explain how to :
stop at the point where m+step-1 does not exceed size(im,1) .
should I modify loop? or anything in reshape part?
另请参阅
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!发生错误
由于页面发生更改,无法完成操作。请重新加载页面以查看其更新后的状态。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom(English)
亚太
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)