The previous period of time to see the OpenCV, in fact, there are a lot of examples of procedures, reference code worthy of our learning, the image feature extraction three magic weapon: Hog features, LBP features, haar characteristics have a certain understanding.
The example of this article in the beginning of the program has not been adjusted, and tonight to increase the pass, try to try the results can also need to understand deeply. It is worthwhile to try it out, or it is very fulfilling, although it is a ready-made example ....
Environment: OPENCV3.1+VS2013+WIN10
Copy Code
/*!
* \file Capture.cpp
*
* \author Ranjiewen
* \date November 2016
*
* http://www.cnblogs.com/tanfy/p/5552270.html
Analytic OpenCV self-brought face recognition source (.../opencv-3.1.0/samples/cpp/facedetect.cpp)
*/
#include "opencv2/objdetect.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/imgproc.hpp"
#include <iostream>
using namespace Std;
using namespace CV;
static void Help ()
{
cout << "\nthis program demonstrates the Cascade recognizer. Now you can use Haar or LBP features.\n "
"This classifier can recognize many kinds of rigid objects, once the appropriate classifier is trained.\n"
"It's most known use are for fawww.ymzxrj.com Direct marketing software
Www.yyzx66.cn/ces.\n "
"Usage:\n"
"./facedetect [--cascade=<cascade_path> This is the primary trained classifier such as frontal face]\n"
"[--nested-cascade[=nested_cascade_path This an optional secondary classifier such as eyes]]\n"
"[--scale=<image scale greater or www.ymzxrj.com equal to 1, try 1.3 for example>]\n"
"[--try-flip]\n]
"[filename|camera_index]\n\n]
"See Facedetect.cmd for one call:\n"
"./facedetect--cascade=\". /.. /data/haarcascades/haarcascade_frontalface_alt.xml\ "--nested-cascade=\". /.. /data/haarcascades/haarcascade_ewww.yunfeizao.cn Fiyun International
www.feiyunfan.cn ye_tree_eyeglasses.xml\ "--scale=1.3\n\n"
"During execution:\n\thit any key to quit.\n"
"\tusing OpenCV Version" << cv_version << "\ n" << Endl;
}
void Detectanddraw (mat& img, cascadeclassifier& Cascade,
cascadeclassifier& Nestedcascade,
Double scale, bool tryflip);
String Cascadename;
String Nestedcascadename;
int main (int argc, const char** argv)
{
Videocapture capture;
Mat frame, image;
String InputName;
BOOL Tryflip;
Cascadeclassifier is a OPENCV in the face detection of a cascade classifier, now there are two options: one is to use the old version of the Cvhaarclassifiercascade function, One is to use the new version of the Cascadeclassifier class. Older classifiers only support class Haar features, while new versions of classifiers can use Haar as well as LBP features.
Cascadeclassifier Cascade, Nestedcascade;
Double scale;
Cv::commandlineparser parser (argc, argv,
"{Help h| |}"
"{cascade| d:/opencv/sources/data/haarcascades/haarcascade_frontalface_alt.xml|} " The default path is the actual installation path under sample, modifying the path to load the load successfully
"{nested-cascade| d:/opencv/sources/data/haarcascades/haarcascade_eye_tree_eyeglasses.xml|} " modifying paths
"{scale|1|} {try-flip| |} www.fsmaxbuy.com/{@filename | |} "//File is empty, set the camera, real-time detection of human face
);
if (Parser.has ("Help"))
{
Help ();
return 0;
}
Cascadename = www.yiqianou.cn/parser.get<string> ("cascade");
Nestedcascadename = parser.get<string> ("Nested-cascade");
Scale = parser.get<double> ("scale");
if (Scale < 1)
scale = 1;
Tryflip = Parser.has ("Try-flip");
InputName = parser.get<string> ("@filename");
Std::cout << inputname << Std::endl; Test
if (!parser.check ())
{
Parser.printerrors ();
return 0;
}
Load model
if (!nestedcascade.load (nestedcascadename))
Cerr << "warning:could not load classifier cascade for nested objects" << Endl;
if (!cascade.load (cascadename))
{
Cerr << "error:could not load classifier cascade" << Endl;
Help ();
return-1;
}
Read camera
IsDigit detect if the character is an Arabic numeral
if (Inputname.empty () | | | (IsDigit (inputname[0]) && inputname.size () = = 1))
{
int c = Inputname.empty ()? 0:inputname[0]-' 0 ';
Here if the system is on the virtual machine, you need to set up the takeover camera in the virtual machine: virtual machine (M), mobile device, camera name--connect (disconnect from host)
if (!capture.open (c))
cout << "Capture from camera #" << C << "didn-t work" << Endl;
else {
Capture.set (Cv_cap_prop_frame_width, 640);
Capture.set (Cv_cap_prop_frame_height, 480);
}
}
else if (inputname.size ())
{
Image = Imread (InputName, 1);
if (Image.empty ())
{
if (!capture.open (InputName))
cout << "Could not read" << inputname << Endl;
}
}
Else
{
Image = Imread (".. /data/lena.jpg ", 1);
if (Image.empty ()) cout << "couldn" t read. /data/lena.jpg "<< Endl;
}
if (capture.isopened ())
{
cout << "Video capturing has been started ..." << Endl;
for (;;)
{
Std::cout << "Capturing ..." << Std::endl; Test
Capture >> frame;
if (Frame.empty ())
Break
Mat frame1 = Frame.clone ();
Std::cout << "Start to detect ..." << Std::endl; Test
Detectanddraw (FRAME1, Cascade, Nestedcascade, scale, tryflip);
int c = Waitkey (10);
if (c = = | | c = = ' Q ' | | c = = ' Q ')
Break
}
}
Else
{
cout << "detecting face (s) in" << InputName << Endl;
if (!image.empty ())
{
Detectanddraw (image, Cascade, Nestedcascade, scale, tryflip);
Waitkey (0);
}
else if (!inputname.empty ())
{
/* Assume it is a text file containing the
List of the image filenames to being processed-one per line */
file* f = fopen (Inputname.c_str (), "RT");
if (f)
{
Char buf[1000 + 1];
while (Fgets (buf, +, F))
{
int len = (int) strlen (BUF), C;
while (len > 0 && isspace (buf[len-1]))
len--;
Buf[len] = ' + ';
cout << "File" << buf << Endl;
Image = Imread (buf, 1);
if (!image.empty ())
{
Detectanddraw (image, Cascade, Nestedcascade, scale, tryflip);
c = Waitkey (0);
if (c = = | | c = = ' Q ' | | c = = ' Q ')
Break
}
Else
{
Cerr << "Aw snap, couldn ' t read image" << buf << Endl;
}
}
Fclose (f);
}
}
}
return 0;
}
void Detectanddraw (mat& img, cascadeclassifier& Cascade,
cascadeclassifier& Nestedcascade,
Double scale, BOOL tryflip)
{
Double T = 0;
Vector<rect> faces, Faces2;
Const static Scalar colors[] =
{
Scalar (255, 0, 0),
Scalar (255, 128, 0),
Scalar (255, 255, 0),
Scalar (0, 255, 0),
Scalar (0, 128, 255),
Scalar (0, 255, 255),
Scalar (0, 0, 255),
Scalar (255, 0, 255)
};
Mat Gray, Smallimg;
Cvtcolor (IMG, Gray, Color_bgr2gray);
Double FX = 1/scale;
Resize (Gray, Smallimg, Size (), FX, FX, Inter_linear);
Equalizehist (smallimg, smallimg);
T = (double) cvgettickcount ();
Cascade.detectmultiscale (Smallimg, Faces,
1.1, 2, 0
//| Cascade_find_biggest_object
//| Cascade_do_rough_search
| Cascade_scale_image,
Size (30, 30));
if (tryflip)
{
Flip (smallimg, smallimg, 1);
Cascade.detectmultiscale (Smallimg, Faces2,
1.1, 2, 0
//| Cascade_find_biggest_object
//| Cascade_do_rough_search
| Cascade_scale_image,
Size (30, 30));
for (Vector<rect>::const_iterator r = Faces2.begin (); R! = Faces2.end (); r++)
{
Faces.push_back (Rect (s) Mallimg.cols-r->x-r->width, R->y, R->width, r->height));
}
}
T = (double) cvgettickcount ()-T;
printf ("Detection time =%g ms\n", t/((double) cvgettickfrequency ( ) (*1000));
for (size_t i = 0; i < faces.size (); i++)
{
Rect r = faces[i];
Mat Smallimgroi;
Vector<rect> nestedobjects;
Point Center;
Scalar color = colors[i% 8];
int radius;
Double aspect_ratio = (double) r.width/r.height;
if (0.75 < 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
//| Cascade_find_biggest_object
//| Cascade_do_rough_search
//| Cascade_do_canny_pruning
| Cascade_scale_image,
Size (30, 30));
for (size_t j = 0; J < Nestedobjects.size (); j + +)
{
Rect nr = Nestedobjects[j];
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 ("Result", IMG);
}
/*****************************************************
* \file Capture.cpp
* \date 2016/11/10 0:22
* \author Ranjiewen
* \contact: [email protected]
* \ Problem Description:
Http://www.cnblogs.com/lingshaohu/archive/2011/12/16/2290017.html
* \ Problem Analysis:
Can save AVI, but can not open, to improve
*****************************************************/
#include <iostream>
#include <opencv2/opencv.hpp>
using namespace CV;;
using namespace Std;
int main ()
//{
cvcapture* capture = Cvcapturefromcam (-1);
cvvideowriter* video = NULL;
iplimage* frame = NULL;
int n;
if (!capture)//If you cannot turn on the camera to give a warning
// {
cout << "Can not open the camera." << Endl;
return-1;
// }
Else
// {
frame = Cvqueryframe (capture); First get a frame in the camera
Video = Cvcreatevideowriter ("Camera.avi", CV_FOURCC (' X ', ' V ', ' I ', ' D '), 25,
Cvsize (Frame->width, frame->height)); Create a Cvvideowriter object and allocate space
The saved file name is Camera.avi, the encoding to be selected when running the program, size is the size of the camera video, frame frequency is 32
if (video)//If you can create a Cvvideowriter object, it indicates success
// {
cout << "Videowriter has created." << Endl;
// }
//
Cvnamedwindow ("Camera Video", 1); Create a new window
int i = 0;
while (I <= 300)//Let it cycle 200 times automatically stop admission
// {
frame = Cvqueryframe (capture); Get a frame from the Cvcapture
if (!frame)
// {
cout << "Can not get frame from the capture." << Endl;
Break
// }
n = cvwriteframe (video, frame); Determines if the write succeeds, if the return is 1, indicates a successful write
cout << n << Endl;
Cvshowimage ("Camera Video", frame); Picture showing the video content
i++;
if (Cvwaitkey (2) > 0)
Break If there are other keyboard responses, exit
// }
//
Cvreleasevideowriter (&video);
Cvreleasecapture (&capture);
Cvdestroywindow ("Camera Video");
// }
return 0;
//}
Copy Code
This is called the camera dynamic detection of human face program, the experimental results:
Console output:
Copy Code
Videoinput library-0.1995-tfw07 * * * * *
Setup:setting up Device 0
Setup:integrated Camera
Setup:couldn ' t find preview pin using Smarttee
Setup:default Format is set to 640x480
Setup:trying specified format RGB24 @ 640x480
setup:trying format RGB24 @ 640x480
setup:trying format RGB32 @ 640x480
setup:trying format RGB555 @ 640x480
setup:trying format RGB565 @ 640x480
setup:trying format YUY2 @ 640x480
Setup:capture Callback Set
Setup:device is setup and ready to capture.
Video capturing has been started ...
Capturing ...
Start to detect ...
Detection time = 486.754 ms
Capturing ...
Start to detect ...
Detection time = 444.236 ms
Capturing ...
Start to detect ...
Detection time = 441.649 ms
Capturing ...
Start to detect ...
Detection time = 447.361 ms
Capturing ...
Start to detect ...
Detection time = 427.589 ms
Capturing ...
Start to detect ...
Detection time = 453.187 ms
Capturing ...
Start to detect ...
OpenCV routines for face detection