图像增强标注代码(旋转、平移、翻转)

图像增强标注代码C++(旋转、平移、翻转)(已有新方法,链接如下)

已封装类
最近在做项目时需要用到labelme进行数据集标注,大量图像以及增强图像的标注十分费时,因此想到利用代码对标注好的原始图像进行处理来扩充数据集。

使用说明

	注意:暂时仅用于只有一个类别标签的txt文件(后续有需求会进行更改),确保txt文件的命名格式为6位数字,从000001.txt到999999.txt为止,序号可以不连续

使用前需要更改:
一:folder地址和读取txt、写入txt的地址
二:图片大小、平移距离、旋转角度(逆时针)、旋转中心(归一化坐标)等参数

旋转

constexpr auto PI = 3.141592654;
const auto ang = 90;//逆时针旋转角度

int h = 1236;
int w = 1626;//原始图片大小

double x = 0.5;
double y = 0.5;//旋转中心的归一化坐标

Point2d rotate(Point2d a, Point2d a0, double angle) {
    a = Point2d(a.x * h, a.y * w);
    a0 = Point2d(a0.x * h, a0.y * w);
	Point2d b;
	b.x = (a.x - a0.x) * cos(angle * PI / 180) - (a.y - a0.y) * sin(angle * PI / 180) + a0.x;
	b.y = (a.x - a0.x) * sin(angle * PI / 180) + (a.y - a0.y) * cos(angle * PI / 180) + a0.y;
    b = Point2d(b.x / h, b.y / w);
	return b;
}

平移

int t_h = -100;
int t_w = -100;//平移距离

Point2d translation(Point2d a, int th, int tw) {
    a = Point2d(a.x * h, a.y * w);
    Point2d b;
    b = Point2d(a.x + th, a.y + tw);
    b = Point2d(b.x / h, b.y / w);
    return b;
}

翻转

Point2d horizontal(Point2d a) {//水平翻转
    Point2d b;
    b = Point2d(a.x, 1 - a.y);
    return b;
}

Point2d vertical(Point2d a) {//垂直翻转
    Point2d b;
    b = Point2d(1 - a.x, a.y);
    return b;
}

完整代码(以旋转为例)

#include <opencv2/opencv.hpp>
#include <fstream>
#include <sstream>

using namespace std;
using namespace cv;
  
constexpr auto PI = 3.141592654;
const auto ang = 90;//逆时针旋转角度;                                  需要更改

int h = 1236;
int w = 1626;//图片大小

double x = 0.5;
double y = 0.5;//旋转中心的归一化坐标

//旋转:点a以a0为圆心逆时针旋转angle度(角度制)到点b
Point2d rotate(Point2d a, Point2d a0, double angle) {
    a = Point2d(a.x * h, a.y * w);
    a0 = Point2d(a0.x * h, a0.y * w);
	Point2d b;
	b.x = (a.x - a0.x) * cos(angle * PI / 180) - (a.y - a0.y) * sin(angle * PI / 180) + a0.x;
	b.y = (a.x - a0.x) * sin(angle * PI / 180) + (a.y - a0.y) * cos(angle * PI / 180) + a0.y;
    b = Point2d(b.x / h, b.y / w);
	return b;
}
//double转string
string dou2str(double num) {
    stringstream ss;
    string str;
    ss << num;
    ss >> str;
    return str;
}
//坐标变换
void Transform(string &strx, string &stry) {
    double Tx = atof(strx.c_str());
    double Ty = atof(stry.c_str());
    Point2d point = Point2d(Tx, Ty);
    Point2d T_point = rotate(point, Point2d(x, y), ang);
    Tx = T_point.x;
    Ty = T_point.y;
    strx = dou2str(Tx);
    stry = dou2str(Ty);
}

