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的這個問題。謝謝! – 2015-12-27 15:02:32
用戶也可能有興趣閱讀[post here](https://stackoverflow.com/questions/24805500/can-i-fix-photos-with-corrupt-jpeg-data) – jdhao 2017-11-14 15:03:28