常用的数据增强方法——图片镜像、图片缩放、图片旋转、加噪点

最近遇到了自己制作数据集,通过图片镜像,缩放,旋转和加噪点的方式增加图片数据集,c++代码如下:

  • 图片镜像
//rotate with mirror
#include   
#include   
using namespace std;
using namespace cv;
int main()
{
    int d = 0;
    int dx = 0;
    char image[100];
    char ad[1000];
    for (int i = 0; i < 18; i++) 
    {
        sprintf_s(image, "C:\\Users\\Desktop\\vechicle\\%d.jpg", ++d);
        Mat src = imread(image, CV_LOAD_IMAGE_UNCHANGED);
        Mat dst;
        dst.create(src.size(), src.type());
        Mat map_x;
        Mat map_y;
        map_x.create(src.size(), CV_32FC1);
        map_y.create(src.size(), CV_32FC1);
        for (int i = 0; i < src.rows; ++i)
        {
            for (int j = 0; j < src.cols; ++j)
            {
                //map_x.at(i, j) = (float)(src.cols - j);
                //map_y.at(i, j) = (float)i;  //水平
                map_x.at<float>(i, j) = (float)j;
                map_y.at<float>(i, j) = (float)(src.rows - i);  //垂直                                 
            }
        }
        remap(src, dst, map_x, map_y, CV_INTER_LINEAR);
        sprintf_s(ad, "C:\\Users\\Desktop\\novehicle\\%d.jpg", ++dx);
        imwrite(ad, dst);
    }
    return 0;
}
  • 图片缩放
//resize the image
#include
#include
using namespace cv;
using namespace std;
int main()
{
    Mat srcImage, dstImage,temp;
    int d = 0;
    int dx = 0;
    char ad[3000];
    char image[100];
    for (int i = 0; i < 15; i++)
    {
        sprintf_s(image, "C:\\Users\\Desktop\\novehicle\\%d.jpg", d++);
        srcImage = imread(image);
        temp = srcImage;
        //resize(temp, dstImage, Size(temp.cols/8 , temp.rows/8 ), (0, 0), (0, 0), 3);
        resize(temp, dstImage, Size(128, 128), (0, 0), (0, 0), 3);
        sprintf_s(ad, "C:\\Users\\Desktop\\128wu\\%d.jpg", ++dx);
        imwrite(ad,dstImage);
    }
    return 0;
}
  • 图片旋转
//rotate the image
#include
#include
#include
using namespace std;
using namespace cv;
int main() {
    cv::Mat dst;
    int dx = 0;
    int d = 1;
    char ad[3000];
    char image[100];
    for (int i = 0; i < 81; i++)
    {
        sprintf_s(image, "C:\\Users\\Desktop\\novechicle\\%d.jpg", d++);
        Mat src = imread(image);
        for (double angle = 0; angle <= 360; angle = angle + 8) {
            cv::Size dst_sz(src.cols, src.rows);
            cv::Point2f center(src.cols / 2., src.rows / 2.);
            cv::Mat rot_mat = cv::getRotationMatrix2D(center, angle, 1.0);
            cv::warpAffine(src, dst, rot_mat, dst_sz, INTER_LINEAR, BORDER_REPLICATE);
            sprintf_s(ad, "C:\\Users\\Desktop\\resize\\%d.jpg", ++dx);
            imwrite(ad, dst);
        }
    }
    return 0;
}
  • 加噪点
//add noise
#include   
#include   
#include
using namespace cv;
using namespace std;
void salt(Mat img, int n);
int main()
{
    int d = 0;
    int dx = 0;
    char image[1000];
    char ad[1000];
    for (int j = 0; j < 18; j++)
    {
        sprintf_s(image, "C:\\Users\\Desktop\\yuantu\\%d.jpg",++d);
        Mat img = imread(image);
        salt(img, 800);
        sprintf_s(ad, "C:\\Users\\Desktop\\novehicle\\%d.jpg",++dx);
        imwrite(ad,img);
    }
    return 0;
}
void salt(Mat img, int n)
{
    for (int k = 0; kint i = rand() % img.cols;
        int j = rand() % img.rows;
        if (img.channels() == 1)
        {
            img.at(j, i) = 255;
        }
        else if (img.channels() == 3)
        {
            img.at(j, i)[0] = 255;
            img.at(j, i)[1] = 255;
            img.at(j, i)[2] = 255;
        }
    }
}

你可能感兴趣的:(图像处理,图像处理)