当前位置:网站首页>PCA dimension reduction application of OpenCV (I)

PCA dimension reduction application of OpenCV (I)

2022-06-21 14:31:00 Modest learning and progress

About opencv Inside PCA Dimension reduction
Today, I finally opencv The built-in dimension reduction function runs through , It took me a day ,,
First of all, thank the author of this blog :
http://blog.codinglabs.org/articles/pca-tutorial.html
I've seen him “PCA The mathematical principle of ”, I really feel enlightened , Simplify the profound mathematical principles , The narration is vivid and clear .


All right. , Let's get to the point :
First, I extracted before dimensionality reduction VLAD Is characterized by 60*64 Dimensional , namely 3840 dimension , My picture library has 5063 A picture , So before dimensionality reduction, my image library VLAD Is characterized by 5063*3840, Let's give him a name , It's called VLAD_all, yes 5063 That's ok ,3840 Oh , Don't confuse rows and columns , I want to put 3840 The characteristic of dimension is reduced to 128 dimension , That is to reduce 30 times ,
Finally, my image library features become 5063*128, All right. , Look at the code :
PCA pca(before_PCA, Mat(), CV_PCA_DATA_AS_ROW, 128);
before_PCA What is stored is the library features before dimensionality reduction , That's it. 5063*3840, You can just bring it in here without transposing , After this sentence , The eigenvector after dimensionality reduction is 128*3840 Of ( yes 128 That's ok ,3840 Column , I wrote the following strictly in the order of rows and columns , I won't go into details )
namely Mat eigenvectors = pca.eigenvectors.clone(); 
eigenvectors That's what's in it 128*3840 Eigenvector of , Note that this matrix is not the result of dimensionality reduction , It is just an eigenvector matrix generated in the middle , This matrix is very important , Please refer to the above for details “PCA The mathematical principle of ”,
All right. , Let's start with the most important :
Mat VLAD_all_pca = eigenvectors*VLAD_all.t();
Notice I'm using theta VLAD_all Transpose of , Otherwise, if the rows and columns of matrix multiplication do not correspond to each other, it will be interrupted , Let's analyze the most important code in this line :
eigenvectors yes 128*3840 Eigenvector of ,VLAD_all.t() yes 3840*5063 The library of VLAD Transpose of features , So it just corresponds to ,eigenvectors yes 3840 Column ,VLAD_all.t() yes 3840 That's ok , It can be multiplied correspondingly ,, All right. , Let's guess the result of multiplication VLAD_all_pca How many rows and columns of data are stored ,
you 're right , This VLAD_all_pca Namely 128*5063( Transpose as appropriate , I have suffered a loss in the transposition ), Now we use it as the new VLAD features , This is what I said PCA Real image library features after dimensionality reduction . Is it better than before 5063*3840 Much less , The original 5063*3840xml File is 327M,
After the dimension reduction VLAD_all_pca Of xml File is 10.9M, Yes , The small 30 times , That is to say, the speed is increased 30 times ( Of course, this is a rude statement. In fact, there will be a lot of other time expenses ).
 

//**************************** Original picture , Read picture path , read xml The file path needs to be changed in three places , also  pictures_num****************************
 
#include <boost/lexical_cast.hpp>       
#include <iostream>    
#include <string>
#include <iostream>
#include <vector>
#include <boost/filesystem.hpp>
#include <opencv/cv.h>
#include <opencv/highgui.h>
#include <opencv/ml.h>
#include <opencv2/nonfree/features2d.hpp>
#include <string>
#include <iostream>
#include <vector>
#include <boost/filesystem.hpp>
#include <opencv/cv.h>
#include <opencv/highgui.h>
#include <opencv/ml.h>
#include <opencv2/nonfree/features2d.hpp>
using namespace boost::filesystem;
using namespace std;
using namespace cv;
//#pragma comment( lib, "opencv_highgui249d.lib")
//#pragma comment( lib, "opencv_core49d.lib")
// Global variables 
#define numClusters 60 // Number of cluster centers , If the Extract_VLAD_Features function , stay surf Next numClusters No more than 64, stay sift Under the circumstances numClusters It should not be greater than 128
Mat Descriptor_all(0, 64, CV_32FC1);
Mat VLAD_all(0, 64 * numClusters, CV_32FC1);// Store the overall picture library VLAD features ,
Mat centers(numClusters, 64, CV_32FC1);// In fact, the centers It is possible to define no size , After clustering centers It automatically stores the clustering results  centers(numClusters, 128, CV_32FC1);
Mat labels(0, numClusters, CV_32FC1);
 
