【问题标题】:Cropping an image with OpenCV and C使用 OpenCV 和 C 裁剪图像
【发布时间】:2013-08-12 13:56:25
【问题描述】:

您好,我正在尝试调试 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;

}

【问题讨论】:

    标签: c++ visual-c++ opencv dll


    【解决方案1】:
    smallFrame = image(Rect(x, y, CROPPING_WIDTH, CROPPING_HEIGHT));
    

    * 固定的 CROPPING_WIDTH 或 HEIGHT 是不行的。你必须检查一下,如果你的 Rect 没有部分出现在图像之外,即 x+CROPPING_WIDTH

    【讨论】:

      【解决方案2】:

      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* and wchar*: Errors not announced by a compiler warning?,尤其是this

      【讨论】:

        猜你喜欢
        • 2020-10-18
        • 1970-01-01
        • 2017-08-04
        • 1970-01-01
        • 1970-01-01
        • 2012-12-31
        • 1970-01-01
        • 2023-03-05
        • 1970-01-01
        相关资源
        最近更新 更多