[英]Cropping an image with OpenCV and C
您好我正在嘗試調試C ++ / C開發人員代碼,他給我們寫了一個dll,我們正在adobe原生擴展中使用它基本上用網絡攝像頭網格拍照,並且做了一些人臉檢測之后假設裁剪圖像並寫他們要光盤。
但該應用程序總是掛起並最終在此行崩潰:
smallFrame = image(Rect(x, y, CROPPING_WIDTH, CROPPING_HEIGHT));
我通過在它周圍拋出一個try / catch將它縮小到這一行,並且它吐出的異常不是很有幫助,它只是說???????n作為例外。
像這樣:
try
{
smallFrame = image(Rect(x, y, CROPPING_WIDTH, CROPPING_HEIGHT));
}
catch(exception ex)
{
wsprintf (str, L"Exception Occured during Face Found : %s", ex.what());
WriteLogFile(str);
smallFrame = frame;
}
這是整個方法:
Mat cropFaceFrame( Mat frame)
{
std::vector<Rect> faces;
Mat frame_gray, smallFrame;
int height = 0;
unsigned index, i;
cvtColor( frame, frame_gray, CV_BGR2GRAY );
equalizeHist( frame_gray, frame_gray );
face_cascade.detectMultiScale( frame_gray, faces, 1.1, 2, 0|CV_HAAR_SCALE_IMAGE, Size(60, 60));
index = faces.size();
wsprintf (str, L
for (i = 0; i < faces.size(); i++ )
{
if (height < faces[i].height)
{
height = faces[i].height;
index = i;
}
}
Mat image(frame);
int maxRight, maxDown;
maxRight = IMAGE_WIDTH-CROPPING_WIDTH -1;
// right margin
maxDown = IMAGE_HEIGHT-CROPPING_HEIGHT-1;
// down margin
if (index == faces.size())
{
// crop the center part if no face found
try
{
smallFrame = image(Rect(maxRight/2, maxDown/2, CROPPING_WIDTH, CROPPING_HEIGHT));
}
catch(exception ex)
{
smallFrame = frame;
}
}
else
{
int x, y;
x = faces[index].x - (CROPPING_WIDTH-faces[index].width)/2;
if (x < 0) x = 0;
else if (x > maxRight) x = maxRight;
y = faces[index].y - (CROPPING_HEIGHT-faces[index].height)/3;
if (y < 0) y = 0;
else if (y > maxDown) y = maxDown;
try
{
smallFrame = image(Rect(x, y, CROPPING_WIDTH, CROPPING_HEIGHT));
}
catch(exception ex)
{
wsprintf (str, L
"Exception Occured during no Face Found : %s", ex.what());
WriteLogFile(str);
smallFrame = frame;
}
}
return smallFrame;
}
smallFrame = image(Rect(x, y, CROPPING_WIDTH, CROPPING_HEIGHT));
*固定的CROPPING_WIDTH或HEIGHT不會。 你必須檢查,如果你的Rect沒有在圖像之外部分結束,即如果x + CROPPING_WIDTH <img.cols-1
berak向您解釋了您的錯誤的可能原因,但您無法調試的原因是您正在使用:
wsprintf (str, L"Exception Occured during Face Found : %s", ex.what());
在Visual Studio環境中,ex.what()返回一個const char*
。 不幸的是,%s的行為與平台有關,在這種情況下,它需要一個寬字符串(因此,%s是wsprintf的寬字符串和sprintf的字節字符串)。 在Unix中,您將擁有正確的行為。 在Visual Studio中,您必須使用%S。
檢查一下: printf,wprintf,%s,%S,%ls,char *和wchar *:編譯器警告未公布錯誤? 特別是這個 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.