-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChapter8.cpp
More file actions
88 lines (72 loc) · 2.83 KB
/
Chapter8.cpp
File metadata and controls
88 lines (72 loc) · 2.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/objdetect.hpp>
#include <iostream>
using namespace cv;
using namespace std;
//// IMPORTING IMAGES /////
//No output func?? wasnt allowed
int main(){
string path = "/Users/macbookpro/Desktop/UTEC/OpenCV/opencv3test/Resources/facetest2.png";
//Mat is the var type for images
//Mat is a ma^trix data type that opencv handles
//It handles all images
Mat img = imread(path);
CascadeClassifier faceCascade;
faceCascade.load("/Users/macbookpro/Desktop/UTEC/OpenCV/opencv3test/Resources/haarcascade_frontalface_default.xml");
//Then we check if it is loaded propperly or not...
if(faceCascade.empty()){
cout << "Error loading cascade classifier" << endl;
return -1;
}
//Then we resize the image to fit the cascade classifier
//resize(img, img, Size(200, 200));
//Next we are going to detect the faces
//So first we must store the bounding boxes
//We create a vector of rectangles
vector<Rect> faces;
faceCascade.detectMultiScale(img,faces,1.1,10);
//the parameters of detectMultiscale are..
//img - the image to detect faces in
//faces - the vector of rectangles
//1.1 - Scale Factor
//10 - Min Neighbors
//So we iterate through the faces and print the coordinates
for(int i = 0; i < faces.size(); i++){
rectangle(img,faces[i].tl(),faces[i].br(), Scalar(0, 0, 255), 3);
cout << faces[i].tl();
cout << faces[i].br();
//get the x coordinate of faces[i].tl();
//get the y coordinate of faces[i].tl();
//get the x coordinate of faces[i].br();
//get the y coordinate of faces[i].br();
}
Point p1, p2;
p1 = faces[0].tl();
p2 = faces[0].br();
cout << p2;
//CROPPING IMAGES
//Important to cut object, to add more processing in that specific region
// This region is called region of interest -> ROI
// To crop images, we can use rectangles
Rect roi(p1.x,p1.y,p2.x - p1.x,(p2.y - p1.y)/1.5); //Inside here we must define x,y witdh and height
// //So from point 100,100. We go 300px to the right, then 250 bottom
Mat imgCrop;
imgCrop = img(roi);
Mat imgGray, imgBlur, imgCanny, img_dilated, imgErode;
//Preprocessing
cvtColor(imgCrop,imgGray,COLOR_BGR2GRAY);
GaussianBlur(imgGray,imgBlur,Size(3,3),3,0);
Canny(imgBlur, imgCanny,25,75);
Mat kernel = getStructuringElement(MORPH_RECT,Size(3,3));
dilate(imgCanny,img_dilated,kernel);
//This prevents holes in the outlines
// getContours(img_dilated,img);
//
imshow("ImageName", img);
imshow("roi", imgCrop);
imshow("cnt",img_dilated);
waitKey(5000); //Whenever it opens up it will not close until we pres the open button
return 0;
}