基于Opencv的SIFT、SURF、HOG的实现

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;
}

在这里插入图片描述

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值