opencv 批量 随机截取图片

第一篇博客就随便写点吧…

正在做车辆检测的毕设,需要获取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);
        }
    }
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值