SIFT实现代码:
#include<opencv2/opencv.hpp>
#include<opencv2/xfeatures2d.hpp>
#include<iostream>
using namespace std;
using namespace cv;
using namespace cv::xfeatures2d;
int main(int argc, char** argv)
{
Mat src = imread("D:/ptext/girl.jpg");
if (src.empty())
{
printf("could not load image...\n");
return -1;
}
namedWindow("input image", CV_WINDOW_AUTOSIZE);
imshow("input image", src);
int numFeatures = 400;
Ptr<SIFT>detector = SIFT::create(numFeatures);
vector<KeyPoint>keypoints;
detector->detect(src, keypoints, Mat());
printf("Total KeyPoints:%d\n", keypoints.size());
Mat keypoint_img;
drawKeypoints(src, keypoints, keypoint_img, Scalar::all(-1), DrawMatchesFlags::DEFAULT);
namedWindow("SIFT KeyPoints", CV_WINDOW_AUTOSIZE);
imshow("SIFT KeyPoints", keypoint_img);
waitKey(0);
return 0;
}
SURF的实现:
#include<opencv2/opencv.hpp>
#include<opencv2/xfeatures2d.hpp>
#include<iostream>
using namespace std;
using namespace cv;
using namespace cv::xfeatures2d;
int main(int argc, char** argv)
{
Mat src = imread("D:/ptext/girl.jpg",IMREAD_GRAYSCALE);
if (src.empty())
{
printf("could not load image...\n");
return -1;
}
namedWindow("input image", CV_WINDOW_AUTOSIZE);
imshow("input image", src);
//SURF特征检测
int minHessian = 400;
Ptr<SURF>detector = SURF::create(minHessian,4,3,false,false);
vector<KeyPoint> keypoints;
detector->detect(src, keypoints, Mat());
//绘制关键点
Mat keypoint_img;
drawKeypoints(src, keypoints, keypoint_img, Scalar::all(-1), DrawMatchesFlags::DEFAULT);
imshow("KeyPoints Image", keypoint_img);
waitKey(0);
return 0;
}
用Opencv自带的行人检测库实现行人检测:
#include<opencv2/opencv.hpp>
#include<iostream>
using namespace std;
using namespace cv;
int main(int argc,char** argv)
{
Mat src;
src = imread("D:/ptext/people.jpg");
if (src.empty())
{
printf("could not load image...\n");
return -1;
}
namedWindow("input image", CV_WINDOW_AUTOSIZE);
imshow("input image", src);
/*
//验证:对于64*128的像素块,可以分为8*16个Cell,分为7*15个块,总计直方图向量数为:7*15*2*2*9=3780个向量组
Mat dst, dst_gray;
resize(src, dst, Size(64, 128));
cvtColor(dst, dst_gray, COLOR_BGR2GRAY);
HOGDescriptor detector(Size(64, 128), Size(16, 16), Size(8, 8), Size(8, 8), 9);
vector<float>desciptors;
vector<Point>locations;
detector.compute(dst_gray, desciptors, Size(0, 0), Size(0, 0), locations);
printf("number of HOG descriptors:%d", desciptors.size());
*/
HOGDescriptor hog=HOGDescriptor();
hog.setSVMDetector(hog.getDefaultPeopleDetector());
vector<Rect>foundLocaions;
hog.detectMultiScale(src, foundLocaions, 0, Size(8, 8), Size(32, 32), 1.05, 2);
Mat result = src.clone();
for (size_t t= 0;t < foundLocaions.size();t++)
{
rectangle(result, foundLocaions[t], Scalar(0, 0, 255), 2, 8, 0);
}
namedWindow("HOG SVM Detector Demo", CV_WINDOW_AUTOSIZE);
imshow("HOG SVM Detector Demo", result);
waitKey(0);
return 0;
}