Mat Extract_VLAD_Features(Mat &descriptors, Mat ¢ers, float alpha = 1);
float ComputeSimilarity(Mat vlad_feat_query, Mat vlad_feat_train);
void Extractor_allDescriptors(path&basepath);
void Extractor_allVLAD(char filePath[150], int n_picture);//  Read  n_picture  A picture 
 
 
 
int main(){
	double time = static_cast<double>(getTickCount());
 
	FileStorage fsd("D:/kmeans_data/centers60_64_74picture.xml", FileStorage::READ);
	fsd["centers"] >> centers;
	fsd.release();
 
	// Read library pictures VLAD  Eigenvector 
	double timedata = static_cast<double>(getTickCount());
	FileStorage fvlad("D:/kmeans_data/all_VLAD5063.xml", FileStorage::READ);
	fvlad["VLAD_all"] >> VLAD_all;
	fvlad.release();
 
	timedata = ((double)getTickCount() - timedata) / getTickFrequency();
	cout << " read VLAD_all Data time :" << timedata << " second " << endl << endl;
 
	 Dimensionality reduction , Generate the eigenvectors required for dimensionality reduction :
	//double PCA_time = static_cast<double>(getTickCount());
	//int pca_num = 128;// Down to 128 dimension 
	//Mat before_PCA = VLAD_all.clone();//before_PCA Used to store dimension features before dimension reduction , It is VLAD_all The transpose , namely before_PCA yes 60*64 That's ok ,5063 Column ,5063 yes fvlad Number of library features read , How many pictures are there in the gallery before_PCA How many columns are there ,
	//Mat after_PCA_eigenvectors;// Store the reduced dimension eigenvector 
	//cout << " Underway PCA Dimension reduction ..." << endl;
	//PCA pca(before_PCA, Mat(), CV_PCA_DATA_AS_ROW, pca_num);
	//Mat eigenvectors = pca.eigenvectors.clone();//eigenvectors It is used to store the reduced feature vector 
	//PCA_time = ((double)getTickCount() - PCA_time) / getTickFrequency();
	//cout << "PCA Dimensionality reduction time :" << PCA_time << " second " << endl << endl;
	//FileStorage f_pca("D:/kmeans_data/after_PCA_eigenvectors.xml", FileStorage::WRITE);
	//f_pca << "after_PCA_eigenvectors" << eigenvectors;
	//f_pca.release();
 
	 Read the above eigenvector , Real dimensionality reduction processing for input data :
	double time_T = static_cast<double>(getTickCount());
	Mat after_PCA_eigenvectors;// Store the reduced dimension eigenvector 
	FileStorage f_pca("D:/kmeans_data/after_PCA_eigenvectors.xml", FileStorage::READ);
	f_pca["after_PCA_eigenvectors"] >> after_PCA_eigenvectors;
	f_pca.release();
	Mat VLAD_all_pca = after_PCA_eigenvectors*VLAD_all.t();//VLAD_all_pca It is used to store the real reduced dimension  all_VLAD5063  data 
 
	FileStorage fs_pca("D:/kmeans_data/VLAD_all_pca.xml", FileStorage::WRITE);
	fs_pca << "VLAD_all_pca" << VLAD_all_pca;
	fs_pca.release();
	cout << " Dimension reduced data has been extracted successfully !" << endl;
 
	time_T = ((double)getTickCount() - time_T) / getTickFrequency();
	cout << " Read feature 、PCA Matrix multiplication time :" << time_T << " second " << endl << endl;
 
	time = ((double)getTickCount() - time) / getTickFrequency();
	cout << " Total time :" << time << " second " << endl << endl;
 
	system("pause");
	return 0;
}
 

Let's see the effect : The three pictures at the bottom right are 5063 The three pictures most similar to the original one found in the cool pictures ,

The first is the recognition result before dimensionality reduction , The last two pictures are the recognition results after dimension reduction , You can see the features from 3840 Dimension down to 128 The recognition effect seems to be very good after the dimension , Careful students can see my data , The recognition speed before dimensionality reduction is about 9.8S, After the dimension reduction 1.3S, Of course, this also includes reading files , Many links can be optimized .

Actually, I've referred to this before :http://blog.csdn.net/u010555682/article/details/52563970

I just feel that the rows and columns of the feature vectors extracted by him are different from mine , Maybe I didn't understand it thoroughly , Children's shoes are afraid of being around, so don't look at him .

Mr. , by the way , About VLAD Feature image retrieval can refer to the article written by this person , Although I don't like the author , But the article still has reference value :

http://www.cnblogs.com/mafuqiang/p/6909556.html

If you want to study image retrieval in detail, you can see this master's thesis , Well written : be based on GMM_VLAD Image retrieval of _ Chenmanyu
from :https://blog.csdn.net/guanyonglai/article/details/78418078#commentBox

原网站

版权声明
本文为[Modest learning and progress]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202221424252477.html