基于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
    评论
好的,我可以帮您回答这个问题。基于OpenCV的车辆识别可以分为以下几个步骤: 1. 数据集准备:首先需要收集大量的车辆图片作为训练数据,并标注每张图片中车辆的位置。可以使用一些开源的数据集,例如KITTI、PASCAL VOC、COCO等。 2. 特征提取:使用OpenCV提供的特征提取算法,例如HOGSURFSIFT等,从车辆图片中提取特征。 3. 训练模型:使用机器学习算法,例如SVM、神经网络等,对提取的特征进行训练,得到车辆识别模型。 4. 目标检测:使用训练好的模型对新的车辆图片进行识别,检测出车辆的位置。 下面是一个简单的基于OpenCV的车辆识别代码示例: ```python import cv2 # 加载训练好的车辆识别模型 car_cascade = cv2.CascadeClassifier('cars.xml') # 加载测试图片 img = cv2.imread('test.jpg') # 转换为灰度图像 gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # 检测车辆 cars = car_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30)) # 在图片中标记车辆位置 for (x, y, w, h) in cars: cv2.rectangle(img, (x, y), (x+w, y+h), (0, 0, 255), 2) # 显示图片 cv2.imshow('Car detection', img) cv2.waitKey(0) cv2.destroyAllWindows() ``` 其中,`cars.xml`是训练好的车辆识别模型,`test.jpg`是待识别的车辆图片。`detectMultiScale`方法是OpenCV提供的目标检测算法,可以检测出图片中的车辆位置。最后,通过`rectangle`方法在图片中标记出车辆位置,并将结果显示出来。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值