簡單的數據增強代碼(C++與opencv)

包括了圖片批量平移、旋轉、以及像素變換

#include <opencv2/opencv.hpp>
#include "opencv2/features2d.hpp"
#include <vector>
#include <algorithm>
#include <iostream>
#include "windows.h"
#include <stdio.h>
#include <time.h>
#include <math.h>  
#include <fstream>
#include <fstream>
#include <iostream>
#include <filesystem>
#define WINDOW_NAME "【程序窗口】"			
using namespace cv;using namespace std;
using std::ifstream;
using std::ofstream;
using std::cout;
using std::endl;
namespace fs = std::filesystem;Mat Translation(Mat& src, int x, int y)
{cv::Mat dst;cv::Size dst_sz = src.size();//定義平移矩陣cv::Mat t_mat = cv::Mat::zeros(2, 3, CV_32FC1);t_mat.at<float>(0, 0) = 1;t_mat.at<float>(0, 2) = x; //水平平移量t_mat.at<float>(1, 1) = 1;t_mat.at<float>(1, 2) = y; //豎直平移量//根據平移矩陣進行仿射變換cv::warpAffine(src, dst, t_mat, dst_sz);//顯示平移效果//cv::imshow("image", src);//cv::imshow("result", dst);return dst;
}Mat Rotate(Mat& src, double angle)
{cv::Mat dst;cv::Size src_sz = src.size();cv::Size dst_sz(src_sz.height, src_sz.width);int len = max(src.cols, src.rows);//指定旋轉中心cv::Point2f center(len / 2., len / 2.);//獲取旋轉矩陣(2x3矩陣)cv::Mat rot_mat = cv::getRotationMatrix2D(center, angle, 1.0);//根據旋轉矩陣進行仿射變換cv::warpAffine(src, dst, rot_mat, dst_sz);return dst;
}Mat K_Pixels(Mat& src, double K)
{cv::Mat dst = K * src;return dst;
}fs::path p{ "D:/一些文件/python code/AI/使用數據集/增強后的label與文件名.txt" };
ofstream output{ p };
void Save_filenameLabel(cv::String savedfilename)
{output << savedfilename << "   " << to_string(1) << endl;
}
int main()
{cv::String path = "C:/Users/LENOVO/Desktop/顏色轉換/";        //待處理圖片文件夾地址cv::String dest = "C:/Users/LENOVO/Desktop/增強后的圖片/";    //處理后圖片的保存地址cout << "獲取地址成功" << endl;cv::String savedfilename;std::vector<cv::String> filenames;int len = path.length();cv::Mat srcImg;cv::glob(path, filenames);                 //opencv里面用來讀取指定路徑下文件名的一個很好用的函數		for (int i = 0; i < 13; i++) {srcImg = cv::imread(filenames[i]);resize(srcImg, srcImg, Size(128, 128));Mat dstImg;int Count = 1;//*************************對圖片的處理部分***************************/向右邊下方平移for (int j = 1; j < 21; j++){dstImg = Translation(srcImg, j * 2, j * 2);savedfilename = dest + to_string(Count) + filenames[i].substr(len);std::cout << savedfilename << std::endl;cv::imwrite(savedfilename, dstImg);Save_filenameLabel(to_string(Count) + filenames[i].substr(len));cout << "第" << j << "張完成" << endl;Count++;}向左上方平移for (int j = 1; j < 21; j++){dstImg = Translation(srcImg, -1 * j * 2, -1 * j * 2);savedfilename = dest + to_string(Count) + filenames[i].substr(len);std::cout << savedfilename << std::endl;cv::imwrite(savedfilename, dstImg);cout << "第" << j << "張完成" << endl;Save_filenameLabel(to_string(Count) + filenames[i].substr(len));Count++;}向右邊上方平移for (int j = 1; j < 21; j++){dstImg = Translation(srcImg, j * 2, -1 * j * 2);savedfilename = dest + to_string(Count) + filenames[i].substr(len);std::cout << savedfilename << std::endl;cv::imwrite(savedfilename, dstImg);cout << "第" << j << "張完成" << endl;Save_filenameLabel(to_string(Count) + filenames[i].substr(len));Count++;}向左下方平移for (int j = 1; j < 21; j++){dstImg = Translation(srcImg, -1 * j * 2, j * 2);savedfilename = dest + to_string(Count) + filenames[i].substr(len);std::cout << savedfilename << std::endl;cv::imwrite(savedfilename, dstImg);cout << "第" << j << "張完成" << endl;Save_filenameLabel(to_string(Count) + filenames[i].substr(len));Count++;}//旋轉for (int j = 1; j < 21; j++){dstImg = Rotate(srcImg, j * 8);savedfilename = dest + to_string(Count) + filenames[i].substr(len);std::cout << savedfilename << std::endl;cv::imwrite(savedfilename, dstImg);cout << "第" << j << "張完成" << endl;Save_filenameLabel(to_string(Count) + filenames[i].substr(len));Count++;}//像素變化for (int j = 1; j < 11; j++){dstImg = K_Pixels(srcImg, 0.5 + j * 0.1);savedfilename = dest + to_string(Count) + filenames[i].substr(len);std::cout << savedfilename << std::endl;cv::imwrite(savedfilename, dstImg);cout << "第" << j << "張完成" << endl;Save_filenameLabel(to_string(Count) + filenames[i].substr(len));Count++;}//resize(srcImg, dstImg, Size(128, 128));//********************************************************************/}output.close();waitKey(0);return 0;
}

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/377359.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/377359.shtml
英文地址,請注明出處:http://en.pswp.cn/news/377359.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

