#include <iostream>
static void help(const char** argv)
{
cout << "\n本程式演示了cv::CascadeClassifier類的用法,用於檢測物件(人臉+眼睛)。您可以使用Haar或LBP特徵。\n"
"此分類器在經過適當訓練後,可以識別多種剛性物件。\n"
"其最常見的用途是人臉識別。\n"
"用法:\n"
<< argv[0]
<< " [--cascade=<cascade_path> 這是主要的訓練分類器,例如正面人臉檢測器]\n"
" [--nested-cascade[=nested_cascade_path 這是一個可選的次級分類器,例如眼睛檢測器]]\n"
" [--scale=<影像縮放比例,大於或等於1,例如嘗試1.3>]\n"
" [--try-flip]\n"
" [檔名|攝像頭索引]\n\n"
"示例:\n"
<< argv[0]
<< " --cascade=\"data/haarcascades/haarcascade_frontalface_alt.xml\" --nested-cascade=\"data/haarcascades/haarcascade_eye_tree_eyeglasses.xml\" --scale=1.3\n\n"
"執行期間:\n\t按任意鍵退出。\n"
}
double scale, bool tryflip );
string cascadeName;
string nestedCascadeName;
int main(
int argc,
const char** argv )
{
string inputName;
bool tryflip;
double scale;
"{help h||}"
"{cascade|data/haarcascades/haarcascade_frontalface_alt.xml|}"
"{nested-cascade|data/haarcascades/haarcascade_eye_tree_eyeglasses.xml|}"
"{scale|1|}{try-flip||}{@filename||}"
);
if (parser.has("help"))
{
help(argv);
return 0;
}
cascadeName = parser.get<string>("cascade");
nestedCascadeName = parser.get<string>("nested-cascade");
scale = parser.get<
double>(
"scale");
if (scale < 1)
tryflip = parser.has("try-flip");
inputName = parser.get<string>("@filename");
if (!parser.check())
{
parser.printErrors();
return 0;
}
if (!nestedCascade.
load(samples::findFileOrKeep(nestedCascadeName)))
cerr << "警告:無法載入巢狀物件的分類器級聯檔案" << endl;
if (!cascade.
load(samples::findFile(cascadeName)))
{
cerr << "錯誤:無法載入分類器級聯檔案" << endl;
help(argv);
return -1;
}
if( inputName.empty() || (isdigit(inputName[0]) && inputName.size() == 1) )
{
int camera = inputName.empty() ? 0 : inputName[0] - '0';
if(!capture.
open(camera))
{
cout << "無法從攝像頭 #" << camera << " 捕獲" << endl;
return 1;
}
}
else if (!inputName.empty())
{
image =
imread(samples::findFileOrKeep(inputName), IMREAD_COLOR);
{
if (!capture.
open(samples::findFileOrKeep(inputName)))
{
cout << "無法讀取 " << inputName << endl;
return 1;
}
}
}
else
{
image =
imread(samples::findFile(
"lena.jpg"), IMREAD_COLOR);
{
cout << "無法讀取lena.jpg" << endl;
return 1;
}
}
{
cout << "影片捕獲已開始..." << endl;
for(;;)
{
capture >> frame;
if( frame.empty() )
break;
detectAndDraw( frame1, cascade, nestedCascade, scale, tryflip );
if( c == 27 || c == 'q' || c == 'Q' )
break;
}
}
else
{
cout << "正在檢測 " << inputName << " 中的人臉" << endl;
{
detectAndDraw( image, cascade, nestedCascade, scale, tryflip );
}
else if( !inputName.empty() )
{
FILE* f = fopen( inputName.c_str(), "rt" );
if( f )
{
char buf[1000+1];
while( fgets( buf, 1000, f ) )
{
int len = (int)strlen(buf);
while( len > 0 && isspace(buf[len-1]) )
len--;
buf[len] = '\0';
cout << "file " << buf << endl;
image =
imread( buf, IMREAD_COLOR );
{
detectAndDraw( image, cascade, nestedCascade, scale, tryflip );
if( c == 27 || c == 'q' || c == 'Q' )
break;
}
else
{
cerr << "糟糕,無法讀取影像 " << buf << endl;
}
}
fclose(f);
}
}
}
return 0;
}
double scale, bool tryflip )
{
double t = 0;
vector<Rect> faces, faces2;
const static Scalar colors[] =
{
};
resize( gray, smallImg,
Size(), fx, fx, INTER_LINEAR_EXACT );
1.1, 2, 0
|CASCADE_SCALE_IMAGE,
if( tryflip )
{
flip(smallImg, smallImg, 1);
1.1, 2, 0
|CASCADE_SCALE_IMAGE,
for( vector<Rect>::const_iterator r = faces2.begin(); r != faces2.end(); ++r )
{
faces.push_back(
Rect(smallImg.
cols - r->x - r->width, r->y, r->width, r->height));
}
}
for ( size_t i = 0; i < faces.size(); i++ )
{
vector<Rect> nestedObjects;
int radius;
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
color, 3, 8, 0);
if( nestedCascade.
empty() )
continue;
smallImgROI = smallImg( r );
1.1, 2, 0
|CASCADE_SCALE_IMAGE,
for ( size_t j = 0; j < nestedObjects.size(); j++ )
{
Rect nr = nestedObjects[j];
circle( img, center, radius, color, 3, 8, 0 );
}
}
}
用於物件檢測的 Cascade 分類器類。
定義 objdetect.hpp:258
cv::getTickFrequency
檢查分類器是否已載入。
bool load(const String &filename)
從檔案中載入分類器。
void detectMultiScale(InputArray image, std::vector< Rect > &objects, double scaleFactor=1.1, int minNeighbors=3, int flags=0, Size minSize=Size(), Size maxSize=Size())
在輸入影像中檢測不同大小的物件。檢測到的物件以列表形式返回...
如果陣列沒有元素,則返回 true。
int64_t int64
CV_NODISCARD_STD Mat clone() const
建立陣列及其底層資料的完整副本。
cv::getTickFrequency
double getTickFrequency()
2D 矩形的模板類。
定義 types.hpp:444
_Tp x
左上角的 x 座標
定義 types.hpp:487
_Tp y
左上角的 y 座標
定義 types.hpp:488
_Tp width
矩形的寬度
定義 types.hpp:489
_Tp height
矩形的高度
定義 types.hpp:490
用於指定影像或矩形大小的模板類。
Definition types.hpp:335
用於從影片檔案、影像序列或攝像頭進行影片捕獲的類。
Definition videoio.hpp:772
virtual bool open(const String &filename, int apiPreference=CAP_ANY)
開啟影片檔案、捕獲裝置或IP影片流進行影片捕獲。
virtual bool isOpened() const
如果影片捕獲已初始化,則返回 true。
#define CV_VERSION
Definition version.hpp:19
void flip(InputArray src, OutputArray dst, int flipCode)
沿垂直、水平或兩個軸翻轉 2D 陣列。
int cvRound(double value)
將浮點數舍入為最接近的整數。
Definition fast_math.hpp:200
@ circle
定義 gr_skig.hpp:62
void imshow(const String &winname, InputArray mat)
在指定視窗中顯示影像。
int waitKey(int delay=0)
等待按鍵按下。
CV_EXPORTS_W Mat imread(const String &filename, int flags=IMREAD_COLOR_BGR)
從檔案載入影像。
void cvtColor(InputArray src, OutputArray dst, int code, int dstCn=0, AlgorithmHint hint=cv::ALGO_HINT_DEFAULT)
將影像從一個顏色空間轉換為另一個顏色空間。
void rectangle(InputOutputArray img, Point pt1, Point pt2, const Scalar &color, int thickness=1, int lineType=LINE_8, int shift=0)
繪製一個簡單、粗或填充的矩形。
void equalizeHist(InputArray src, OutputArray dst)
均衡化灰度影像的直方圖。
int main(int argc, char *argv[])
定義 highgui_qt.cpp:3
void scale(cv::Mat &mat, const cv::Mat &range, const T min, const T max)
Definition quality_utils.hpp:90