當前位置: 妍妍網 > 碼農

C# OpenCvSharp 影像濾波技巧

2024-06-06碼農

效果

平滑濾波

平滑影像,減少噪點,讓畫面更加柔和。

銳化濾波

讓細節更明顯。

高通濾波

高通濾波器,比如拉普拉斯濾波器,可以幫助我們辨識邊緣,讓主體脫穎而出。

中值濾波

對抗椒鹽雜訊。

雙邊濾波

保持邊緣的同時平滑。

自訂濾波

計畫

程式碼

using OpenCvSharp;
using System;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Forms;
namespace OpenCvSharp_Demo
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
string fileFilter = "*.*|*.bmp;*.jpg;*.jpeg;*.tiff;*.tiff;*.png";
string startupPath;
string image_path;
Stopwatch stopwatch = new Stopwatch();
Mat image;
Mat result_image;
private void Form1_Load(object sender, EventArgs e)
{
startupPath = System.Windows.Forms.Application.StartupPath;
image_path = "1.jpg";
pictureBox1.Image = new Bitmap(image_path);
image = new Mat(image_path,ImreadModes.Grayscale);
}
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = fileFilter;
if (ofd.ShowDialog() != DialogResult.OK) return;
pictureBox1.Image = null;
pictureBox2.Image = null;
textBox1.Text = "";
image_path = ofd.FileName;
pictureBox1.Image = new Bitmap(image_path);
image = new Mat(image_path);
}
/// <summary>
/// 保存
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button3_Click(object sender, EventArgs e)
{
if (pictureBox2.Image == null)
{
return;
}
Bitmap output = new Bitmap(pictureBox2.Image);
var sdf = new SaveFileDialog();
sdf.Title = "保存圖片";
sdf.Filter = "Images (*.jpg)|*.jpg|Images (*.png)|*.png|Images (*.bmp)|*.bmp";
if (sdf.ShowDialog() == DialogResult.OK)
{
switch (sdf.FilterIndex)
{
case 1:
{
output.Save(sdf.FileName, ImageFormat.Jpeg);
break;
}
case 2:
{
output.Save(sdf.FileName, ImageFormat.Png);
break;
}
case 3:
{
output.Save(sdf.FileName, ImageFormat.Bmp);
break;
}
}
MessageBox.Show("保存成功,位置:" + sdf.FileName);
}
}
/// <summary>
/// 平滑濾波
/// 平滑影像,減少噪點,讓畫面更加柔和。
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button2_Click(object sender, EventArgs e)
{
if (image_path == "")
{
return;
}
stopwatch.Restart();
result_image = image.Clone();

//平均模糊
//Cv2.Blur(result_image, result_image, new OpenCvSharp.Size(5, 5));//(5, 5)是核大小,越大越模糊
//高斯模糊
Cv2.GaussianBlur(result_image, result_image, new OpenCvSharp.Size(5, 5), 0);//0表示自動選擇sigma值

double costTime = stopwatch.Elapsed.TotalMilliseconds;
textBox1.Text = $"耗時:{costTime:F2}ms";
pictureBox2.Image = new Bitmap(result_image.ToMemoryStream());
}
/// <summary>
/// 銳化濾波
/// 讓細節更明顯
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button4_Click(object sender, EventArgs e)
{
if (image_path == "")
{
return;
}
stopwatch.Restart();
result_image = new Mat();
//定義銳化摺積核
InputArray kernel = InputArray.Create<float>(new float[3, 3] {
{ -1, -1, -1 },
{ -1, 9, -1 },
{ -1, -1, -1 } });
Cv2.Filter2D(image, result_image, image.Type(), kernel);

double costTime = stopwatch.Elapsed.TotalMilliseconds;
textBox1.Text = $"耗時:{costTime:F2}ms";
pictureBox2.Image = new Bitmap(result_image.ToMemoryStream());
}
/// <summary>
/// 高通濾波
/// 高通濾波器,比如拉普拉斯濾波器,可以幫助我們辨識邊緣,讓主體脫穎而出
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button5_Click(object sender, EventArgs e)
{
if (image_path == "")
{
return;
}
stopwatch.Restart();

result_image = new Mat();
Cv2.Laplacian(image, result_image, MatType.CV_64F);
Cv2.ConvertScaleAbs(result_image, result_image);
result_image.ConvertTo(result_image, MatType.CV_8U);

double costTime = stopwatch.Elapsed.TotalMilliseconds;
textBox1.Text = $"耗時:{costTime:F2}ms";
pictureBox2.Image = new Bitmap(result_image.ToMemoryStream());
}
/// <summary>
/// 中值濾波
/// 對抗椒鹽雜訊
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button6_Click(object sender, EventArgs e)
{
if (image_path == "")
{
return;
}
stopwatch.Restart();

result_image = new Mat();
Cv2.MedianBlur(image, result_image, 5);

double costTime = stopwatch.Elapsed.TotalMilliseconds;
textBox1.Text = $"耗時:{costTime:F2}ms";
pictureBox2.Image = new Bitmap(result_image.ToMemoryStream());
}
/// <summary>
/// 雙邊濾波
/// 保持邊緣的同時平滑
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button7_Click(object sender, EventArgs e)
{
if (image_path == "")
{
return;
}
stopwatch.Restart();
result_image = new Mat();
Cv2.BilateralFilter(image, result_image, 9,75,75);
double costTime = stopwatch.Elapsed.TotalMilliseconds;
textBox1.Text = $"耗時:{costTime:F2}ms";
pictureBox2.Image = new Bitmap(result_image.ToMemoryStream());
}
/// <summary>
/// 自訂濾波
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button8_Click(object sender, EventArgs e)
{
if (image_path == "")
{
return;
}
stopwatch.Restart();
result_image = new Mat();
//定義摺積核
InputArray kernel = InputArray.Create<float>(new float[3, 3] {
{ 0, -1,0},
{ -1, 5, -1 },
{ 0, -1, 0 } });
Cv2.Filter2D(image, result_image, image.Type(), kernel);

double costTime = stopwatch.Elapsed.TotalMilliseconds;
textBox1.Text = $"耗時:{costTime:F2}ms";
pictureBox2.Image = new Bitmap(result_image.ToMemoryStream());
}
}
}