生活随笔
收集整理的這篇文章主要介紹了
c++ opencv 将视频转化成字符串
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
直接貼代碼,我在這個大佬(https://www.bilibili.com/video/BV1f5411t7oD)的代碼基礎上進行了修改
這是他的代碼:(方便你們更好的理解)
#include<stdio.h>
#include<opencv2/opencv.hpp>
#include<string>
#include<vector>
#include<windows.h>using namespace cv
;
using namespace std
;int main()
{VideoCapture video
;Mat frame
, gray
;video
.open("E:/黑人抬棺.mp4");int cols
= video
.get(CAP_PROP_FRAME_WIDTH
);int rows
= video
.get(CAP_PROP_FRAME_HEIGHT
);int framecount
= video
.get(CAP_PROP_FRAME_COUNT
);int fps
= video
.get(CAP_PROP_FPS
);int delty
= 10;int deltx
= 5;int value
;int n
= 0;char c
[] = " .,-'`:!1&@#$";vector
<string
> v
;HANDLE h
= GetStdHandle(STD_OUTPUT_HANDLE
);COORD pos
= { 0,0 };while (n
< framecount
){n
++;video
.read(frame
);cvtColor(frame
, gray
, COLOR_BGR2GRAY
);string s
= "";for (int row
= 0; row
< rows
- delty
; row
= row
+ delty
){for (int col
= 0; col
< cols
- deltx
; col
= col
+ deltx
){value
= gray
.at
<uchar
>(row
, col
);s
= s
+ c
[int(value
/ 20)];}s
= s
+ '\n';}v
.push_back(s
);system("cls");printf("正在讀取:%d/%d", n
, framecount
);}for (int i
= 0; i
< v
.size(); i
++){SetConsoleCursorPosition(h
, pos
);cout
<< v
[i
];waitKey(1000 / fps
);}return 0;
}
我在原有基礎上,
增加讀文件操作,方便那些不懂c++的人放自己喜歡的視頻,以及更改顯示的字符串和修改每秒顯示的幀數;并且可以自行設定字符串之間的間隔增加寫文件操作,將輸出字符串存到對應的文本文件中。(因為這樣的文本文件打開很大,并且可能打開時會卡住,所以請根據需要將這個文件留住或者刪除,不影響進程)將在棧區的數據都改成了堆區,強制類型轉換和指針用c++11的格式修復了導入某些視頻出bug的問題,即增加了通道判斷增加了注釋把代碼的所有警告都進行了修正(除了opencv的警告我不懂改不了以外)這個是沒有聲音的,聲音只能用其他庫去加,或者自己用視頻合成軟件去添加針對容器進行了優化,提前擴容,增加效率哪怕沒人看,我也要認真搞好><
#include<iostream>
#include<fstream>
#include<opencv2/opencv.hpp>
#include<vector>
#include<windows.h>
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/imgproc/types_c.h"using namespace cv
;
using namespace std
;
int main()
{cout
<< "注意要將視頻的路徑的\\改成/" << endl
<<endl
;ifstream ifs
;ifs
.open("MyVideo.txt", ios
::in
);if (!ifs
.is_open()) {cout
<< "視頻文件沒有打開" << endl
;return 0;}char filePath
[1024];ifs
.getline(filePath
, sizeof(filePath
));ifs
.close();unique_ptr
<VideoCapture
> video
= make_unique
<VideoCapture
>(filePath
);ifstream ifs3
;ifs3
.open("MyInfo.txt", ios
::in
);if (!ifs3
.is_open()) {cout
<< "MyInfo文件沒有打開" << endl
;return 0;}vector
<string
> myInfoVector
;myInfoVector
.reserve(6);string myInfoString
;while (ifs3
>> myInfoString
) {myInfoVector
.push_back(myInfoString
);}ifs3
.close();int cols
= static_cast<int>(video
->get(CAP_PROP_FRAME_WIDTH
));int rows
= static_cast<int>(video
->get(CAP_PROP_FRAME_HEIGHT
));_int64 framecount
= static_cast<_int64
>(video
->get(CAP_PROP_FRAME_COUNT
))-1;HANDLE h
= GetStdHandle(STD_OUTPUT_HANDLE
);COORD pos
= { 0,0 };int fps
= static_cast<int>(video
->get(CAP_PROP_FPS
));int delty
= atoi(myInfoVector
[4].c_str());int deltx
= atoi(myInfoVector
[5].c_str());int value
;_int64 n
= 0;cout
<< "你可以在MyChar.txt文件中修改展現的字符串" << endl
<< endl
;ifstream ifs2
;ifs2
.open("MyChar.txt", ios
::in
);if (!ifs2
.is_open()) {cout
<< "字符串文件沒有打開" << endl
;return 0;}char filePath2
[1024];ifs2
.getline(filePath2
, sizeof(filePath2
));ifs2
.close();string
myChar(filePath2
);int myCharSize
= static_cast<int>(myChar
.size());ofstream ofs
;ifstream ifs_out_test
;ifs_out_test
.open("MyOut.txt", ios
::in
);if (!ifs_out_test
.is_open()) {cout
<< "MyOut文件沒有打開" << endl
<< endl
;ifs_out_test
.close();}else {ifs_out_test
.close();ofs
.open("MyOut.txt", ios
::out
);}unique_ptr
<vector
<string
>> v
= make_unique
<vector
<string
>>();v
->reserve(framecount
);unique_ptr
<Mat
> frame
= make_unique
<Mat
>();unique_ptr
<Mat
> gray
= make_unique
<Mat
>();unique_ptr
<string
> s
= make_unique
<string
>();int reservesize
= (rows
/ delty
) * (cols
/ deltx
);s
->reserve(reservesize
);while (n
< framecount
){n
++;video
->read(*frame
);if (frame
->channels() == 4) {s
->clear();cv
::cvtColor(*frame
, *gray
, CV_BGRA2GRAY
);}else if (frame
->channels() == 3) {s
->clear();cv
::cvtColor(*frame
, *gray
, CV_BGR2GRAY
);}else if (frame
->channels() == 2) {s
->clear();cv
::cvtColor(*frame
, *gray
, CV_BGR5652GRAY
);}else {v
->push_back(*s
);continue;}for (int row
= 0; row
< rows
- delty
; row
= row
+ delty
){for (int col
= 0; col
< cols
- deltx
; col
= col
+ deltx
){ value
= gray
->at
<uchar
>(row
, col
);int index
= (value
* (myCharSize
-1)) / 255 ;s
->push_back(myChar
.at(index
));}s
->push_back('\n');}v
->push_back(*s
);printf("Now reading :%lld/%lld\n", n
, framecount
);}system("cls");for (const auto& printValue
: *v
) {SetConsoleCursorPosition(h
, pos
);cout
<< printValue
<< endl
;if (ofs
.is_open()) {ofs
<< printValue
<< endl
;}waitKey(atoi(myInfoVector
[3].c_str()) / fps
);}if (ofs
.is_open()) {ofs
.close();}return 0;
}
注意事項:如果僅僅復制粘貼代碼,是無法運行的,必須配置opencv庫,至于怎么配置,可以看我的這個教程鏈接
https://blog.csdn.net/bioinformatique/article/details/105655809
下面是我打包后的exe文件,可以直接用,不需要安裝任何庫,不需要vs,解壓,按照說明,一定可以運行,
(我在注意事項中詳細說明了怎么使用這個exe文件,非常簡單,并且你們可以隨意diy,不懂c++,不懂編程,也可以用我的exe文件將你們的視頻轉成字符串)
鏈接:https://pan.baidu.com/s/10rajqK8OZi996LMoKthTBw
提取碼:6r3o
如果代碼對你有用,或者exe文件你用的很順手,麻煩點個贊
成品如下
總結
以上是生活随笔為你收集整理的c++ opencv 将视频转化成字符串的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。