OpenCV常用小工具……

时间:2022-11-18 18:26:56

图像批量缩放(改名)小工具(OpenCV2.4.4+Qt5.2.1+MSVC2010):

#include <opencv2/opencv.hpp>//我很懒

using namespace std;
using namespace cv;

int main(){
string src_img_name = "D:yaleface/";
string dst_img_name = "D:yale/";
char charn[3],chari[3],charj[3];

//把s1.bmp..s165.bmp改名为i_sj.bmp并缩小图像
for(int i=1;i<=15;i++){
for(int j=1;j<=11;j++){
_itoa((i
-1)*11+j,charn,10);
_itoa(i,chari,
10);
_itoa(j,charj,
10);

//输入文件夹的文件s1.bmp...s165.bmp
src_img_name+="s";
src_img_name
+=charn;
src_img_name
+=".bmp";

Mat src_img
= imread(src_img_name,0);
Mat dst_img;
resize(src_img,dst_img,Size(
40,40),0,0,INTER_LINEAR);

//输出文件夹中的文件名1_s1..1_s11..2_s1..2_s11....15_s11.
dst_img_name+=chari;
dst_img_name
+="_s";
dst_img_name
+=charj;
dst_img_name
+=".bmp";

imwrite(dst_img_name,dst_img);
src_img_name
= "D:yaleface/";
dst_img_name
= "D:yale/";
clog
<<(i-1)*11+j<<endl;
}
}
}

 

2. 批量图片写成视频(OpenCV2.4.4+Qt5.2.1+MSVC2010):

#include <opencv2/opencv.hpp>
#include
<iostream>

using namespace std;
using namespace cv;

int main(){
//我的环境下所有目录都不能有中文.
string dir_name = "F:/baiduyundownload/face/1_1_05(06)_0/prob/dongnanmeneast_15_1920x1080_30_R1/";
char img_name[6];

Mat img_src
= imread("F:baiduyundownload/face/1_1_05(06)_0/prob/dongnanmeneast_15_1920x1080_30_R1/15980.jpg");
if(img_src.empty())
cerr
<<"imread image error";

VideoWriter output_src(
"D:faceDetect.avi",CV_FOURCC('M','J','P','G'),10,img_src.size(),1);
//我的图片名是15980.jpg,15982.jpg...18000.jpg
for(int i=15980;i<18000;i+=2){

/****图片名字处理****/
_itoa(i,img_name,
10);
dir_name
+=img_name;
dir_name
+=".jpg";

img_src
=imread(dir_name);//显示
if(img_src.empty())
return 0;
output_src
<<img_src;//写入
imshow("src",img_src);

char c = waitKey(10);
if(27==c)
return 0;

clog
<<i<<endl;
dir_name
= "F:/baiduyundownload/face/1_1_05(06)_0/prob/dongnanmeneast_15_1920x1080_30_R1/";
}
return 0;
}

 

3. 读取视频序列:

#include <opencv2/opencv.hpp>

using namespace std;
using namespace cv;

int main(){
VideoCapture capture(
"D:faceDetect.avi");
if(!capture.isOpened()){
clog
<<"Read Video Error";
return 0;
}

double videoFPS = capture.get(CV_CAP_PROP_FPS);
int videoDelay = 1000/videoFPS;
bool stop = false;
Mat frame;

while(!stop){
if(!capture.read(frame))
break;
imshow(
"SHOW",frame);
if(waitKey(videoDelay)>=0)
stop
= true;
}
}

 stop可以提供外部控制接口,如果不需要的话,也可以简化如下:

#include <opencv2/opencv.hpp>

using namespace std;
using namespace cv;

int main(){
VideoCapture capture(
"D:faceDetect.avi");
if(!capture.isOpened()){
clog
<<"Read Video Error";
return 0;
}

double videoFPS = capture.get(CV_CAP_PROP_FPS);
int videoDelay = 1000/videoFPS;
Mat frame;

while(capture.read(frame)){
if(frame.empty())
break;
imshow(
"Video Demo",frame);
if(waitKey(videoDelay)==27)
break;
}
}

 

 4. 剪切视频段:

#include <opencv2/opencv.hpp>
#include
<iostream>

using namespace std;
using namespace cv;

int main(){
//打开原输入视频
VideoCapture capture("C:tuike.avi");
if(!capture.isOpened()){
cout
<<"open failed";
return 0;
}
//设置剪切的起始位置
double position = 31680;
capture.
set(CV_CAP_PROP_POS_FRAMES,position);

//设置剪切后的视频属性
Mat frame;
capture.read(frame);
VideoWriter output_video(
"D:suozixie1.avi",CV_FOURCC('M','J','P','G'),45,frame.size(),1);

//循环剪切视频31681-40000帧
while(position<=40000){
if(!capture.read(frame)){
cout
<<"read failed";
break;
}
output_video
<<frame;
position
++;
}
}

 

5. 读写摄像头:

#include <opencv2/opencv.hpp>

using namespace std;
using namespace cv;

int main(){
VideoCapture camera(
0);//打开默认摄像头
if(!camera.isOpened()){
clog
<<"Read Video Error";
return 0;
}

Mat frame;
camera.read(frame);
//写入的视频路径,编码方式,FPS,大小,彩色.
VideoWriter video("D:try.avi",CV_FOURCC('M','J','P','G'),15,frame.size(),1);
while(camera.read(frame)){
if(frame.empty())
break;
imshow(
"Video Demo",frame);
video
<<frame;
if(waitKey(33)==27)//ESC结束拍摄
break;
}
}

 

6. 从已知文本信息中分割图像:

#include <opencv2/opencv.hpp>
#include
<iostream>
#include
<fstream>
#include
<string>

using namespace std;
using namespace cv;

int main(){
//从已知文本信息中分割图像
ifstream input("D:ImageInfo.txt");
string sinput;

//我的环境下所有目录都不能有中文.
string dir_name = "D:dongnanmeneast_15_1920x1080_30_R1/";
string dst_name = "D:secface/";
char img_name[6];

input
>>sinput;
Mat img_src;
for(int i=15980;i<=18080;i+=2){
/****图片名字处理****/
_itoa(i,img_name,
10);
dir_name
+=img_name;
dir_name
+=".jpg";

int n=0;
while(((string)img_name+".jpg")==sinput.c_str()){
//从txt文档中获取face的位置x,y,w,h.
string l,t,r,b;
input
>>l>>t>>r>>b;
int x = atoi(l.c_str());
int y = atoi(t.c_str());
int width = atoi(r.c_str())-x;
int height = atoi(b.c_str())-y;

//保存图像的名称
char si[2];
itoa(n,si,
10);
dst_name
+= img_name;
dst_name
+="_face";
dst_name
+=si;
dst_name
+=".jpg";

//保存感兴趣区域即face图像
img_src=imread(dir_name);//read
Mat imgROI = img_src(Rect(x,y,width,height));
imwrite(dst_name,imgROI);

sinput
= "";
cout
<<n<<endl;
n
++;
input
>>sinput;
dst_name
= "D:secface/";
}
dir_name
= "D:dongnanmeneast_15_1920x1080_30_R1/";
cout
<<i<<endl;
}
cout
<<"complete";
return 0;
}