int main()
{
    cv::String folder = "./txt/*.txt";//需要转换的txt文件总数
    std::vector<cv::String> imagePathList;
    cv::glob(folder, imagePathList, false);

    int index = 1;
    for (auto fileName : imagePathList) {

        bool ispoint = false;//跳过第一个表示类别的“0”
        bool space_y = false;//下一个空格是否是x坐标前的空格
        bool space_x = false;//下一个空格是否是y坐标前的空格
        char intxtName[50];
        char outtxtName[50];
        if (index < 10) {//按名字读取txt文件(需要优化)
            sprintf_s(intxtName, "./txt/00000%d.txt", index);
            sprintf_s(outtxtName, "./D%dtxt/00000%d.txt", ang, index);
        }
        if (index >= 10 && index < 100) {
            sprintf_s(intxtName, "./txt/0000%d.txt", index);
            sprintf_s(outtxtName, "./D%dtxt/0000%d.txt", ang, index);
        }
        if (index >= 100 && index < 1000) {
            sprintf_s(intxtName, "./txt/000%d.txt", index);
            sprintf_s(outtxtName, "./D%dtxt/000%d.txt", ang, index);
        }
        if (index >= 1000 && index < 10000) {
            sprintf_s(intxtName, "./txt/00%d.txt", index);
            sprintf_s(outtxtName, "./D%dtxt/00%d.txt", ang, index);
        }
        if (index >= 10000 && index < 100000) {
            sprintf_s(intxtName, "./txt/0%d.txt", index);
            sprintf_s(outtxtName, "./D%dtxt/0%d.txt", ang, index);
        }
        if (index >= 100000 && index < 1000000) {
            sprintf_s(intxtName, "./txt/%d.txt", index);
            sprintf_s(outtxtName, "./D%dtxt/%d.txt", ang, index);
        }
        ifstream fIn(intxtName);
        ofstream fOut(outtxtName);

        if (!fIn) {
            index++;
            cout << "Open input file faild." << endl;
            continue;
        }
        if (!fOut) cout << "Open output file faild." << endl;

        fIn >> noskipws;//读取所有字符(包括空格和回车)

        string str = "";
        string strx = "";
        string stry = "";
        while (!fIn.eof())
        {
            char c;
            fIn >> c;
            if (ispoint) {
                if (c != ' ') {
                    str = str + c;
                }
                else if (space_y) {
                    strx = str;
                    str = "";
                    space_y = false;
                    space_x = true;
                }
                else if (space_x) {
                    stry = str;
                    str = "";
                    space_y = true;
                    space_x = false;
                }
                if (strx != "" && stry != "") {//在检测到x坐标前的空格时有strx!=""&& stry!=""
                    Transform(strx, stry);
                    fOut << " " << stry << " " << strx;
                    strx = "";
                    stry = "";
                }
            }
            else {
                ispoint = true;
                space_y = true;
                fOut << c;
            }

        }
        strx = str;
        Transform(strx, stry);//最后一个y坐标后没有空格,在循环外进行最后一个坐标的旋转
        fOut << " " << stry << " " << strx;
        strx = "";
        stry = "";

        fIn.close();
        fOut.close();
        index++;
    }
    return 0;
}
  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
基于深度学习的图像增强是指利用深度学习技术对图像进行处理,以提高图像的质量、增强细节或改善视觉效果。以下是一个基于深度学习的图像增强代码的简单介绍: 1. 使用深度学习库:首先,你需要选择一个深度学习库,如TensorFlow、PyTorch或Keras,作为代码的基础。 2. 数据准备:准备用于训练的图像数据集。可以使用公开的数据集,如ImageNet,或者自己收集和标注数据。 3. 构建模型:使用深度学习库构建一个图像增强模型。可以选择使用已有的预训练模型,如VGG、ResNet等,或者自己设计和训练一个模型。 4. 数据增强:在训练过程中,可以使用各种数据增强技术来扩充数据集,如旋转、缩放、平移翻转等。这样可以增加数据的多样性,提高模型的泛化能力。 5. 模型训练:使用准备好的数据集和模型,进行模型的训练。可以使用随机梯度下降等优化算法来优化模型参数。 6. 模型评估:训练完成后,使用测试集对模型进行评估,计算模型的准确率、精度、召回率等指标。 7. 图像增强应用:使用训练好的模型对新的图像进行增强。可以将图像输入到模型中,得到增强后的图像输出。 这只是一个简单的介绍,实际的代码实现可能更加复杂和细致。具体的代码实现可以根据你选择的深度学习库和具体需求进行进一步的研究和开发。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值