You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

1679 lines
63 KiB

using AForge.Imaging;
using Customize.Controls;
using OpenCvSharp;
using ShenTun.ImageCollection.Common;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Forms.Design;
namespace ShenTun.ImageCollection.AForgeCamera
{
public partial class ImageEdit : UserControl
{
private Bitmap Bi; //定义位图对像
private string filePath;
private bool boolShow1 = false;//把截图的图片付给picturebox的时候用到
private Rectangle rect = Rectangle.Empty;
private Pen p;
private bool clipMode = false; // 是否开启裁切
private bool textMode = false; // 是否开启标注
public bool isSelected = false; // 是否点击图像
public bool isDrawing = false; // 是否点击图像
public System.Drawing.Point mouseDownPoint; // 记录鼠标点击坐标
private ExtendedTextPanel currentInputPanel = null;
private System.Drawing.Point mouseOff;//鼠标移动位置变量
private bool leftFlag;//标签是否为左键
private string trackBarType = "";
private SelectImageInfo selectImage = null;
private ApiService apiService = new ApiService();
private bool isShowList = false;
private int widthbox;
private int heightbox;
//定义一个枚举,表示拖动方向
public enum MouseDirection
{
Herizontal,//水平方向拖动,只改变窗体的宽度
Vertical,//垂直方向拖动,只改变窗体的高度
Declining,//倾斜方向,同时改变窗体的宽度和高度
None//不做标志,即不拖动窗体改变大小
}
#region 缩放、裁剪图像用到的变量
/// <summary>
///
/// </summary>
private int x1; //鼠标按下时横坐标
private int y1; //鼠标按下时纵坐标
private int width; //所打开的图像的宽
private int heigth; //所打开的图像的高
private bool HeadImageBool = false; // 此布尔变量用来判断pictureBox1控件是否有图片
#endregion
#region 画矩形使用到的变量
private System.Drawing.Point p1; //定义鼠标按下时的坐标点
private System.Drawing.Point p2; //定义移动鼠标时的坐标点
private System.Drawing.Point p3; //定义松开鼠标时的坐标点
#endregion
public ImageEdit()
{
InitializeComponent();
}
public void LoadImageAndInit(MemoryStream mstr, string _filePath)
{
Init(mstr, _filePath);
isShowList = false;
}
public void LoadImageAndInit(MemoryStream mstr, SelectImageInfo imageInfo)
{
selectImage = imageInfo;
isShowList = true;
Init(mstr, imageInfo.Image.localPathName);
}
public void Init(MemoryStream mstr,string _filePath)
{
if (Bi != null)
{
Bi.Dispose();
}
Bi = new Bitmap(mstr);
filePath = _filePath;
widthbox = this.pictBoxSour.Width;
heightbox = this.pictBoxSour.Height;
ImageCut IC = new ImageCut(0, 0, widthbox, heightbox);
this.pictBoxSour.Image = IC.KiCut((Bitmap)(this.GetSelectImage(widthbox, heightbox)));
this.btnList.Visible = isShowList;
}
/// <summary>
/// 额 ,这个在打开图片里用到了
/// 获取指定宽度和高度的图像即使图片和pictureBox1控件一样宽和高,返回值为图片Image
/// </summary>
/// <param name="Width表示宽"></param>
/// <param name="Height表示高"></param>
/// <returns></returns>
private System.Drawing.Image GetSelectImage(int Width, int Height)
{
//Image initImage = this.pictureBox1.Image;
System.Drawing.Image initImage = Bi;
//原图宽高均小于模版,不作处理,直接保存
if (initImage.Width <= Width && initImage.Height <= Height)
{
//initImage.Save(fileSaveUrl, System.Drawing.Imaging.ImageFormat.Jpeg);
return initImage;
}
else
{
//原始图片的宽、高
int initWidth = initImage.Width;
int initHeight = initImage.Height;
//非正方型先裁剪为正方型
if (initWidth != initHeight)
{
//截图对象
System.Drawing.Image pickedImage = null;
System.Drawing.Graphics pickedG = null;
//宽大于高的横图
if (initWidth > initHeight)
{
//对象实例化
pickedImage = new System.Drawing.Bitmap(initHeight, initHeight);
pickedG = System.Drawing.Graphics.FromImage(pickedImage);
//设置质量
pickedG.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
pickedG.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
//定位
Rectangle fromR = new Rectangle((initWidth - initHeight) / 2, 0, initHeight, initHeight);
Rectangle toR = new Rectangle(0, 0, initHeight, initHeight);
//画图
pickedG.DrawImage(initImage, toR, fromR, System.Drawing.GraphicsUnit.Pixel);
//重置宽
initWidth = initHeight;
}
//高大于宽的竖图
else
{
//对象实例化
pickedImage = new System.Drawing.Bitmap(initWidth, initWidth);
pickedG = System.Drawing.Graphics.FromImage(pickedImage);
//设置质量
pickedG.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
pickedG.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
//定位
Rectangle fromR = new Rectangle(0, (initHeight - initWidth) / 2, initWidth, initWidth);
Rectangle toR = new Rectangle(0, 0, initWidth, initWidth);
//画图
pickedG.DrawImage(initImage, toR, fromR, System.Drawing.GraphicsUnit.Pixel);
//重置高
initHeight = initWidth;
}
initImage = (System.Drawing.Image)pickedImage.Clone();
// //释放截图资源
pickedG.Dispose();
pickedImage.Dispose();
}
return initImage;
}
}
#region pictBoxSour控件裁剪事件
/// <summary>
/// 鼠标进入控件picturebox,光标十字 左侧要剪切的大图
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void pictBoxSour_MouseEnter(object sender, EventArgs e)
{
this.Cursor = Cursors.Cross;//十字线光标
}
/// <summary>
/// 鼠标离开picturebox时,光标默认 左侧要剪切的大图
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void pictBoxSour_MouseLeave(object sender, EventArgs e)
{
this.Cursor = Cursors.Default;//默认光标
}
#region 鼠标按下时发生的事件
/// <summary>
/// 鼠标按下事件 左侧要剪切的大图
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void pictBoxSour_MouseDown(object sender, MouseEventArgs e)
{
this.Cursor = Cursors.Cross;//十字光标
this.p1 = new System.Drawing.Point(e.X, e.Y);
x1 = e.X;
y1 = e.Y;
if (this.pictBoxSour.Image != null)
{
HeadImageBool = true;
}
else
{
HeadImageBool = false;
}
}
#endregion
#region 移动鼠标发生的事件
/// <summary>
/// 鼠标移动事件 左侧要剪切的大图
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void pictBoxSour_MouseMove(object sender, MouseEventArgs e)
{
//十字光标并且鼠标有按下
if (this.Cursor == Cursors.Cross && e.Button != MouseButtons.None)
{
this.p2 = new System.Drawing.Point(e.X, e.Y);
if ((p2.X - p1.X) > 0 && (p2.Y - p1.Y) > 0) //当鼠标从左上角向开始移动时P3坐标
{
this.p3 = new System.Drawing.Point(p1.X, p1.Y);
}
if ((p2.X - p1.X) < 0 && (p2.Y - p1.Y) > 0) //当鼠标从右上角向左下方向开始移动时P3坐标
{
this.p3 = new System.Drawing.Point(p2.X, p1.Y);
}
if ((p2.X - p1.X) > 0 && (p2.Y - p1.Y) < 0) //当鼠标从左下角向上开始移动时P3坐标
{
this.p3 = new System.Drawing.Point(p1.X, p2.Y);
}
if ((p2.X - p1.X) < 0 && (p2.Y - p1.Y) < 0) //当鼠标从右下角向左方向上开始移动时P3坐标
{
this.p3 = new System.Drawing.Point(p2.X, p2.Y);
}
this.pictBoxSour.Invalidate(); //使控件的整个图面无效,并导致重绘控件
}
}
#endregion
#region 释放鼠标 松开鼠标发生的事件,实例化ImageCut类对像
ImageCut IC1; //定义所画矩形的图像对像
/// <summary>
/// 鼠标放开事件 左侧要剪切的大图
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void pictBoxSour_MouseUp(object sender, MouseEventArgs e)
{
if (HeadImageBool)
{
width = this.pictBoxSour.Image.Width;
heigth = this.pictBoxSour.Image.Height;
if ((e.X - x1) > 0 && (e.Y - y1) > 0) //当鼠标从左上角向右下方向开始移动时发生
{
IC1 = new ImageCut(x1, y1, Math.Abs(e.X - x1), Math.Abs(e.Y - y1)); //实例化ImageCut1类
}
if ((e.X - x1) < 0 && (e.Y - y1) > 0) //当鼠标从右上角向左下方向开始移动时发生
{
IC1 = new ImageCut(e.X, y1, Math.Abs(e.X - x1), Math.Abs(e.Y - y1)); //实例化ImageCut1类
}
if ((e.X - x1) > 0 && (e.Y - y1) < 0) //当鼠标从左下角向右上方向开始移动时发生
{
IC1 = new ImageCut(x1, e.Y, Math.Abs(e.X - x1), Math.Abs(e.Y - y1)); //实例化ImageCut1类
}
if ((e.X - x1) < 0 && (e.Y - y1) < 0) //当鼠标从右下角向左上方向开始移动时发生
{
IC1 = new ImageCut(e.X, e.Y, Math.Abs(e.X - x1), Math.Abs(e.Y - y1)); //实例化ImageCut1类
}
//判断是否裁剪了
if (IC1 != null)
{
this.pictureBoxShow.Image = IC1.KiCut((Bitmap)(this.pictBoxSour.Image));
}
this.Cursor = Cursors.Cross;
}
else
{
this.Cursor = Cursors.Default;
}
//以下代码是消除原图片框内的黑色边框
//原理,将需要画的框设定为长宽均为0
p1 = new System.Drawing.Point(0, 0);
p2 = new System.Drawing.Point(0, 0);
p3 = new System.Drawing.Point(0, 0);
//强行触发原图画框的Paint事件。同时,请注意原图框的控件名称是pictBoxSour不是pictureBox1
this.pictBoxSour.Refresh();
}
#endregion
#region 重新绘制pictBoxSour控件,即移动鼠标画矩形
/// <summary>
/// paint事件 左侧要剪切的大图
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void pictBoxSour_Paint(object sender, PaintEventArgs e)
{
if (HeadImageBool)
{
p = new Pen(Color.Black, 1);//画笔
p.DashStyle = System.Drawing.Drawing2D.DashStyle.Solid;//.DashStyle.Dash;
rect = new Rectangle(p3, new System.Drawing.Size(System.Math.Abs(p2.X - p1.X), System.Math.Abs(p2.Y - p1.Y)));
e.Graphics.DrawRectangle(p, rect);
}
}
#endregion
#endregion
private void pictureBox_MouseDown(object sender, MouseEventArgs e)
{
if (!clipMode)
{
//if (currentImageBox != null)
//{
// currentImageBox.Focus();
//}
if (e.Button == MouseButtons.Left)
{
if (textMode)
{
mouseDownPoint.X = e.Location.X;
mouseDownPoint.Y = e.Location.Y;
Cursor.Current = Cursors.Cross;
isDrawing = true;
this.addTextInput(e);
}
else
{
mouseDownPoint.X = Cursor.Position.X;
mouseDownPoint.Y = Cursor.Position.Y;
Cursor.Current = Cursors.SizeAll;
isSelected = true;
}
}
}
}
private void pictureBox_MouseMove(object sender, MouseEventArgs e)
{
if (!clipMode)
{
if (isDrawing)
{
this.resizeTextInput(e);
mouseDownPoint.X = e.Location.X;
mouseDownPoint.Y = e.Location.Y;
}
else if (isSelected)
{
//currentPictureBox.Left = currentPictureBox.Left + (Cursor.Position.X - mouseDownPoint.X);
//currentPictureBox.Top = currentPictureBox.Top + (Cursor.Position.Y - mouseDownPoint.Y);
mouseDownPoint.X = Cursor.Position.X;
mouseDownPoint.Y = Cursor.Position.Y;
}
}
}
private void pictureBox_MouseUp(object sender, MouseEventArgs e)
{
if (textMode)
{
this.importTextInput();
}
isSelected = false;
isDrawing = false;
}
private void addTextInput(MouseEventArgs e)
{
Debug.WriteLine("addTextInput");
ExtendedTextPanel inputPanel = new ExtendedTextPanel();
currentInputPanel = inputPanel;
currentInputPanel.Opacity = 0;
currentInputPanel.Mode = "text";
pictureBoxShow.Controls.Add(inputPanel);
currentInputPanel.Size = new System.Drawing.Size(0, 0);
currentInputPanel.Location = new System.Drawing.Point(e.Location.X, e.Location.Y);
}
private void resizeTextInput(MouseEventArgs e)
{
if (currentInputPanel != null)
{
int dtW = e.Location.X - mouseDownPoint.X;
int dtH = e.Location.Y - mouseDownPoint.Y;
currentInputPanel.Width += dtW;
currentInputPanel.Height += dtH;
}
}
private void importTextInput()
{
if (currentInputPanel == null)
{
return;
}
// 太小的不创建
if (currentInputPanel.Width < 10 || currentInputPanel.Height < 10)
{
pictureBoxShow.Controls.Remove(currentInputPanel);
currentInputPanel = null;
return;
}
Debug.WriteLine("importTextInput");
currentInputPanel.showTransTextBox();
//currentInputPanel.LostFocus += new EventHandler(txt_LostFocus); //失去焦点后发生事件
//textBox.GotFocus += new EventHandler(txt_GotFocus); //获取焦点前发生事件
//textBox.MouseClick += new MouseEventHandler(txt_MouseClick); // 鼠标点击事件
}
private void writeToImage()
{
if (currentInputPanel == null) return;
if (currentInputPanel.TransTextBox.Text.Length == 0) return;
Graphics g = Graphics.FromImage(pictureBoxShow.Image);//定义一个GDI+
SolidBrush drawBrush = new SolidBrush(currentInputPanel.TransTextBox.ForeColor);//定义一个画笔,在这里我读取了textBox1的字体颜色
Font drawFont = new Font(currentInputPanel.TransTextBox.Font.FontFamily, currentInputPanel.TransTextBox.Font.Size);//定义一个font,设置字的样式,大小,都是读取textBox1的值<br>         //往图片上写字。这里需要注意的是Point,因为在图片加载的时候你可能采取了一定的压缩或者放大,所以要乘以压缩比例scaling
g.DrawString(currentInputPanel.TransTextBox.Text, drawFont, drawBrush, new System.Drawing.Point(currentInputPanel.Location.X, currentInputPanel.Location.Y));
pictureBoxShow.Controls.Remove(currentInputPanel);//然后移除添加的那个textBox1
}
private void RemoveTextHandle()
{
this.pictureBoxShow.MouseDown -= this.pictureBox_MouseDown;
this.pictureBoxShow.MouseMove -= this.pictureBox_MouseMove;
this.pictureBoxShow.MouseUp -= this.pictureBox_MouseUp;
}
private void AddTextHandle()
{
this.pictureBoxShow.MouseDown += new System.Windows.Forms.MouseEventHandler(this.pictureBox_MouseDown);
this.pictureBoxShow.MouseMove += new System.Windows.Forms.MouseEventHandler(this.pictureBox_MouseMove);
this.pictureBoxShow.MouseUp += new System.Windows.Forms.MouseEventHandler(this.pictureBox_MouseUp);
}
private void RemoveCutHandle()
{
this.pictBoxSour.MouseDown -= this.pictBoxSour_MouseDown;
this.pictBoxSour.MouseEnter -= this.pictBoxSour_MouseEnter;
this.pictBoxSour.MouseLeave -= this.pictBoxSour_MouseLeave;
this.pictBoxSour.MouseMove -= this.pictBoxSour_MouseMove;
this.pictBoxSour.MouseUp -= this.pictBoxSour_MouseUp;
}
private void AddCutHandle()
{
this.pictBoxSour.MouseDown += new System.Windows.Forms.MouseEventHandler(this.pictBoxSour_MouseDown);
this.pictBoxSour.MouseEnter += new System.EventHandler(this.pictBoxSour_MouseEnter);
this.pictBoxSour.MouseLeave += new System.EventHandler(this.pictBoxSour_MouseLeave);
this.pictBoxSour.MouseMove += new System.Windows.Forms.MouseEventHandler(this.pictBoxSour_MouseMove);
this.pictBoxSour.MouseUp += new System.Windows.Forms.MouseEventHandler(this.pictBoxSour_MouseUp);
}
private void btnCancel_Click(object sender, EventArgs e)
{
if (pictBoxSour.Image != null)
{
pictBoxSour.Image.Dispose();
}
if (this.Parent is Form)
{
if (((Form)this.Parent).Name.Equals("FrmImage"))
{
((Form)this.Parent).DialogResult = DialogResult.Cancel;
}
}
if (this.Parent is SplitterPanel)
{
if (this.Parent.Parent.Parent is Form)
{
if (((Form)this.Parent.Parent.Parent).Name.Equals("FrmImageList"))
{
((Form)this.Parent.Parent.Parent).DialogResult = DialogResult.Cancel;
}
}
}
}
private void btnList_Click(object sender, EventArgs e)
{
if (this.Parent is SplitterPanel)
{
if (this.Parent.Parent is SplitContainer)
{
((SplitContainer)this.Parent.Parent).Panel1Collapsed = !((SplitContainer)this.Parent.Parent).Panel1Collapsed;
if (((SplitContainer)this.Parent.Parent).Panel1Collapsed)
{
ImageCut IC = new ImageCut(0, 0, widthbox, heightbox);
this.pictBoxSour.Image = IC.KiCut((Bitmap)(this.GetSelectImage(widthbox, heightbox)));
}
}
}
}
private async void btnSave_Click(object sender, EventArgs e)
{
try {
if (clipMode)
{
this.btnCut.BackColor = Color.Transparent;
this.btnCut.ForeColor = Color.Black;
RemoveCutHandle();
clipMode = false;
}
if (textMode)
{
writeToImage();
textMode = false;
this.btnText.BackColor = Color.Transparent;
this.btnText.ForeColor = Color.Black;
RemoveTextHandle();
textMode = false;
}
if (this.Parent is Form)
{
if (pictureBoxShow.Image == null)
{ ImageData.Image = (Bitmap)pictBoxSour.Image.Clone(); }
else { ImageData.Image = (Bitmap)pictureBoxShow.Image.Clone(); }
if (pictureBoxShow.Image != null)
{
pictureBoxShow.Image.Dispose();
}
if (((Form)this.Parent).Name.Equals("FrmImage"))
{
((Form)this.Parent).DialogResult = DialogResult.OK;
}
}
if (this.Parent is SplitterPanel)
{
if (this.Parent.Parent.Parent is Form)
{
if (((Form)this.Parent.Parent.Parent).Name.Equals("FrmImageList"))
{
this.btnSave.Enabled = false;
if (File.Exists(selectImage.Image.localPathName))
{
File.Delete(selectImage.Image.localPathName);
}
Bitmap oldImg = null;
if (pictureBoxShow.Image == null)
{ oldImg = (Bitmap)pictBoxSour.Image.Clone(); }
else { oldImg = (Bitmap)pictureBoxShow.Image.Clone(); }
Bitmap img = new Bitmap(oldImg);
Graphics draw = Graphics.FromImage(img);
draw.DrawImage(oldImg, 0, 0, oldImg.Width, oldImg.Height);
oldImg.Dispose();
int iPos = selectImage.Image.localPathName.LastIndexOf("\\");
string localFilename = selectImage.Image.localPathName.Substring(iPos + 1);
using (MemoryStream stream = new MemoryStream())
{
img.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);
await apiService.UploadImage(stream, selectImage);
}
img.Save(selectImage.Image.localPathName, System.Drawing.Imaging.ImageFormat.Jpeg);
img.Dispose();
this.btnSave.Enabled = true;
}
}
}
}
catch (Exception exp)
{
MessageBox.Show(exp.ToString());
return;
}
}
private void btnCut_Click(object sender, EventArgs e)
{
clipMode = !clipMode;
if (textMode)
{
writeToImage();
textMode = false;
this.btnText.BackColor = Color.Transparent;
this.btnText.ForeColor = Color.Black;
RemoveTextHandle();
}
if (clipMode)
{
this.btnCut.BackColor = Color.Blue;
this.btnCut.ForeColor = Color.White;
AddCutHandle();
}
else
{
this.btnCut.BackColor = Color.Transparent;
this.btnCut.ForeColor = Color.Black;
RemoveCutHandle();
}
}
private void btnText_Click(object sender, EventArgs e)
{
textMode = !textMode;
if (clipMode)
{
this.btnCut.BackColor = Color.Transparent;
this.btnCut.ForeColor = Color.Black;
RemoveCutHandle();
}
if (textMode)
{
this.btnText.BackColor = Color.Blue;
this.btnText.ForeColor = Color.White;
AddTextHandle();
}
else
{
this.btnText.BackColor = Color.Transparent;
this.btnText.ForeColor = Color.Black;
RemoveTextHandle();
writeToImage();
}
}
private void ImageModifyBefore()
{
var oldImg = (Bitmap)pictBoxSour.Image.Clone();
Bitmap img = new Bitmap(oldImg);
Graphics draw = Graphics.FromImage(img);
draw.DrawImage(oldImg, 0, 0, oldImg.Width, oldImg.Height);
oldImg.Dispose();
img.Save(filePath, System.Drawing.Imaging.ImageFormat.Jpeg);
img.Dispose();
}
private void ImageModifyAfter()
{
using (FileStream fileStream = File.Open(filePath, FileMode.Open, System.IO.FileAccess.Read, FileShare.ReadWrite))
{
// 创建一个字节数组来保存FileStream的内容
byte[] fileBytes = new byte[fileStream.Length];
fileStream.Read(fileBytes, 0, fileBytes.Length);
// 使用字节数组创建MemoryStream
using (MemoryStream memoryStream = new MemoryStream(fileBytes))
{
Bi.Dispose();
Bi = new Bitmap(memoryStream);
this.pictureBoxShow.Image = Bi;
}
}
}
private void btnToBig_Click(object sender, EventArgs e)
{
try
{
ImageModifyBefore();
Mat src = new Mat(filePath, ImreadModes.Color);
Mat temImage = new Mat();
Mat dstImage2 = new Mat();
temImage = src;
Cv2.Resize(temImage, dstImage2, new OpenCvSharp.Size(temImage.Cols * 2, temImage.Rows * 2), 0, 0, InterpolationFlags.Linear);
dstImage2.SaveImage(filePath);
ImageModifyAfter();
}
catch (Exception exp)
{
MessageBox.Show(exp.ToString());
return;
}
}
private void btnToSmall_Click(object sender, EventArgs e)
{
try
{
ImageModifyBefore();
Mat src = new Mat(filePath, ImreadModes.Color);
Mat temImage = new Mat();
Mat dstImage1 = new Mat();
temImage = src;
Cv2.Resize(temImage, dstImage1, new OpenCvSharp.Size(temImage.Cols / 2, temImage.Rows / 2), 0, 0, InterpolationFlags.Linear);
dstImage1.SaveImage(filePath);
ImageModifyAfter();
}
catch (Exception exp)
{
MessageBox.Show(exp.ToString());
return;
}
}
private void btnmirror_Click(object sender, EventArgs e)
{
try
{
ImageModifyBefore();
Mat image = new Mat(filePath, ImreadModes.Color);
Mat result = new Mat();
Cv2.Flip(image, result, FlipMode.Y);
result.SaveImage(filePath);
ImageModifyAfter();
}
catch (Exception exp)
{
MessageBox.Show(exp.ToString());
return;
}
}
private void btnvertical_Click(object sender, EventArgs e)
{
try
{
ImageModifyBefore();
Mat image = new Mat(filePath, ImreadModes.Color);
Mat result = new Mat();
Cv2.Flip(image, result, FlipMode.X);
result.SaveImage(filePath);
ImageModifyAfter();
}
catch (Exception exp)
{
MessageBox.Show(exp.ToString());
return;
}
}
private void pDialog_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)//判断鼠标左键是否被按下
{
mouseOff = new System.Drawing.Point(-e.X, -e.Y);//得到变量的值
leftFlag = true;//标记鼠标左键的状态
}
}
private void pDialog_MouseUp(object sender, MouseEventArgs e)
{
if (leftFlag)
{
leftFlag = false;//释放鼠标后标注为false;
}
}
private void pDialog_MouseMove(object sender, MouseEventArgs e)
{
if (leftFlag)//判断鼠标左键有没有被按下
{
System.Drawing.Point mouseSet = Control.MousePosition;//获取屏幕中鼠标所在的位置
mouseSet.Offset(mouseOff.X, mouseOff.Y);//设置移动后的位置
pDialog.Location = mouseSet;
}
}
private void picClose_Click(object sender, EventArgs e)
{
pDialog.Visible = false;
}
private void trackBar1_Scroll(object sender, EventArgs e)
{
try
{
if (trackBarType.Equals("Emergence"))
{
ImageModifyBefore();
float mSize = float.Parse("0." + trackBar1.Value.ToString());
Mat src = new Mat(filePath, ImreadModes.Color);
int width = src.Cols;
int heigh = src.Rows;
int centerX = width >> 1;
int centerY = heigh >> 1;
int maxV = centerX * centerX + centerY * centerY;
int minV = (int)(maxV * (1 - mSize));
int diff = maxV - minV;
float ratio = width > heigh ? (float)heigh / (float)width : (float)width / (float)heigh;
Mat img = new Mat();
src.CopyTo(img);
Scalar avg = Cv2.Mean(src);
Mat dst = new Mat(img.Size(), MatType.CV_8UC3);
unsafe
{
for (int y = 0; y < heigh; y++)
{
IntPtr a = src.Ptr(y);
byte* p0 = (byte*)a.ToPointer();
IntPtr b = dst.Ptr(y);
byte* p1 = (byte*)b.ToPointer();
for (int x = 0; x < width; x++)
{
int b1 = p0[3 * x];
int g = p0[3 * x + 1];
int r = p0[3 * x + 2];
float dx = centerX - x;
float dy = centerY - y;
if (width > heigh)
dx = (dx * ratio);
else
dy = (dy * ratio);
float dstSq = dx * dx + dy * dy;
float v = ((float)dstSq / diff) * 255;
r = (int)(r + v);
g = (int)(g + v);
b1 = (int)(b1 + v);
r = (r > 255 ? 255 : (r < 0 ? 0 : r));
g = (g > 255 ? 255 : (g < 0 ? 0 : g));
b1 = (b1 > 255 ? 255 : (b1 < 0 ? 0 : b1));
p1[3 * x] = (byte)b1;
p1[3 * x + 1] = (byte)g;
p1[3 * x + 2] = (byte)r;
}
}
}
dst.SaveImage(filePath);
ImageModifyAfter();
}
else if (trackBarType.Equals("SobelThresholding"))
{
ImageModifyBefore();
Mat image = new Mat(filePath, ImreadModes.Grayscale);
Mat sobelX = new Mat();
Cv2.Sobel(image, sobelX, MatType.CV_8U, 1, 0, 3, 0.4, 128);
Mat sobelY = new Mat();
Cv2.Sobel(image, sobelY, MatType.CV_8U, 0, 1, 3, 0.4, 128);
Cv2.Sobel(image, sobelX, MatType.CV_16S, 1, 0);
Cv2.Sobel(image, sobelY, MatType.CV_16S, 0, 1);
Mat sobel;
// 计算L1模
sobel = sobelX.Abs() + sobelY.Abs();
double sobmin, sobmax;
OpenCvSharp.Point minLoc, maxLoc;
Cv2.MinMaxLoc(sobel, out sobmin, out sobmax, out minLoc, out maxLoc);
Mat sobelL1Image = new Mat();
sobel.ConvertTo(sobelL1Image, MatType.CV_8U, -255.0 / sobmax, 255);
Mat sobelThresholded = new Mat();
double threshold = double.Parse(trackBar1.Value.ToString());
Cv2.Threshold(sobelL1Image, sobelThresholded, threshold, 255, ThresholdTypes.Binary);
sobelThresholded.SaveImage(filePath);
ImageModifyAfter();
}
else if (trackBarType.Equals("FixThresholding"))
{
ImageModifyBefore();
Mat src = new Mat(filePath, ImreadModes.Grayscale);
Mat OutImage1 = new Mat();
Cv2.Threshold(src, OutImage1, trackBar1.Value, 255, ThresholdTypes.Binary);//固定阈值分割
OutImage1.SaveImage(filePath);
ImageModifyAfter();
}
else if (trackBarType.Equals("Hue"))
{
ImageModifyBefore();
Mat image = new Mat(filePath, ImreadModes.Color);
Mat hsv = new Mat();
Cv2.CvtColor(image, hsv, ColorConversionCodes.BGR2HSV);
Mat[] channels = Cv2.Split(hsv);
for (int i = 0; i < channels[0].Rows; i++)//遍历所有像素
{
for (int j = 0; j < channels[0].Cols; j++)
{
channels[0].Set(i, j, trackBar1.Value);//改变亮度
}
}
Cv2.Merge(channels, hsv);//合并通道
Mat newImage = new Mat();
Cv2.CvtColor(hsv, newImage, ColorConversionCodes.HSV2BGR);
newImage.SaveImage(filePath);
ImageModifyAfter();
}
else if (trackBarType.Equals("Brightness"))
{
ImageModifyBefore();
Mat image = new Mat(filePath, ImreadModes.Color);
Mat hsv = new Mat();
Cv2.CvtColor(image, hsv, ColorConversionCodes.BGR2HSV);
Mat[] channels = Cv2.Split(hsv);
for (int i = 0; i < channels[2].Rows; i++)//遍历所有像素
{
for (int j = 0; j < channels[2].Cols; j++)
{
channels[2].Set(i, j, trackBar1.Value);//改变亮度
}
}
Cv2.Merge(channels, hsv);//合并通道
Mat newImage = new Mat();
Cv2.CvtColor(hsv, newImage, ColorConversionCodes.HSV2BGR);
newImage.SaveImage(filePath);
ImageModifyAfter();
}
else if (trackBarType.Equals("Saturation"))
{
ImageModifyBefore();
Mat image = new Mat(filePath, ImreadModes.Color);
Mat hsv = new Mat();
Cv2.CvtColor(image, hsv, ColorConversionCodes.BGR2HSV);
Mat[] channels = Cv2.Split(hsv);
for (int i = 0; i < channels[1].Rows; i++)//遍历所有像素
{
for (int j = 0; j < channels[1].Cols; j++)
{
channels[1].Set(i, j, trackBar1.Value);//改变亮度
}
}
Cv2.Merge(channels, hsv);//合并通道
Mat newImage = new Mat();
Cv2.CvtColor(hsv, newImage, ColorConversionCodes.HSV2BGR);
newImage.SaveImage(filePath);
ImageModifyAfter();
}
}
catch (Exception exp)
{
MessageBox.Show(exp.ToString());
return;
}
}
private void tsmiHue_Click(object sender, EventArgs e)
{
if (pDialog.Visible)
{
MessageBox.Show("滑块窗口已经打开,请先关闭滑块窗口");
return;
}
pDialog.Visible = true;
pDialog.BringToFront();
trackBar1.Maximum = 255;
trackBarType = "";
trackBar1.Value = 0;
trackBarType = "Hue";
}
private void tsmiBrightness_Click(object sender, EventArgs e)
{
if (pDialog.Visible)
{
MessageBox.Show("滑块窗口已经打开,请先关闭滑块窗口");
return;
}
pDialog.Visible = true;
pDialog.BringToFront();
trackBar1.Maximum = 255;
trackBarType = "";
trackBar1.Value = 0;
trackBarType = "Brightness";
}
private void tsmiSaturation_Click(object sender, EventArgs e)
{
if (pDialog.Visible)
{
MessageBox.Show("滑块窗口已经打开,请先关闭滑块窗口");
return;
}
pDialog.Visible = true;
pDialog.BringToFront();
trackBar1.Maximum = 255;
trackBarType = "";
trackBar1.Value = 0;
trackBarType = "Saturation";
}
private void tsmiBoxFilter_Click(object sender, EventArgs e)
{
try
{
ImageModifyBefore();
Mat image = new Mat(filePath, ImreadModes.Color);
Mat out1 = new Mat();
Cv2.BoxFilter(image, out1, -1, new OpenCvSharp.Size(5, 5));
out1.SaveImage(filePath);
ImageModifyAfter();
}
catch (Exception exp)
{
MessageBox.Show(exp.ToString());
return;
}
}
private void tsmiMeanFilter_Click(object sender, EventArgs e)
{
try
{
ImageModifyBefore();
Mat image = new Mat(filePath, ImreadModes.Color);
Mat dstImage = new Mat();
Cv2.Blur(image, dstImage, new OpenCvSharp.Size(5, 5));
dstImage.SaveImage(filePath);
ImageModifyAfter();
}
catch (Exception exp)
{
MessageBox.Show(exp.ToString());
return;
}
}
private void tsmiGaussianFilter_Click(object sender, EventArgs e)
{
try
{
ImageModifyBefore();
Mat image = new Mat(filePath, ImreadModes.Color);
Mat dstImage = new Mat();
Cv2.GaussianBlur(image, dstImage, new OpenCvSharp.Size(5, 5), 0, 0);
dstImage.SaveImage(filePath);
ImageModifyAfter();
}
catch (Exception exp)
{
MessageBox.Show(exp.ToString());
return;
}
}
private void tsmiMedianFilter_Click(object sender, EventArgs e)
{
try
{
ImageModifyBefore();
Mat image = new Mat(filePath, ImreadModes.Color);
Mat dstImage = new Mat();
Cv2.MedianBlur(image, dstImage, 5);
dstImage.SaveImage(filePath);
ImageModifyAfter();
}
catch (Exception exp)
{
MessageBox.Show(exp.ToString());
return;
}
}
private void tsmiBilateralFilter_Click(object sender, EventArgs e)
{
try
{
ImageModifyBefore();
Mat image = new Mat(filePath, ImreadModes.Color);
Mat dstImage = new Mat();
Cv2.BilateralFilter(image, dstImage, 25, 25 * 2, 25 / 2);
dstImage.SaveImage(filePath);
ImageModifyAfter();
}
catch (Exception exp)
{
MessageBox.Show(exp.ToString());
return;
}
}
private void tsmiExpansion_Click(object sender, EventArgs e)
{
try
{
ImageModifyBefore();
Mat image = new Mat(filePath, ImreadModes.Color);
Mat dstImage = new Mat();
Cv2.Erode(image, dstImage, new Mat());
dstImage.SaveImage(filePath);
ImageModifyAfter();
}
catch (Exception exp)
{
MessageBox.Show(exp.ToString());
return;
}
}
private void tsmiCorrosion_Click(object sender, EventArgs e)
{
try
{
ImageModifyBefore();
Mat image = new Mat(filePath, ImreadModes.Color);
Mat dstImage = new Mat();
Cv2.Dilate(image, dstImage, new Mat());
dstImage.SaveImage(filePath);
ImageModifyAfter();
}
catch (Exception exp)
{
MessageBox.Show(exp.ToString());
return;
}
}
private void tsmiOpenOperation_Click(object sender, EventArgs e)
{
try
{
ImageModifyBefore();
Mat image = new Mat(filePath, ImreadModes.Color);
Mat element5 = new Mat(5, 5, MatType.CV_8U, new Scalar(1));
Mat dstImage = new Mat();
Cv2.MorphologyEx(image, dstImage, MorphTypes.Open, element5);
dstImage.SaveImage(filePath);
ImageModifyAfter();
}
catch (Exception exp)
{
MessageBox.Show(exp.ToString());
return;
}
}
private void tsmiCloseOperation_Click(object sender, EventArgs e)
{
try
{
ImageModifyBefore();
Mat image = new Mat(filePath, ImreadModes.Color);
Mat element5 = new Mat(5, 5, MatType.CV_8U, new Scalar(1));
Mat dstImage = new Mat();
Cv2.MorphologyEx(image, dstImage, MorphTypes.Close, element5);
dstImage.SaveImage(filePath);
ImageModifyAfter();
}
catch (Exception exp)
{
MessageBox.Show(exp.ToString());
return;
}
}
private void tsmiMorphologicalGradient_Click(object sender, EventArgs e)
{
try
{
ImageModifyBefore();
Mat image = new Mat(filePath, ImreadModes.Grayscale);
Mat element5 = new Mat(5, 5, MatType.CV_8U, new Scalar(1));
Mat dstImage = new Mat();
Cv2.MorphologyEx(image, dstImage, MorphTypes.Gradient, element5);
dstImage.SaveImage(filePath);
ImageModifyAfter();
}
catch (Exception exp)
{
MessageBox.Show(exp.ToString());
return;
}
}
private void tsmiTopHat_Click(object sender, EventArgs e)
{
try
{
ImageModifyBefore();
Mat image = new Mat(filePath, ImreadModes.Grayscale);
Mat element5 = new Mat(5, 5, MatType.CV_8U, new Scalar(1));
Mat dstImage = new Mat();
Cv2.MorphologyEx(image, dstImage, MorphTypes.TopHat, element5);
dstImage.SaveImage(filePath);
ImageModifyAfter();
}
catch (Exception exp)
{
MessageBox.Show(exp.ToString());
return;
}
}
private void tsmiBlackHat_Click(object sender, EventArgs e)
{
try
{
ImageModifyBefore();
Mat image = new Mat(filePath, ImreadModes.Grayscale);
Mat element5 = new Mat(5, 5, MatType.CV_8U, new Scalar(1));
Mat dstImage = new Mat();
Cv2.MorphologyEx(image, dstImage, MorphTypes.BlackHat, element5);
dstImage.SaveImage(filePath);
ImageModifyAfter();
}
catch (Exception exp)
{
MessageBox.Show(exp.ToString());
return;
}
}
private void tsmiNostalgia_Click(object sender, EventArgs e)
{
try
{
ImageModifyBefore();
Mat src = new Mat(filePath, ImreadModes.Color);
Mat dstImage = new Mat(src.Size(), MatType.CV_8UC3);
int width = src.Cols;
int heigh = src.Rows;
unsafe
{
for (int y = 0; y < heigh; y++)
{
IntPtr a = src.Ptr(y);
byte* p0 = (byte*)a.ToPointer();
IntPtr b = dstImage.Ptr(y);
byte* p1 = (byte*)b.ToPointer();
for (int x = 0; x < width; x++)
{
float B = p0[3 * x];
float G = p0[3 * x + 1];
float R = p0[3 * x + 2];
double newB = 0.272 * R + 0.534 * G + 0.131 * B;
double newG = 0.349 * R + 0.686 * G + 0.168 * B;
double newR = 0.393 * R + 0.769 * G + 0.189 * B;
if (newB < 0) newB = 0;
if (newB > 255) newB = 255;
if (newG < 0) newG = 0;
if (newG > 255) newG = 255;
if (newR < 0) newR = 0;
if (newR > 255) newR = 255;
p1[3 * x] = (byte)newB;
p1[3 * x + 1] = (byte)newG;
p1[3 * x + 2] = (byte)newR;
}
}
}
dstImage.SaveImage(filePath);
ImageModifyAfter();
}
catch (Exception exp)
{
MessageBox.Show(exp.ToString());
return;
}
}
private void tsmiFrost_Click(object sender, EventArgs e)
{
try
{
ImageModifyBefore();
Mat src = new Mat(filePath, ImreadModes.Color);
Mat dstImage = new Mat(src.Size(), MatType.CV_8UC3);
int width = src.Cols;
int heigh = src.Rows;
unsafe
{
for (int y = 0; y < heigh; y++)
{
IntPtr a = src.Ptr(y);
byte* p0 = (byte*)a.ToPointer();
IntPtr b = dstImage.Ptr(y);
byte* p1 = (byte*)b.ToPointer();
for (int x = 0; x < width; x++)
{
float b0 = p0[3 * x];
float g0 = p0[3 * x + 1];
float r0 = p0[3 * x + 2];
float b1 = (b0 - g0 - r0) * 3 / 2;
float g = (g0 - b0 - r0) * 3 / 2;
float r = (r0 - g0 - b0) * 3 / 2;
r = (r > 255 ? 255 : (r < 0 ? -r : r));
g = (g > 255 ? 255 : (g < 0 ? -g : g));
b1 = (b1 > 255 ? 255 : (b1 < 0 ? -b1 : b1));
p1[3 * x] = (byte)b1;
p1[3 * x + 1] = (byte)g;
p1[3 * x + 2] = (byte)r;
}
}
}
dstImage.SaveImage(filePath);
ImageModifyAfter();
}
catch (Exception exp)
{
MessageBox.Show(exp.ToString());
return;
}
}
private void tsmiCasting_Click(object sender, EventArgs e)
{
try
{
ImageModifyBefore();
Mat src = new Mat(filePath, ImreadModes.Color);
Mat dstImage = new Mat(src.Size(), MatType.CV_8UC3);
int width = src.Cols;
int heigh = src.Rows;
unsafe
{
for (int y = 0; y < heigh; y++)
{
IntPtr a = src.Ptr(y);
byte* p0 = (byte*)a.ToPointer();
IntPtr b = dstImage.Ptr(y);
byte* p1 = (byte*)b.ToPointer();
for (int x = 0; x < width; x++)
{
float b0 = p0[3 * x];
float g0 = p0[3 * x + 1];
float r0 = p0[3 * x + 2];
float b1 = b0 * 255 / (g0 + r0 + 1);
float g = g0 * 255 / (b0 + r0 + 1);
float r = r0 * 255 / (g0 + b0 + 1);
r = (r > 255 ? 255 : (r < 0 ? 0 : r));
g = (g > 255 ? 255 : (g < 0 ? 0 : g));
b1 = (b1 > 255 ? 255 : (b1 < 0 ? 0 : b1));
p1[3 * x] = (byte)b1;
p1[3 * x + 1] = (byte)g;
p1[3 * x + 2] = (byte)r;
}
}
}
dstImage.SaveImage(filePath);
ImageModifyAfter();
}
catch (Exception exp)
{
MessageBox.Show(exp.ToString());
return;
}
}
private void tsmiGrayscale_Click(object sender, EventArgs e)
{
try
{
ImageModifyBefore();
Mat dstImage = new Mat(filePath, ImreadModes.Grayscale);
dstImage.SaveImage(filePath);
ImageModifyAfter();
}
catch (Exception exp)
{
MessageBox.Show(exp.ToString());
return;
}
}
private void tsmiEmergence_Click(object sender, EventArgs e)
{
try
{
if (pDialog.Visible)
{
MessageBox.Show("滑块窗口已经打开,请先关闭滑块窗口");
return;
}
pDialog.Visible = true;
pDialog.BringToFront();
trackBar1.Maximum = 9;
trackBarType = "";
trackBar1.Value = 0;
trackBarType = "Emergence";
}
catch (Exception exp)
{
MessageBox.Show(exp.ToString());
return;
}
}
private void tsmiSobelX_Click(object sender, EventArgs e)
{
try
{
ImageModifyBefore();
Mat image = new Mat(filePath, ImreadModes.Grayscale);
Mat dstImage = new Mat();
Cv2.Sobel(image, dstImage, MatType.CV_8U, 1, 0, 3, 0.4, 128);
dstImage.SaveImage(filePath);
ImageModifyAfter();
}
catch (Exception exp)
{
MessageBox.Show(exp.ToString());
return;
}
}
private void tsmiSobelY_Click(object sender, EventArgs e)
{
try
{
ImageModifyBefore();
Mat image = new Mat(filePath, ImreadModes.Grayscale);
Mat dstImage = new Mat();
Cv2.Sobel(image, dstImage, MatType.CV_8U, 0, 1, 3, 0.4, 128);
dstImage.SaveImage(filePath);
ImageModifyAfter();
}
catch (Exception exp)
{
MessageBox.Show(exp.ToString());
return;
}
}
private void tsmiSobelMode_Click(object sender, EventArgs e)
{
try
{
ImageModifyBefore();
Mat image = new Mat(filePath, ImreadModes.Grayscale);
Mat sobelX = new Mat();
Cv2.Sobel(image, sobelX, MatType.CV_8U, 1, 0, 3, 0.4, 128);
Mat sobelY = new Mat();
Cv2.Sobel(image, sobelY, MatType.CV_8U, 0, 1, 3, 0.4, 128);
Cv2.Sobel(image, sobelX, MatType.CV_16S, 1, 0);
Cv2.Sobel(image, sobelY, MatType.CV_16S, 0, 1);
Mat sobel;
// 计算L1模
sobel = sobelX.Abs() + sobelY.Abs();
double sobmin, sobmax;
OpenCvSharp.Point minLoc, maxLoc;
Cv2.MinMaxLoc(sobel, out sobmin, out sobmax, out minLoc, out maxLoc);
Mat dstImage = new Mat();
sobel.ConvertTo(dstImage, MatType.CV_8U, -255.0 / sobmax, 255);
dstImage.SaveImage(filePath);
ImageModifyAfter();
}
catch (Exception exp)
{
MessageBox.Show(exp.ToString());
return;
}
}
private void tsmiSobelThresholding_Click(object sender, EventArgs e)
{
try
{
if (pDialog.Visible)
{
MessageBox.Show("滑块窗口已经打开,请先关闭滑块窗口");
return;
}
pDialog.Visible = true;
pDialog.BringToFront();
trackBar1.Maximum = 255;
trackBarType = "";
trackBar1.Value = 0;
trackBarType = "SobelThresholding";
}
catch (Exception exp)
{
MessageBox.Show(exp.ToString());
return;
}
}
private void tsmiLaplace_Click(object sender, EventArgs e)
{
try
{
ImageModifyBefore();
Mat image = new Mat(filePath, ImreadModes.Grayscale);
Mat dst = new Mat();
Mat dstImage = new Mat();
Cv2.Laplacian(image, dst, MatType.CV_16S, 3, 1, 0, BorderTypes.Default);
Cv2.ConvertScaleAbs(dst, dstImage);
dstImage.SaveImage(filePath);
ImageModifyAfter();
}
catch (Exception exp)
{
MessageBox.Show(exp.ToString());
return;
}
}
private void tsmiCanny_Click(object sender, EventArgs e)
{
try
{
ImageModifyBefore();
//Mat dstImage = new Mat(filePath, ImreadModes.Grayscale);
//dstImage.SaveImage(filePath);
ImageModifyAfter();
}
catch (Exception exp)
{
MessageBox.Show(exp.ToString());
return;
}
}
private void tsmiScharrX_Click(object sender, EventArgs e)
{
try
{
ImageModifyBefore();
Mat image = new Mat(filePath, ImreadModes.Grayscale);
Mat grad_x = new Mat();
Mat dstImage = new Mat();
Cv2.Scharr(image, grad_x, MatType.CV_16S, 1, 0, 1, 0, BorderTypes.Default);
Cv2.ConvertScaleAbs(grad_x, dstImage);
dstImage.SaveImage(filePath);
ImageModifyAfter();
}
catch (Exception exp)
{
MessageBox.Show(exp.ToString());
return;
}
}
private void tsmiScharrY_Click(object sender, EventArgs e)
{
try
{
ImageModifyBefore();
Mat image = new Mat(filePath, ImreadModes.Grayscale);
Mat grad_y = new Mat();
Mat dstImage = new Mat();
Mat dst = new Mat();
Cv2.Scharr(image, grad_y, MatType.CV_16S, 0, 1, 1, 0, BorderTypes.Default);
Cv2.ConvertScaleAbs(grad_y, dstImage);
dstImage.SaveImage(filePath);
ImageModifyAfter();
}
catch (Exception exp)
{
MessageBox.Show(exp.ToString());
return;
}
}
private void tsmiMergeGradients_Click(object sender, EventArgs e)
{
try
{
ImageModifyBefore();
Mat image = new Mat(filePath, ImreadModes.Grayscale);
Mat grad_x = new Mat();
Mat abs_grad_x = new Mat();
Cv2.Scharr(image, grad_x, MatType.CV_16S, 1, 0, 1, 0, BorderTypes.Default);
Cv2.ConvertScaleAbs(grad_x, abs_grad_x);
Mat grad_y = new Mat();
Mat abs_grad_y = new Mat();
Mat dstImage = new Mat();
Cv2.Scharr(image, grad_y, MatType.CV_16S, 0, 1, 1, 0, BorderTypes.Default);
Cv2.ConvertScaleAbs(grad_y, abs_grad_y);
Cv2.AddWeighted(abs_grad_x, 0.5, abs_grad_y, 0.5, 0, dstImage);
dstImage.SaveImage(filePath);
ImageModifyAfter();
}
catch (Exception exp)
{
MessageBox.Show(exp.ToString());
return;
}
}
private void tsmiFixThresholding_Click(object sender, EventArgs e)
{
if (pDialog.Visible)
{
MessageBox.Show("滑块窗口已经打开,请先关闭滑块窗口");
return;
}
pDialog.Visible = true;
pDialog.BringToFront();
trackBar1.Maximum = 255;
trackBarType = "";
trackBar1.Value = 0;
trackBarType = "FixThresholding";
}
private void tsmiSelfThresholding_Click(object sender, EventArgs e)
{
try
{
ImageModifyBefore();
Mat src = new Mat(filePath, ImreadModes.Grayscale);
Mat OutImage1 = new Mat();
Mat OutImage2 = new Mat();
Cv2.AdaptiveThreshold(src, OutImage2, 255, AdaptiveThresholdTypes.MeanC, ThresholdTypes.Binary, 31, 10);//自适应阈值分割
OutImage2.SaveImage(filePath);
ImageModifyAfter();
}
catch (Exception exp)
{
MessageBox.Show(exp.ToString());
return;
}
}
private void tsmiComic_Click(object sender, EventArgs e)
{
try
{
ImageModifyBefore();
Mat src1 = new Mat(filePath, ImreadModes.Color);
Mat img = new Mat();
Cv2.BilateralFilter(src1, img, 5, 150, 150);
Cv2.BilateralFilter(img, src1, 5, 150, 150);
Mat src = new Mat();
Cv2.CvtColor(src1, src, ColorConversionCodes.BGR2GRAY);
Mat imgL = new Mat();
Mat imgC = new Mat();
Cv2.Canny(src, imgC, 30, 90);
Mat imgS = new Mat();
Mat imgSx = new Mat();
Mat imgSy = new Mat();
Mat imgS0 = new Mat();
Cv2.Sobel(src, imgSx, -1, 0, 1);
Cv2.Sobel(src, imgSy, -1, 1, 0);
imgS = imgSx + imgSy;
Cv2.Sobel(src, imgS0, -1, 1, 1);
Mat imgTotal = new Mat();
imgTotal = imgC + imgS + imgL;
//imgTotal.convertTo(imgTotal,CV_32FC1);
Cv2.Normalize(imgTotal, imgTotal, 255, 0, NormTypes.MinMax);
Cv2.GaussianBlur(imgTotal, imgTotal, new OpenCvSharp.Size(3, 3), 3);
Cv2.Threshold(imgTotal, imgTotal, 100, 255, ThresholdTypes.BinaryInv);
Mat imgTotalC3 = new Mat();
Cv2.CvtColor(imgTotal, imgTotalC3, ColorConversionCodes.GRAY2BGR);
Cv2.BitwiseAnd(src1, imgTotalC3, src1);
src1.SaveImage(filePath);
ImageModifyAfter();
}
catch (Exception exp)
{
MessageBox.Show(exp.ToString());
return;
}
}
}
}