一、背景介紹:
opencv無法對gif進行直接讀取,在python中PIL可以讀取,但是在C++中,目前不知道如何使用PIL庫,另外有推薦說使用FreeImage,但是檔案不全使用非常麻煩,故使用以下方法解碼gif的每一幀,并存放到frames里面,簡單、方便、有效!
二、代碼:
main.cpp
#include<iostream>
#include<opencv2/opencv.hpp>
#include<vector>
using namespace cv;
using namespace std;
int main()
{
VideoCapture capture;
Mat frame;
frame= capture.open("Gif.gif"); //讀取gif檔案
if(!capture.isOpened())
{
printf("can not open ...\n");
return -1;
}
std::vector<cv::Mat> frames; //存放gif的所有幀,每個frame都是Mat格式
while (capture.read(frame))
{
std::cout << frame.size() << std::endl; //列印每一幀的尺寸
cv::imwrite("demo2.jpg", frame); // 保存gif的其中一幀便于檢查例外
frames.push_back(frame);
}
capture.release();
return 0;
}
三、應用案例:該專案中用于對圖片提取特征,里面除了opencv可以直接讀取的,還有opencv不能讀取的,如gif檔案,甚至可能還有其他無法決議的檔案:在這里面,我的處理邏輯是:先使用opencv讀取,如果讀取為空,那么他可能是gif檔案,再使用gif方式讀取,如果依然為空,則不作處理,【gif這里只取其中一幀進行提取特征】
各類別資料量級:opencv可讀取 >> gif > 其他各個型別
cv::Mat img = cv::imread(image);
if (img.empty()) {
cv::VideoCapture capture;
cv::Mat imggif;
imggif = capture.open(image);
if(!capture.isOpened())
{
json = WriteJsonError(IMAGEERROR);
return FAILED;
}
while (capture.read(imggif))
{
std::cout << img.size() << std::endl;
img = imggif.clone();
}
capture.release();
if (img.empty()){
json = WriteJsonError(IMAGEERROR);
return FAILED;
}
}
std::vector<float> curfeature = inference->InferSingle(img);
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/197876.html
標籤:python
上一篇:使用Python發送郵件