aes模式_AES的完整形式是什么?

aes模式AES&#xff1a;高級加密標準 (AES: Advanced Encryption Standard) AES is an abbreviation of Advanced Encryption Standard, also known by its original name Rijndael. It is an arrangement of standard for the encryption of electronic data set up by the U.…

IOS ----UIButton用法詳解

這段代碼動態的創建了一個UIButton,并且把相關常用的屬性都列舉了.希望對大家有用. //這里創建一個圓角矩形的按鈕UIButton *button1 [UIButton buttonWithType:UIButtonTypeRoundedRect];// 能夠定義的button類型有以下6種&#xff0c;// typedef enum {// UIButtonTypeCusto…

針對一個class寫出它的內存管理池以及總結出allocator類(三個版本)

目錄示例版本1&#xff1a;per-class allocator,1示例版本2&#xff1a;per-class allocator,2最終版本&#xff1a;static allocator針對版本三進行macro如果我們不針對對象做內存管理&#xff0c;那么我們每次進行Foo* p new Foo(x);時總是會調用malloc函數。 盡管malloc函數…

kotlin 第一個程序_Kotlin程序添加兩個矩陣

kotlin 第一個程序Given two matrices, we have to add them. 給定兩個矩陣&#xff0c;我們必須將它們相加。 Example: 例&#xff1a; Input:matrix 1:[2, 3][4, 5][7, 1]matrix 2:[4, 6][9, 0][7, 6]Output:[6, 9][13, 5][14, 7] 在Kotlin中添加兩個矩陣的程序 (Progra…

ubuntu 切換用戶的命令[shell, linux]

