当前位置:网站首页>Opencv notes 20 PCA
Opencv notes 20 PCA
2022-07-03 10:03:00 【Σίσυφος one thousand and nine hundred】
One 、 Introduction and practical application
PCA It is mainly used to obtain the main direction of the object and reduce the dimension of the data .
PCA The main idea of is to find several important characteristics that can reflect the characteristics in a pile of dimensional data , So it can reduce the amount of calculation , Eliminate those attributes that are not particularly important from these data .
Two 、 Deduction of mathematical principles
3、 ... and 、 opencv Medium PCA class
PCA::PCA(InputArray data, InputArray mean, int flags, int maxComponents=0)
data // input data ( It can be a set of contour points )
mean // Data zero mean , It's empty (Mat()) It's calculated automatically
flag // Represents the way data is provided (0 Represents input by line ,1 It means input by column )
maxComponents // How many eigenvalues to keep ( By default, all are reserved )
- Original image , Project into a new space
Mat PCA::project(InputArray vec) const
- Conduct project Later data , Reflect the original image
Mat PCA::backProject(InputArray vec) const
The variable values are :mean-------- The mean of the raw data
eigenvalues-------- The eigenvalues of the covariance matrix
eigenvectors-------- Eigenvector
Four 、 Code shows
opencv actual combat ——PCA Application of algorithm - Only you are strong - Blog Garden
#if 1 // PCA Algorithm Algorithm
// Get the main direction of building
double getOrientation(vector<Point>& contour, Mat& img)
{
// structure pca data . What we're doing here is we're going to take the contour points x and y As two dimensions press into data_pts In the middle .
Mat data_pts = Mat(contour.size(), 2, CV_64FC1);// Use mat To save the data , It's also for the back pca Processing needs
for (int i = 0; i < data_pts.rows; ++i)
{
data_pts.at<double>(i, 0) = contour[i].x;
data_pts.at<double>(i, 1) = contour[i].y;
}
// perform PCA analysis
PCA pca_analysis(data_pts, Mat(), 0);
// Get the most important component ( mean value ), In this case , It corresponds to the midpoint of the contour , It's also the midpoint of the image
Point pos = Point(pca_analysis.mean.at<double>(0, 0), pca_analysis.mean.at<double>(0, 1));
// Store eigenvectors and eigenvalues
vector<Point2d> eigen_vecs(2);
vector<double> eigen_val(2);
for (int i = 0; i < 2; ++i)
{
eigen_vecs[i] = Point2d(pca_analysis.eigenvectors.at<double>(i, 0), pca_analysis.eigenvectors.at<double>(i, 1));
eigen_val[i] = pca_analysis.eigenvalues.at<double>(i, 0);// In the outline / Draw a small circle at the midpoint of the image
circle(img, pos, 3, CV_RGB(255, 0, 255), 2);
// Calculate the straight line , Draw a line in the main direction ( Each eigenvector is multiplied by its eigenvalue and converted to the average position . There is one 0.02 The scaling factor of , It's just to make sure the vector fits the image and doesn't 10000 The length of the pixel )
line(img, pos, pos + 0.02 * Point(eigen_vecs[0].x * eigen_val[0], eigen_vecs[0].y * eigen_val[0]), CV_RGB(255, 255, 0));
line(img, pos, pos + 0.02 * Point(eigen_vecs[1].x * eigen_val[1], eigen_vecs[1].y * eigen_val[1]), CV_RGB(0, 255, 255));
// Finally calculate and return the strongest ( It has the largest eigenvalue ) The angle of the eigenvector of
return atan2(eigen_vecs[0].y, eigen_vecs[0].x);
}
}
int main(int argc, char** argv)
{
Mat src = imread("C:\\Users\\19473\\Desktop\\opencv_images\\600.png");
imshow(" The input image ", src);
Mat gray, binary;
cvtColor(src, gray, COLOR_BGR2GRAY);
// Threshold processing
threshold(gray, binary, 150, 255, THRESH_BINARY);
imshow(" Two valued ", binary);
//1 、 Look for the outline
vector<vector<Point> > contours;
vector<Vec4i> hierarchy;
findContours(binary, contours, hierarchy, RETR_LIST, CHAIN_APPROX_NONE);
// Profile analysis , Find the artifact
for (size_t i = 0; i < contours.size(); ++i)
{
// Calculate the contour size
double area = contourArea(contours[i]);
// Remove small or large contour areas ( Scientific notation means le2 Express 1X10 Of 2 Power )
if (area < 1e2 || 1e4 < area) continue;
// Draw the outline
drawContours(src, contours, i, Scalar(0, 0, 255), 2, 8, hierarchy, 0);
// Look for the direction of each profile
double angle = getOrientation(contours[i], src);
cout << angle << endl;
}
imshow(" result ", src);
waitKey(0);
return 0;
}
#endif
边栏推荐
- QT qcombobox QSS style settings
- LeetCode - 673. 最长递增子序列的个数
- Working mode of 80C51 Serial Port
- 4G module initialization of charge point design
- Openeuler kernel technology sharing - Issue 1 - kdump basic principle, use and case introduction
- 4G module at command communication package interface designed by charging pile
- el-table X轴方向(横向)滚动条默认滑到右边
- LeetCode - 919. 完全二叉树插入器 (数组)
- 2312、卖木头块 | 面试官与狂徒张三的那些事(leetcode,附思维导图 + 全部解法)
- Stm32f407 key interrupt
猜你喜欢
手机都算是单片机的一种,只不过它用的硬件不是51的芯片
内存数据库究竟是如何发挥内存优势的?
Oracle数据库 SQL语句执行计划、语句跟踪与优化实例
An executable binary file contains more than machine instructions
Comment la base de données mémoire joue - t - elle l'avantage de la mémoire?
要選擇那種語言為單片機編寫程序呢
Mysql database underlying foundation column
学习开发没有捷径,也几乎不存在带路会学的快一些的情况
RESNET code details
2021-10-27
随机推荐
Emballage automatique et déballage compris? Quel est le principe?
Embedded systems are inherently flawed. Compared with the Internet, there are so many holes that it is simply difficult to walk away from
STM32 interrupt switch
My notes on the development of intelligent charging pile (III): overview of the overall design of the system software
2020-08-23
Not many people can finally bring their interests to college graduation
[Li Kou brush question notes (II)] special skills, module breakthroughs, classification and summary of 45 classic questions, and refinement in continuous consolidation
I didn't think so much when I was in the field of single chip microcomputer. I just wanted to earn money to support myself first
Getting started with JMX, MBean, mxbean, mbeanserver
Wireshark use
Drive and control program of Dianchuan charging board for charging pile design
[combinatorics] Introduction to Combinatorics (combinatorial idea 3: upper and lower bound approximation | upper and lower bound approximation example Remsey number)
51 MCU tmod and timer configuration
学历是一张通行证,门票,你有了它,可以踏入更高层次的环境里
byte alignment
Open Euler Kernel Technology Sharing - Issue 1 - kdump Basic Principles, use and Case Introduction
MySQL 数据库基础知识(系统化一篇入门)
Design of charging pile mqtt transplantation based on 4G EC20 module
4G module designed by charging pile obtains signal strength and quality
Education is a pass and ticket. With it, you can step into a higher-level environment