翻译自原文:
目标检测是一个程序,他用来确定图像的某个区域是不是含有要进行识别的对象,对象识别是程序识别对象的能力。识别通常是只处理已经检测到对象的区域,例如,人们总是会在有人脸图像的地方去识别人脸。
一般来说会用到下面的三种技术
1.梯度直方图
2.图像金字塔
3.滑动窗口
行了,话不多说。咱们不是来研究这些东西的,只是为了应用罢了,直接来上一个例子,下面的是c++的demo
#include<opencv2/opencv.hpp>
#include<vector>
using namespace cv;
using namespace std;
int main()
{
Mat img;
vector<Rect> found;
img = imread("2.jpg");
if (img.empty())
{
printf("没有图片\n");
return -1;
}
HOGDescriptor defaultHog;
defaultHog.setSVMDetector(HOGDescriptor::getDefaultPeopleDetector());
//进行检测
defaultHog.detectMultiScale(img, found);
//画长方形,框出行人
for (int i = 0; i < found.size(); i++)
{
Rect r = found[i];
rectangle(img, r.tl(), r.br(), Scalar(0, 0, 255), 3);
}
namedWindow("检测行人", CV_WINDOW_AUTOSIZE);
imshow("检测行人", img);
waitKey(0);
return 0;
}
发完了c++的demo,有的小伙伴说c++看不懂,那没关系,咱们再来一种语言,那就是时下最流行的Python语言
import cv2
import numpy as np
def is_inside(o, i):
ox, oy, ow, oh = o
ix, iy, iw, ih = i
return ox > ix and oy > iy and ox + ow < ix + iw and oy + oh < iy + ih
def draw_person(image, person):
x, y, w, h = person
cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 255), 2)
img = cv2.imread('2.jpg')
# a, b = img.shape[:2]
# img = cv2.resize(img, (a//5, b//5))
hog = cv2.HOGDescriptor()
hog.setSVMDetector(cv2.HOGDescriptor_getDefaultPeopleDetector())
found, w = hog.detectMultiScale(img)
found_filtered = []
for ri, r in enumerate(found):
for qi, q in enumerate(found):
if ri != qi and is_inside(r, q):
break
else:
found_filtered.append(r)
for person in found_filtered:
draw_person(img, person)
cv2.imshow('people detection', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
发完了代码,接下来就是看看demo的运行结果是什么了,先来发一下c++的代码的结果
接下来再来一下py的代码的结果
总之呢,今天的文章就到这里了,代码有什么不明白了,请给我留言,我会尽我所能的给你解答,但是本人水平有限不能保障所有的问题都能解答,或者是本人的时间比较紧迫,有时候会看不见消息,请见谅。