用于制作数据集的部分代码,包含随机裁剪,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);
}
}
}
*/
}