2015-05-14 109 views
0

我正在使用OpenCV從文件夾中讀取圖像。很多這樣的消息顯示:opencv read image「JPEG文件過早結束」

Corrupt JPEG data: premature end of data segment 
Premature end of JPEG file 
Premature end of JPEG file 
Premature end of JPEG file 

如何捕捉此異常並刪除這些圖像文件?

+0

and ..................?顯示一些代碼。 –

回答

0

既然你說你正在閱讀'圖像'(多個圖像),你會循環瀏覽文件夾中的文件。 在這種情況下,如果你檢查圖像是否有效使用以下:

Mat image; 
image = imread(argv[1], CV_LOAD_IMAGE_COLOR); // Read the file 

if(! image.data)        // Check for invalid input 
{ 
    cout << "Could not open or find the image" << std::endl ; 
    return -1; 
} 

然後你可以繼續刪除其腐敗/壞文件。

+0

似乎有推薦使用imdecode()來解決你的問題,參見[this] -image -with-imread-in-opencv /) 和[this SO post](http://stackoverflow.com/questions/4271489/little-help-with-cvimdecode) –