2.OpenCvSharp与Picturebox实现图片打开、读取、灰度转换与保存
0.项目概述
项目实现了图片的打开,并在picturebox上显示,使用了OpenCvSharp中的函数实现图片的宽度、高度、通道数等信息的读取,还使用了OpenCvSharp中的函数实现了灰度转换再在picturebox上更新,最后将图片保存到本地磁盘。
源代码下载地址:https://download.youkuaiyun.com/download/sunsoldeir1/86267164
1.文件->新建->项目
2.选择Windows窗体应用(.NET Framework),再点“下一步”。
3.自行设置“项目名称”和“位置”后,再点“创建”。
4.在工具箱中寻找Picturebox、button、label并拖到到窗体中 ,并更改button属性中的”Font”字体大小和”Text”。
5.左键选择picturebox,在属性中将SizeMode设置为StretchImage。
6.在Form中点击右键,选择“查看代码”,之后安装OpenCvSharp
7.下载并引用using OpenCvSharp;
8下载并引用using OpenCvSharp.Extensions;
9.初始化定义如下变量
string pathname;//定义图片打开路径
Mat Img ;//用 Mat类定义原始图片
Mat ImgCvt;//用 Mat类定义灰度图片
Bitmap bitmap;//Bitmap类定义picturebox读取的图片
10.双击“打开图片”按钮
加入如下代码,实现图片打开:
OpenFileDialog file = new OpenFileDialog();//OpenFileDialog是一个类,实例化此类可以设置弹出一个文件对话框
file.Filter = "JPG(*.JPG;*.JPEG);PNG文件(*.PNG);bmp文件(*.BMP);gif文件(*.GIF)|*.jpg;*.jpeg;*.png;*.bmp;*.gif";//文件类型过滤,只可选择图片的类型
file.ShowDialog();//显示通用对话框
if (file.FileName != string.Empty)
{
try
{
pathname = file.FileName;
Img = Cv2.ImRead(pathname);//读取路径下的图片
// Cv2.ImShow("1", Img);
pictureBox1.Load(pathname); //pictureBox1直接加载
label1.Text = "图片路径:" + pathname; //显示图片路径
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
实现效果为:
11.双击“读取图片信息”按钮,来实现图片宽度、图片高度、图片通道数等信息的读取。
加入以下代码:
if (pathname == null || Img.Empty())//判断原始图片是否为空
{
MessageBox.Show("没有打开图片");
return;
}
else
{
MessageBox.Show("图片宽度:" + Img.Cols+ ",图片高度:" + Img.Rows + ",图片通道数:" + Img.Channels(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
12.双击“转为灰度图”按钮,来实现图片从彩色图转为灰度图。
加入以下代码:
if (pathname==null|| Img.Empty())//判断原始图片是否为空
{
MessageBox.Show("没有打开图片");
return ;
}
else
{
ImgCvt = Img;
Cv2.CvtColor(ImgCvt, ImgCvt, ColorConversionCodes.BGR2GRAY);//转为灰度图
bitmap = BitmapConverter.ToBitmap(ImgCvt); //把Mat格式的图片转换成Bitmap
pictureBox1.Image = bitmap;
}
实现效果如下:
13.双击“保存图片”按钮,来实现图片的保存。
加入以下代码:
```csharp
```csharp
SaveFileDialog saveImageDialog = new SaveFileDialog();
saveImageDialog.Title = "图片保存";
saveImageDialog.Filter = "jpg图片|*.JPG|gif图片|*.GIF|png图片|*.PNG|jpeg图片|*.JPEG|BMP图片|*.BMP";//文件类型过滤,只可选择图片的类型
saveImageDialog.FilterIndex = 1;//设置默认文件类型显示顺序
saveImageDialog.FileName = "图片保存"; //设置默认文件名,可为空
saveImageDialog.RestoreDirectory = true; //OpenFileDialog与SaveFileDialog都有RestoreDirectory属性,这个属性默认是false,打开一个文件后,那么系统默认目录就会指向刚才打开的文件。如果设为true就会使用系统默认目录
if (saveImageDialog.ShowDialog() == DialogResult.OK)
{
string fileName = saveImageDialog.FileName.ToString();
if (fileName != "" && fileName != null)
{
string fileExtName = fileName.Substring(fileName.LastIndexOf(".") + 1).ToString();
System.Drawing.Imaging.ImageFormat imgformat = null;
if (fileExtName != "")
{
switch (fileExtName)
{
case "jpg":
imgformat = System.Drawing.Imaging.ImageFormat.Jpeg;
break;
case "png":
imgformat = System.Drawing.Imaging.ImageFormat.Png;
break;
case "gif":
imgformat = System.Drawing.Imaging.ImageFormat.Gif;
break;
case "bmp":
imgformat = System.Drawing.Imaging.ImageFormat.Bmp;
break;
default:
imgformat = System.Drawing.Imaging.ImageFormat.Jpeg;
break;
}
try
{
MessageBox.Show("保存路径:" + fileName, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
pictureBox1.Image.Save(fileName, imgformat);
}
catch
{
MessageBox.Show("图片保存失败!");
}
}
}
}
实现效果如下: