C ++を使用してOpenCVで目を検出する方法は?
ここでは、OpenCVで目を検出する方法を学びます。 'C:/ opencv / sources / data/haarcascades'にあるhaarcascade_eye.xml分類子を使用して目を検出します。目を検出するには、これらのヘッダーを追加する必要があります。
最初のヘッダーは
OpenCVで目を検出して追跡する方法を示す次のプログラム。
例
#include<iostream>
#include<opencv2/highgui/highgui.hpp>
#include<opencv2/imgproc/imgproc.hpp>
#include<opencv2/objdetect/objdetect.hpp>
using namespace cv;
using namespace std;
int main() {
Mat frame;//declaring a matrix to video frame in it//
namedWindow("Detect");//Declaring a window to show our work//
VideoCapture image(0);//capturing video from default camera//
if (!image.isOpened()){ //Error message if video source is not found//
cout << "Couldn't load video from the source.Make sure your camera is working properly." << endl;
system("pause");
return 0;
}
double height = image.set(CAP_PROP_FRAME_HEIGHT, 480);//setting up height of each frame//
double width = image.set(CAP_PROP_FRAME_WIDTH, 640);//setting up width of each frame//
CascadeClassifier face_cascade, eyes_cascade;//declaring a CascadeClassifier object//
face_cascade.load("C:/opencv/sources/data/haarcascades/haarcascade_frontalface_alt.xml");//loading the cascade classifier//
eyes_cascade.load("C:/opencv/sources/data/haarcascades/haarcascade_eye.xml");
while (true) {
bool temp = image.read(frame);//loading video frames from source to our matrix named frame//
vector<Rect>faces;//Declaring a vector named faces//
face_cascade.detectMultiScale(frame, faces, 1.1, 2, 0 | CASCADE_SCALE_IMAGE, Size(100, 100));//detecting the face
for (int i = 0; i < faces.size(); i++){ //for locating the face
Point center(faces[i].x + faces[i].width * 0.5, faces[i].y + faces[i].height * 0.5);//getting the center of the face//
ellipse(frame, center, Size(faces[i].width * 0.5, faces[i].height * 0.5), 0, 0, 360, Scalar(255, 0, 255), 4, 8, 0);//draw an ellipse on the face//
Mat faceROI = frame(faces[i]);//Taking area of the face as Region of Interest for eyes//
vectoreyes;//declaring a vector named eyes//
eyes_cascade.detectMultiScale(faceROI, eyes, 1.1, 2, 0 | CASCADE_SCALE_IMAGE, Size(5, 5));//detect eyes in every face//
for (size_t j = 0; j < eyes.size(); j++){ //for locating eyes//
Point center(faces[i].x + eyes[j].x + eyes[j].width * 0.5, faces[i].y + eyes[j].y + eyes[j].height * 0.5);//getting the centers of both eyes//
int radius = cvRound((eyes[j].width + eyes[j].height) * 0.25);//declaring radius of the eye enclosing circles//
circle(frame, center, radius, Scalar(255, 0, 0), 4, 8, 0);//drawing circle around both eyes//
}
}
imshow("Detect", frame);//showing result in window named 'Detect'//
if (waitKey(30) == 27){ //wait time for each frame is 30 milliseconds//
break;
}
}
return 0;
} 出力
-
C ++を使用してOpenCVで線を引く方法は?
線を引くには、始点と終点の2つの点が必要です。線を引くためのキャンバスも必要です。 キャンバスのマトリックスであるOpenCVを使用して、ラインの開始点と終了点を定義する必要があります。線にも色を付ける必要があります。線の太さも説明する必要があります。 OpenCVを使用して線を描画する場合は、マトリックス、2つのポイント、および色と線の太さを宣言する必要があります。 OpenCVを使用するには、 を含める必要があります line()のためのヘッダー 関数はこのヘッダーで定義されています。 このメソッドの基本的な構文は次のとおりです- 構文 line(whiteMatrix, star
-
C ++を使用してOpenCVの画像のチャンネル数を計算するにはどうすればよいですか?
このトピックでは、画像のチャンネル数を確認する方法を理解します。プログラムを実行すると、チャンネル番号がコンソールウィンドウに表示されます。 チャネルの番号を取得するために、channels()という名前のOpenCVのクラスを使用しました。 クラスchannels()のオブジェクトとして画像マトリックスを渡すと、チャネルに整数値が与えられます。 次のプログラムは、チャネルの数をカウントし、コンソールウィンドウに表示します。 例 #include<iostream> #include<opencv2/highgui/highgui.hpp> using namesp