第一篇博客就随便写点吧…
正在做车辆检测的毕设,需要获取5000+的负样本图片,手动截取不现实就随机生成一些,下面是代码简单易懂就不多解释啦
#include <opencv2\opencv.hpp>
#include <iostream>
#include <cstring>
#include<stdio.h>
#include<stdlib.h>
#include <time.h>
//#define random(x) (rand()%x)
using namespace cv;
int main(int argc,char* argv){
char *cstr = new char[20];
int n = 1,pos_x,pos_y;
Mat img = imread("C:\\Users\\Ruby\\Desktop\\image7.3300.bmp");
Mat ROI1;
srand((int)time(0));
while (n < 1001){
pos_x = int(rand() % 1920);//生成随机数,本人图片大小1920*1080
pos_y = int(rand() % 1080);
if (pos_x + 120 <= 1920 && pos_y + 120 <= 1080){
ROI1 = img(Rect(pos_x, pos_y, 120, 120));//输出图片大小120*120
sprintf(cstr, "%s%d%s", "neg_7.", n++, ".bmp");//按命名格式输出图片
imwrite(cstr, ROI1);
}
}
}