简体   繁体   English

使用OpenCV和C裁剪图像

[英]Cropping an image with OpenCV and C

Hi am trying to debug a C++/C developers code, He wrote us a dll that we are using in an adobe native extension that basically takes pictures with a grid of web cams and after doing some face detection is suppose to crop the images and write them to disc. 您好我正在尝试调试C ++ / C开发人员代码,他给我们写了一个dll,我们正在adobe原生扩展中使用它基本上用网络摄像头网格拍照,并且做了一些人脸检测之后假设裁剪图像并写他们要光盘。

But the app always hangs and eventually crashes on this line: 但该应用程序总是挂起并最终在此行崩溃:

smallFrame = image(Rect(x, y, CROPPING_WIDTH, CROPPING_HEIGHT));

i have narrowed it down to this line by throwing a try/catch around it and the exception it spits out is not very helpful, it just says ???????n as the exception. 我通过在它周围抛出一个try / catch将它缩小到这一行,并且它吐出的异常不是很有帮助,它只是说???????n作为例外。

like so: 像这样:

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;

}

here is the whole method: 这是整个方法:

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));

* a fixed CROPPING_WIDTH or HEIGHT won't do. *固定的CROPPING_WIDTH或HEIGHT不会。 you've got to check, if your Rect did not end up partly outside the image, ie if x+CROPPING_WIDTH < img.cols-1 你必须检查,如果你的Rect没有在图像之外部分结束,即如果x + CROPPING_WIDTH <img.cols-1

berak explained you the probable cause of your error, but the reason for which you cannot debug is that you are using: berak向您解释了您的错误的可能原因,但您无法调试的原因是您正在使用:

wsprintf (str, L"Exception Occured during Face Found : %s", ex.what());

in Visual Studio environment, with ex.what() returning a const char* . 在Visual Studio环境中,ex.what()返回一个const char* Unfortunately, %s behaviour is platform dependent, in this case it expects a wide character string (so, %s is wide string for wsprintf and byte string for sprintf). 不幸的是,%s的行为与平台有关,在这种情况下,它需要一个宽字符串(因此,%s是wsprintf的宽字符串和sprintf的字节字符串)。 In Unix, you would have the correct behaviour. 在Unix中,您将拥有正确的行为。 In Visual Studio, you have to use %S. 在Visual Studio中,您必须使用%S。

Check this: printf, wprintf, %s, %S, %ls, char* and wchar*: Errors not announced by a compiler warning? 检查一下: printf,wprintf,%s,%S,%ls,char *和wchar *:编译器警告未公布错误? and in particular this . 特别是这个

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM