C#使用OpenCv图像批处理并改变图片大小并且重命名
程序员文章站
2023-11-18 17:59:28
添加nuget包选择最新版就好了安装完成后右键项目重新生成方案使用opencvsharp.cplusplus命名空间using opencvsharp.cplusplus;写一个函数用来改变图片大小并...
添加nuget包
选择最新版就好了
安装完成后右键项目重新生成方案
使用opencvsharp.cplusplus命名空间
using opencvsharp.cplusplus;
写一个函数用来改变图片大小并且重新命名
read_path是存储图片的文件夹的路径,write_path是改变后图片存储的文件夹路径,filename是图片重命名,width和height分别为图片修改后的宽和高
public static void resizeimg(string read_path,string write_path,string filename,double width,double height) { mat img1 = new mat(read_path); mat img2 = img1; opencvsharp.cplusplus.size size = new opencvsharp.cplusplus.size(width, height); cv2.resize(img1, img2, size, 0, 0); cv2.imwrite(write_path+"\\"+ filename, img2); }
在main函数中使用该函数
static void main(string[] args) { string dir_path = @"g:\testimg";//读取路径 string write_path = "g:\\testimg2";//存储路径 string[] filenames=directory.getfiles(dir_path); for (int i = 0; i < filenames.length; i++) { resizeimg(filenames[i], write_path, i + ".png", 200, 300); } console.readkey(); }
运行结果
完整代码
using system; using system.collections.generic; using system.drawing; using system.linq; using system.text; using system.threading.tasks; using opencvsharp; using opencvsharp.cplusplus; using system.runtime.interopservices; using system.io; namespace resizetest { class program { static void main(string[] args) { string dir_path = @"g:\testimg";//读取路径 string write_path = "g:\\testimg2";//存储路径 string[] filenames=directory.getfiles(dir_path); for (int i = 0; i < filenames.length; i++) { resizeimg(filenames[i], write_path, i + ".png", 200, 300); } console.readkey(); } public static void resizeimg(string read_path,string write_path,string filename,double width,double height) { mat img1 = new mat(read_path); mat img2 = img1; opencvsharp.cplusplus.size size = new opencvsharp.cplusplus.size(width, height); cv2.resize(img1, img2, size, 0, 0); cv2.imwrite(write_path+"\\"+ filename, img2); } } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。