gpt4 book ai didi

opencv - 在 OpenCV 中使用 imread() 加载图像时如何捕获损坏的 JPEG?

转载 作者:太空宇宙 更新时间:2023-11-03 20:38:28 25 4
gpt4 key购买 nike

OpenCV 是这样说的

Corrupt JPEG data: premature end of data segment

Corrupt JPEG data: bad Huffman code

Corrupt JPEG data: 22 extraneous bytes before marker 0xd9

当使用 imread() 加载损坏的 jpeg 图像时。我能以某种方式捕获它吗?否则我为什么会得到这些信息?我必须自己检查二进制文件吗?

最佳答案

OpenCV(2.4 版)不会覆盖 libjpeg 的基本错误处理,使它们“无法捕获”。将以下方法添加到 modules/highgui/src/grfmt_jpeg.cpp,就在 error_exit() 定义的下方:

METHODDEF(void)
output_message( j_common_ptr cinfo )
{
char buffer[JMSG_LENGTH_MAX];

/* Create the message */
(*cinfo->err->format_message) (cinfo, buffer);

/* Default OpenCV error handling instead of print */
CV_Error(CV_StsError, buffer);
}

现在将该方法应用于解码器错误处理程序:

state->cinfo.err = jpeg_std_error(&state->jerr.pub);
state->jerr.pub.error_exit = error_exit;
state->jerr.pub.output_message = output_message; /* Add this line */

将该方法也应用于编码器错误处理程序:

cinfo.err = jpeg_std_error(&jerr.pub);
jerr.pub.error_exit = error_exit;
jerr.pub.output_message = output_message; /* Add this line */

像往常一样重新编译和安装 OpenCV。从现在开始,您应该能够像捕获任何其他 OpenCV 错误一样捕获 libjpeg 错误。示例:

>>> cv2.imread("/var/opencv/bad_image.jpg")
OpenCV Error: Unspecified error (Corrupt JPEG data: 1137 extraneous bytes before marker 0xc4) in output_message, file /var/opencv/opencv-2.4.9/modules/highgui/src/grfmt_jpeg.cpp, line 180
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
cv2.error: /var/opencv/opencv-2.4.9/modules/highgui/src/grfmt_jpeg.cpp:180: error: (-2) Corrupt JPEG data: 1137 extraneous bytes before marker 0xc4 in function output_message

(我已经为上面的 submitted a pull request 但它被拒绝了,因为它会导致人们阅读图像而没有异常捕捉的问题。)

希望这对仍在为这个问题苦苦挣扎的人有所帮助。祝你好运。

关于opencv - 在 OpenCV 中使用 imread() 加载图像时如何捕获损坏的 JPEG?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9131992/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com