在处理数据集的时候,有时候会需要把直接的数据集图片大小进行调整,所以写了个脚本
/*
调整图片大小
*/
#include "opencv2/imgproc.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/ml.hpp"
#include "opencv2/objdetect.hpp"
#include
using namespace std;
using namespace cv;
vector<string> getOrderedNames(vector<cv::String> filenames, cv::String folder) {
vector<string> nameArr;
string suffixName;
for (size_t i = 0; i < filenames.size(); ++i)
{
string::size_type iPos = filenames[i].find_last_of('\\') + 1;
string filename = filenames[i].substr(iPos, filenames[i].length() - iPos);
string name = filename.substr(0, filename.rfind("."));
suffixName = filename.substr(filename.rfind("."), filename.length() - 1);//获取后缀名
nameArr.emplace_back(name);
}
sort(nameArr.begin(), nameArr.end(), [](string a, string b) {return stoi(a) < stoi(b); });
vector<string> orderedFilenames;
for (size_t i = 0; i < filenames.size(); ++i)
{
string orderedName = folder + "\\" + nameArr[i] + suffixName;
orderedFilenames.emplace_back(orderedName);
//cout << "改变后:" << orderedFilenames[i] << endl;
}
return orderedFilenames;
}
int main()
{
int imgs_h = 480;//想要的宽高
int imgs_w = 640;
std::vector<cv::String> filenames; // notice here that we are using the Opencv's embedded "String" class
cv::String folder = "C:\\Users\\大喵喵\\Desktop\\大气湍流\\turbulence_dataset\\algorithm_simulated_videos\\BSD\\train\\3\\Blur\\RGB"; // again we are using the Opencv's embedded "String" class
cv::glob(folder, filenames); // new function that does the job ;-)
size_t count = filenames.size();
if (count == 0)
{
std::cout << "file " << folder << " not exits" << std::endl;
return -1;
}
vector<string> orderedFilenames = getOrderedNames(filenames, folder);
for (size_t i = 0; i < filenames.size(); ++i)
{
cout << "顺序:" << orderedFilenames[i] << endl;
cv::Mat srcImage = imread(orderedFilenames[i]);
resize(srcImage, srcImage, Size(imgs_w, imgs_h), 0, 0, INTER_LINEAR);
imwrite(orderedFilenames[i], srcImage);
}
}
来源:[1]
// 01_Combine_Two_Images.cpp
// 环境 VS2017 + OpenCV4.4.0
// 功能介绍:用于将两张图片拼接成一张大图(以左右拼接为例),俗称的硬拼方法
// 特点:简单粗暴,现实应用对拍摄条件要求苛刻,适用性差
//#include "pch.h"
#include
#include
#include
using namespace std;
using namespace cv;
int main()
{
Mat img1 = imread("01.jpg");
Mat img2 = imread("02.jpg");
if (img1.empty() || img2.empty())
{
cout << "加载图片失败,请检查对应路径图片是否存在!" << endl;
return 1;
}
imshow("src1", img1);
imshow("src2", img2);
int w1 = img1.cols; int h1 = img1.rows;
int w2 = img2.cols; int h2 = img2.rows;
int width = w1 + w2; int height = max(h1, h2);
Mat resultImg = Mat(height, width, CV_8UC3, Scalar::all(0));
Mat ROI_1 = resultImg(Rect(0, 0, w1, h1));
Mat ROI_2 = resultImg(Rect(w1, 0, w2, h2));
img1.copyTo(ROI_1);
img2.copyTo(ROI_2);
imshow("result", resultImg);
imwrite("result.jpg", resultImg);
waitKey(0);
return 0;
}
#include
#include
#include
#include
using namespace std;
using namespace cv;
int main()
{
cv::Mat img;
img = imread("D:/workplace/ForGAN/ForDataOperation/ForCVdataOperation/ForFramesToLongVideo/TestDate/testVideo/testvideoDeblurGAN/results0.png");
imshow("原图", img);
cv::Rect m_select0 = Rect(512,0, 256, 256);
cv::Rect m_select1 = Rect(256,0, 256, 256);
cv::Rect m_select2 = Rect(512,0, 256, 256);
Mat ROI0 = img(m_select0);
Mat ROI1 = img(m_select1);
Mat ROI2 = img(m_select2);
/*imshow("剪裁图", ROI0);
imshow("剪裁图", ROI1);
imshow("剪裁图", ROI2);*/
waitKey(0);
return 0;
}
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include "opencv2/imgproc.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/ml.hpp"
#include "opencv2/objdetect.hpp"
#include
using namespace std;
using namespace cv;
vector<string> getOrderedNames(vector<cv::String> filenames, cv::String folder) {
vector<string> nameArr;
string suffixName;
for (size_t i = 0; i < filenames.size(); ++i)
{
string::size_type iPos = filenames[i].find_last_of('\\') + 1;
string filename = filenames[i].substr(iPos, filenames[i].length() - iPos);
string name = filename.substr(0, filename.rfind("."));
suffixName = filename.substr(filename.rfind("."), filename.length() - 1);//获取后缀名
nameArr.emplace_back(name);
}
sort(nameArr.begin(), nameArr.end(), [](string a, string b) {return stoi(a) < stoi(b); });
vector<string> orderedFilenames;
for (size_t i = 0; i < filenames.size(); ++i)
{
string orderedName = folder + "\\" + nameArr[i] + suffixName;
orderedFilenames.emplace_back(orderedName);
//cout << "改变后:" << orderedFilenames[i] << endl;
}
return orderedFilenames;
}
int main()
{
std::vector<cv::String> filenames; // notice here that we are using the Opencv's embedded "String" class
cv::String folder = "D:/workplace/ForGAN/ForDataOperation/ForCVdataOperation/ForFramesToLongVideo/TestDate/TSWGANresultsFrames/1"; // again we are using the Opencv's embedded "String" class
string outdir = "D:/workplace/ForGAN/ForDataOperation/ForCVdataOperation/ForFramesToLongVideo/TestDate/TSWGANresultsFrames/tmp/";
cv::glob(folder, filenames); // new function that does the job ;-)
size_t count = filenames.size();
if (count == 0)
{
std::cout << "file " << folder << " not exits" << std::endl;
return -1;
}
vector<string> orderedFilenames = getOrderedNames(filenames, folder);
for (size_t i = 0; i < filenames.size(); ++i)
{
cout << "顺序:" << orderedFilenames[i] << endl;
Mat img = imread(orderedFilenames[i]);
imshow("原图", img);
int imgH = img.rows;
int imgw = img.cols;
cout << "imgH:" << imgH << endl;
cout << "iimgw:" << imgw << endl;
cv::Rect m_select0 = Rect(0, 0, imgw /2, imgH);
cv::Rect m_select1 = Rect(imgw /2, 0, imgw /2, imgH);
//cv::Rect m_select2 = Rect(512, 0, 256, 256);
Mat ROI0 = img(m_select0);
Mat ROI1 = img(m_select1);
//Mat ROI2 = img(m_select2);
string str_i = to_string(i);
string test = outdir + "left" ;
if (_access(test.c_str(), 0) == -1) //如果文件夹不存在
_mkdir(test.c_str()); //则创建
string test2 = outdir + "right";
if (_access(test2.c_str(), 0) == -1) //如果文件夹不存在
_mkdir(test2.c_str()); //则创建
imwrite(test + "/" + str_i + ".jpg", ROI0);//保存图像,支持PNG、JPG、Tiff文件格式
imwrite(test2+ "/" + str_i + ".jpg", ROI1);
//imwrite(outdir + "3/" + str_i + ".jpg", ROI2);
}
}