数据集制作代码

用于制作数据集的部分代码,包含随机裁剪,RGB->BGR,删除分配等操作

@Echo Off&SetLocal ENABLEDELAYEDEXPANSION
FOR %%a in (*) do (
set "name=%%a"
set "name=!name: (=!"
set "name=!name:)=!"
ren "%%a" "!name!"
)
exit
#include <opencv2\opencv.hpp>
#include <iostream>
#include <cstring>
#include<stdio.h>
#include<stdlib.h>
#include <time.h>

#define ROI_W 512
#define ROI_H 512

using namespace cv;
using namespace std;

Mat BGRToRGB(Mat img)
{
	Mat image(img.rows, img.cols, CV_8UC3);
	for (int i = 0; i < img.rows; ++i)
	{
		//获取第i行首像素指针
		Vec3b *p1 = img.ptr<Vec3b>(i);
		Vec3b *p2 = image.ptr<Vec3b>(i);
		for (int j = 0; j<img.cols; ++j)
		{
			//将img的bgr转为image的rgb 
			p2[j][2] = p1[j][0];
			p2[j][1] = p1[j][1];
			p2[j][0] = p1[j][2];
		}
	}
	return image;
}

void deleteImage(const char* str)
{
	int result = remove(str);  //绝对或者相对路径都可以
	if (result == 0)
		cout << "delete succeeded!" << endl;
	else
		cout << "delete failed!" << endl;
}

int main(int argc, char* argv) {
	char *cstrin = new char[64];
	char *cstrout = new char[64];
	int n = 1, pos_x, pos_y;
	Mat ROI;
	srand((int)time(0));

	for (int i = 1; i < 5000; i+=4)
	{
		sprintf(cstrin, "%s%d%s", "E:\\cpptest\\ffmpegtest\\opencvtest\\data\\train\\PureIron\\2_", i, ".jpg");
		Mat img = imread(cstrin);
		if (img.empty())
			break;
		sprintf(cstrout, "%s%d%s", "E:\\cpptest\\ffmpegtest\\opencvtest\\data\\test\\PureIron\\2_", i, ".jpg");
		imwrite(cstrout, img);
		deleteImage(cstrin);
		cout << "count=" << i << endl;
	}
/*	for (int i = 1; i < 1000; i++)
	{
		n = 1;
		sprintf(cstrin, "%s%d%s", "E:\\cpptest\\ffmpegtest\\opencvtest\\T12Steel\\4_", i, ".jpg");
		Mat img = imread(cstrin);
		if (img.empty())
			break;
		while (n < 11)
		{
			pos_x = int(rand() % (int)img.cols);
			pos_y = int(rand() % (int)img.rows);
			if (pos_x + ROI_W <= img.cols && pos_y + ROI_H <= img.rows)
			{
				cout << i << "-" <<n << endl;
				ROI = img(Rect(pos_x, pos_y, ROI_W, ROI_H));
				sprintf(cstrout, "%s%d%s%d%s", "E:\\cpptest\\ffmpegtest\\opencvtest\\T12Steel1\\", i, "_", n++, ".jpg");
				if(n==7)
					imwrite(cstrout, BGRToRGB(ROI));
				else
					imwrite(cstrout, ROI);
			}
		}
	}
	*/
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值