In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-05 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly explains "C++ how to use opencv to achieve face detection function", interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn how C++ uses opencv to achieve face detection.
When I install opencv under the Linux system, I will talk about it again to prevent some people from not installing the program that has not been debugged and spraying it.
Sudo apt-get install libcv-dev
Sudo apt-get install libopencv-dev
Check to see if there are several training set .XML files in your usr/share/opencv/haarcascades directory. Next, I'll take face and eye detection as an example. The program is as follows:
Many people don't know how to compile opencv. Let me write a few more sentences to solve the difficulties of many rookies.
After copy finishes the code, save it as xiaorun.cpp. Remember to compile and try out a xiaorun.cpp +-o xiaorun. / xiaorun.cpp-lopencv_highgui-lopenc_imgproc-lopencv_core-lopencv_objdetect.
It can be realized.
# include # include using namespace cv;using namespace std;void detectAndDraw (Mat& img, CascadeClassifier& cascade, CascadeClassifier& nestedCascade, double scale, bool tryflip); int main () {CascadeClassifier cascade, nestedCascade; bool stop = false; cascade.load ("/ usr/share/opencv/haarcascades/haarcascade_frontalface_alt.xml"); nestedCascade.load ("/ usr/share/opencv/haarcascades/haarcascade_eye.xml"); / / frame = imread ("renlian.jpg") VideoCapture cap (0); / / Open the default camera if (! cap.isOpened ()) {return-1;} Mat frame; Mat edges;while (! stop) {cap > > frame; detectAndDraw (frame, cascade, nestedCascade,2,0); if (waitKey (30) > = 0) stop = true; imshow ("cam", frame);} / / CascadeClassifier cascade, nestedCascade; / / bool stop = false / / name of the trained file, put it in the same directory as the executable file / / cascade.load ("/ usr/share/opencv/haarcascades/haarcascade_frontalface_alt.xml"); / / nestedCascade.load ("/ usr/share/opencv/haarcascades/aarcascade_eye.xml"); / / frame = imread ("renlian.jpg"); / / detectAndDraw (frame, cascade, nestedCascade,2,0); / / waitKey (); / / while (! stop) / / {/ / cap > > frame / / detectAndDraw (frame, cascade, nestedCascade,2,0); if (waitKey (30) > = 0) stop = true; / /} return 0;} void detectAndDraw (Mat& img, CascadeClassifier& cascade, CascadeClassifier& nestedCascade, double scale, bool tryflip) {int I = 0; double t = 0; / / create a vector container vector faces, faces2 for storing faces / / define some colors that can be used to mark different faces const static Scalar colors [] = {CV_RGB (0mem0255), CV_RGB (0128255), CV_RGB (0255255), CV_RGB (0pr 255re0), CV_RGB (255pr 128rem 0), CV_RGB (255pr 255re0), CV_RGB (255pr 0men0), CV_RGB (255pr 0255)} / / create a scaled-down image to speed up the detection / / nt cvRound (double value) rounds a round number and returns an integer! Mat gray, smallImg (cvRound (img.rows/scale), cvRound (img.cols/scale), CV_8UC1); / convert to grayscale image, Harr features are based on grayscale image cvtColor (img, gray, CV_BGR2GRAY); / / imshow ("grayscale", gray); / / resize the image, using bilinear difference resize (gray, smallImg, smallImg.size (), 0,0, INTER_LINEAR); / / imshow ("reduce size", smallImg) / / the transformed image is processed by histogram averaging equalizeHist (smallImg, smallImg); / / imshow ("histogram mean processing", smallImg); / / the start and end of the program are inserted into this function to obtain the time, and the algorithm execution time t = (double) cvGetTickCount () is calculated. / / detect human face / / smallImg in the detectMultiScale function indicates that the input image to be detected is smallImg,faces to represent the detected face target sequence, and 1.1 indicates that the proportion of each image size reduction is 1.1 2 means that each target must be detected at least three times before it can be considered a real target (because faces can be detected by surrounding pixels and different window sizes / sizes). CV_HAAR_SCALE_IMAGE means that it is not to scale the classifier to detect, but to scale the image. Size (30,30) as the target / / minimum maximum size cascade.detectMultiScale (smallImg, faces, 1.1,2,0 / | CV_HAAR_FIND_BIGGEST_OBJECT / / | CV_HAAR_DO_ROUGH_SEARCH | CV_HAAR_SCALE_IMAGE, Size (30,30)) / / if enabled, the flipped image continues to detect if (tryflip) {flip (smallImg, smallImg, 1); / / imshow ("inverted image", smallImg); cascade.detectMultiScale (smallImg, faces2, 1.1,2,0 / / | CV_HAAR_FIND_BIGGEST_OBJECT / / | CV_HAAR_DO_ROUGH_SEARCH | CV_HAAR_SCALE_IMAGE, Size (30,30)) For (vector::const_iterator r = faces2.begin (); r! = faces2.end ()) {faces.push_back (smallImg.cols-r-> x-r-> width, r-> y, r-> width, r-> height);} t = (double) cvGetTickCount ()-t; / / qDebug ("detection time =% g ms\ n", t / (double) cvGetTickFrequency () * 1000.)) For (vector::const_iterator r = faces.begin (); r! = faces.end ()) {Mat smallImgROI; vector nestedObjects; Point center; Scalar color = colors [I% 8]; int radius; double aspect_ratio = (double) r-> width/r- > height; if
< aspect_ratio && aspect_ratio < 1.3 ) { //标示人脸时在缩小之前的图像上标示,所以这里根据缩放比例换算回去 center.x = cvRound((r->X + r-> width*0.5) * scale); center.y = cvRound ((r-> y + r-> height*0.5) * scale); radius = cvRound ((r-> width + r-> height) * 0.25*scale); circle (img, center, radius, color, 3,8,0) } else rectangle (img, cvPoint (cvRound (r-> x*scale), cvRound (r-> y*scale)), cvPoint (cvRound ((r-> x + r-> width-1) * scale), cvRound ((r-> y + r-> height-1) * scale), color, 3,8,0); if (nestedCascade.empty () continue; smallImgROI = smallImg (* r)) / / nestedCascade.detectMultiScale (smallImgROI, nestedObjects, 1.1,2,0 / / | CV_HAAR_FIND_BIGGEST_OBJECT / | CV_HAAR_DO_ROUGH_SEARCH / / | CV_HAAR_DO_CANNY_PRUNING | CV_HAAR_SCALE_IMAGE, Size (30,30); for (vector::const_iterator nr = nestedObjects.begin (); nr! = nestedObjects.end ()) Nr++) {center.x = cvRound ((r-> x + nr- > x + nr- > width*0.5) * scale); center.y = cvRound ((r-> y + nr- > y + nr- > height*0.5) * scale); radius = cvRound ((nr- > width + nr- > height) * 0.25*scale); circle (img, center, radius, color, 3,8,0);} / / imshow ("recognition result", img) } at this point, I believe you have a deeper understanding of "how C++ uses opencv to achieve face detection". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.