OpenCV在图像上抠取指定区域平移、缩放的图像

来源:互联网 发布:linux android 模拟器 编辑:程序博客网 时间:2024/03/29 22:17

下面程序中的readtxt()函数实现在博客http://blog.csdn.net/susu_love/article/details/53218210


#include "stdafx.h"#include <stdio.h>#include <stdlib.h>#include <iostream>#include <fstream>#include <opencv2/core/core.hpp>#include <opencv2/highgui/highgui.hpp>#include <opencv2/imgproc/imgproc.hpp>#include <opencv2/objdetect/objdetect.hpp>#include <opencv2/ml/ml.hpp>#include <string>using namespace std;using namespace cv;#define IMAGENUM 105#define OBJNUM 168struct FRAME{int framenum;int objnum;int startx;int starty;int width;int height;} frameInfo[IMAGENUM];Rect new_location(Rect rect, struct FRAME newframe, Mat src, int casenum){switch(casenum){case 0://原位置rect.x = newframe.startx;rect.y = newframe.starty;rect.width = newframe.width;rect.height = newframe.height;break;case 1://左移10%rect.x = newframe.startx - newframe.width*0.1;rect.y = newframe.starty;rect.width = newframe.width;rect.height = newframe.height;break;case 2://右移10%rect.x = newframe.startx + newframe.width*0.1;rect.y = newframe.starty;rect.width = newframe.width;rect.height = newframe.height;break;case 3://上移10%rect.x = newframe.startx;rect.y = newframe.starty - newframe.height*0.1;rect.width = newframe.width;rect.height = newframe.height;break;case 4://下移10%rect.x = newframe.startx;rect.y = newframe.starty + newframe.height*0.1;rect.width = newframe.width;rect.height = newframe.height;break;case 5://左上角rect.x = newframe.startx - newframe.width*0.05;rect.y = newframe.starty - newframe.height*0.05;rect.width = newframe.width;rect.height = newframe.height;break;case 6://左下角rect.x = newframe.startx - newframe.width*0.1;rect.y = newframe.starty + newframe.height*0.1;rect.width = newframe.width;rect.height = newframe.height;break;case 7://右上角rect.x = newframe.startx + newframe.width*0.1;rect.y = newframe.starty - newframe.height*0.1;rect.width = newframe.width;rect.height = newframe.height;break;case 8://右下角rect.x = newframe.startx + newframe.width*0.1;rect.y = newframe.starty + newframe.height*0.1;rect.width = newframe.width;rect.height = newframe.height;break;case 9://缩小rect.x = newframe.startx + newframe.width*0.05;rect.y = newframe.starty + newframe.height*0.05;rect.width = newframe.width*0.9;rect.height = newframe.height*0.9;break;case 10://放大rect.x = newframe.startx - newframe.width*0.05;rect.y = newframe.starty - newframe.height*0.05;rect.width = newframe.width*1.1;rect.height = newframe.height*1.1;break;default: break;}if(rect.x < 0)rect.x = 0;if(rect.y < 0)rect.y = 0;if(rect.x + rect.width > src.cols)rect.width = src.cols - rect.x;if(rect.y + rect.height > src.rows)rect.height = src.rows - rect.y;return rect;}int _tmain(int argc, _TCHAR* argv[]){int imageCount = 0;int needframe = 105; //可设置一共取多少帧int interval = IMAGENUM/needframe;int t = readtxt(interval);if (!t){cout<<"读取真实值完成"<<endl;}string srcImg;char saveName[256];ifstream fin("ThreeVideoList.txt");int temp = 0;for(int num=-1; num<IMAGENUM;){Mat src;if ((num-frameInfo[temp].framenum)){num++;getline(fin,srcImg);cout<<"处理:"<<srcImg<<endl;srcImg = "E:\\数据集\\图像帧\\ThreeVideoSelected\\" + srcImg;}src = imread(srcImg);resize(src,src,Size(1280,720));if (!(num-frameInfo[temp].framenum)){for (int casenum=0;casenum<11;casenum++) //通过casenum可以控制所抠取的图像是经过了何种变换{Rect rect;rect = new_location(rect,frameInfo[temp],src,casenum);Mat image = src(rect);sprintf(saveName,"E:\\数据集\\目标区域图像\\目标区域变换\\ThreeVideo\\%03d.jpg",imageCount++);imwrite(saveName,image);}temp++;}}return 0;}


0 0
原创粉丝点击