使用ubuntu過程中免不了和shell(終端)打交道, 也不可避免在各種用戶之間進行切換, 從而實現對各帳戶的管理, 這個就涉及到了一個比較基礎又很重要的工作,怎么樣切換用戶, 對于LINUX老鳥來說,這個根本不值不提的東東卻讓新手撓頭不已, 現在給出普通用戶和超級用戶切換的命令(附圖…

曲苑雜壇--修改數據庫名和文件組名

/* 該腳本示例如何完整的修改一個數據庫的名稱. 數據庫為原名稱為DB_BEIJING&#xff0c;需要修改成DB_SHANGHAI nzperfect 2012.12.19 */--判斷是否存在同名的數據庫&#xff0c;以防止誤刪除 USE master GO IF EXISTS (SELECT name FROM sys.databases WHERE name NDB_BEIJI…

關于new handler與default、delete關鍵字

在https://blog.csdn.net/qq_42604176/article/details/111638568的operate_new源代碼長啥樣中談到過new handler。 當operator new不能夠分配出申請的內存時&#xff0c;會拋出bad_alloc 異常。有的編譯器會返回0. 當定義成new(nothrow) Foo&#xff1b;就不會拋異常&#xff…

模式匹配運算符–Shell

轉載&#xff1a;http://www.firefoxbug.net/?p722 Var/home/firefox/MyProgram/fire.login.name ${Variable#pattern}:如果模式匹配于變量值的開頭處&#xff0c;則刪除匹配的最短部分&#xff0c;并且返回剩下的部分 例子&#xff1a; [fire]$ echo ${Var#*/} [fire]$ home/…

河內塔問題_河內塔的Python程序

河內塔問題You are challenged for a challenge to find the number of moves required to move a stack of disks from one peg to another peg. Wait for a second, it sounds easy? Let’s find are what is going on and in this article, we are introducing a chapter o…

VC6、BC5、G2.9標準分配器一覽

目錄VC6標準分配器BC5標準分配器G2.9標準分配器VC6標準分配器 VCx中源碼可以在電腦路徑中找&#xff1a; [D:\Program Files\VisualStudio\Community\VC\Tools\MSVC\14.28.29333\include\xmemory] 不過太多了。大概在837行左右有關于allocator代碼。還是先看侯捷PPT上的吧。 …

【轉】shell 大括號、圓括號的使用

在這里我想說的是幾種shell里的小括號,大括號結構和有括號的變量&#xff0c;命令的用法&#xff0c;如下&#xff1a; PHP 代碼:1.${var} 2.$(cmd) 3.()和{} 4.${var:-string},${var:string},${var:string},${var:?string} 5.$((exp)) 6.$(var%pattern),$(var%%pattern),$(va…

css clear屬性_CSS中的clear屬性

css clear屬性CSS | 清除財產 (CSS | clear Property) We know so much about float property and how it is used for styling our web pages. If you do not remember the float property, lets help jog your memory. The float property is used to set the elements in a …

linux find prune排除某目錄或文件

http://blog.csdn.net/ysdaniel/article/details/7995681 查找cache目錄下不是html的文件 find ./cache ! -name *.html -type f列出當前目錄下的目錄名,排除includes目錄,后面的-print不能少 find . -path ./includes -prune -o -type d -maxdepth 1 -print排除多個目錄,”(“…

嵌入式指針embedded pointer的概念以及用法

目錄前言概念用法參考前言 在針對一個class寫出它的內存管理池以及總結出allocator類(三個版本)中內存管理池的第二個版本中涉及到了一個非常重要的概念&#xff1a;嵌入式指針。同時嵌入式指針也在G2.9版本的alloc中出現。現在整理一下網上的一些用法和概念 概念 嵌入式指針…

CLI配置和編址

實施基本編址方案&#xff1a; 在設計新網絡或規劃現有網絡時&#xff0c;至少要繪制一幅指示物理連接的拓撲圖&#xff0c;以及一張列出以下信息的地址表&#xff1a; l 設備名稱 l 設計中用到的接口 l IP 地址和子網掩碼 l 終端設備&#xff08;如 PC&#xff09;的默…

sql語句中的in用法示例_PHP中的循環語句和示例

sql語句中的in用法示例循環 (Loops) Imagine that we need a program that says "hello world" 100 times. Its quite stressful and boring to write the statement -- echo "hello world" — 100 times in PHP. This is where loop statement facilitate…

love2d教程30--文件系統

在游戲里少不了文件操作&#xff0c;在love2d里我們可以直接用lua自帶的io函數&#xff0c;如果不熟悉可以先讀一下我的lua文件讀寫。 相對lua&#xff0c;love2d提供了更多的函數&#xff0c; 方便我們操作文件。不過可能處于安全考慮&#xff0c;love2d只允許我們訪問兩個目錄…

std::alloc具體細節

G2.9 std::alloc的缺點&#xff1a; 1、在alloc::deallocate中沒有將拿到的內存資源還給操作系統&#xff0c;在多任務中將占用很大資源 2、alloc::deallocate函數沒有檢查傳入的p指針的有效性。在這里它默認p為alloc::allocate取得。 如果p并非alloc::allocate取得&#xf…

修改函數的返回地址

這篇隨筆源自今天看的這篇文章http://www.cnblogs.com/bluesea147/archive/2012/05/19/2508208.html 1. 如何修改函數返回地址 今天主要寫測試程序思考和驗證了一下這個問題&#xff0c;先看一下這個&#xff23;程序 1 #include <stdio.h>2 void foo(){3 int a,…

調試JavaScript代碼

JavaScript調試代碼 (JavaScript debugging the code) Debugging is the process of finding mistakes or bugs in the program. There are several ways one can debug their JavaScript code. This article will walk you through the strict mode in JavaScript and excepti…