最近在做员工制卡,需要拍照,但是照片有点暗,所以就想用Opencv的拉普拉斯变换增强图像,下面是使用c#下的OpencvSharp. 可以自行下载相关的DLL文件
using AForge.Video.DirectShow;
using System;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;
using IDCard.Service;
using System.Media;
using IDCard.Model;
using System.Runtime.InteropServices;
using System.Reflection;
using System.Drawing.Printing;
using System.Data;
using OpenCvSharp;
using OpenCvSharp.Dnn;
using OpenCvSharp.Extensions;
namespace IDCard
{
public partial class FrmMain : Form
{
private CameraHelper Capture = new CameraHelper();
private VideoCapture cap ;
private Mat src1 = new Mat();
private int currentPageIndex = 0;
private int rowCount = 0;
private int pageCount = 0;
public string param;
FilterInfoCollection videoDevices;
[DllImport("shell32.dll")]
public extern static int ShellAbout(IntPtr hWnd, string szApp, string szOtherStuff, IntPtr hIcon);
// DrawCanvas canvas;
public FrmMain()
{
InitializeComponent();
// Setting.GetPhotoFolder();
}
private void CameraConn()
{
int CameraIndex = cbbCameraList.SelectedIndex;
VideoCaptureDevice videoSource = new VideoCaptureDevice(videoDevices[CameraIndex].MonikerString);
videoSource.DesiredFrameRate = 1;
videoSource.DesiredFrameSize = new System.Drawing.Size(1280, 720);
//videoSource.DesiredFrameRate = 1;
try
{
for(int x = 0; x < videoSource.VideoCapabilities.Length;x++)
{
System.Drawing.Size DefaultSize = new System.Drawing.Size(1280, 720);
System.Drawing.Size size = videoSource.VideoCapabilities[x].FrameSize;
if (size == DefaultSize)
{
videoSource.VideoResolution = videoSource.VideoCapabilities[x];
break;
}
}
}catch(Exception ex)
{
videoSource.VideoResolution = videoSource.VideoCapabilities[8];
}
videPlayer1.VideoSource = videoSource;
videPlayer1.Start();
}
private void CameraClose()
{
videPlayer1.SignalToStop();
videPlayer1.WaitForStop();
if (videPlayer1.IsRunning)
{
videPlayer1.Stop();
}
}
private string GetImagePath()
{
string personImgPath = Path.GetDirectoryName(AppDomain.CurrentDomain.BaseDirectory)
+ Path.DirectorySeparatorChar.ToString() + "PersonImg";
if (!Directory.Exists(personImgPath))
{
Directory.CreateDirectory(personImgPath);
}
return personImgPath;
}
private void btnCameraConn_Click(object sender, EventArgs e)
{
if (videPlayer1.IsRunning)
{
btnCameraConn.Text = "连接";
videPlayer1.SignalToStop();
videPlayer1.WaitForStop();
cbbCameraList.Enabled = true;
}
else
{
CameraConn();
btnCameraConn.Text = "关闭";
cbbCameraList.Enabled = false;
}
}
private void button2_Click(object sender, EventArgs e)
{
Mat src1 = new Mat();
Mat src2 = new Mat();
Capture.Captuer.Read(src1);
float[,] myLengthsArray = new float[3,3] { { 0, -1, 0 }, { 0.1F, 3.33F, 0 }, { 0, -1, 0 } };
Mat imageEnhance = new Mat();// Array ;
Mat kernel = new Mat(3,3,MatType.CV_32F, myLengthsArray);// Scalar.;//= (Mat(3, 3) << 0, -1, 0, 0, 5, 0, 0, -1, 0);
Cv2.Filter2D(src1, imageEnhance, MatType.CV_8UC3, kernel);
Cv2.ImShow("src1", src1);
Cv2.ImShow("src3", imageEnhance);
src1.SaveImage(@"D:\MyProgram\windows\IDCard\IDCard\IDCard\Image\src1.png");
imageEnhance.SaveImage(@"D:\MyProgram\windows\IDCard\IDCard\IDCard\Image\src3.png");
}
private void pbCamera_Paint(object sender, PaintEventArgs e)
{
System.Drawing.Graphics g = e.Graphics;
System.Drawing.Pen pen = new System.Drawing.Pen(System.Drawing.Brushes.LightGreen, 4); // 创建一个红色,宽度为2的画笔
float x1 = 170f, y1 = 60f, x2 = 330f, y2 = 260f;
g.DrawLine(pen, x1, y1, x2, y1);
g.DrawLine(pen, x1, y1, x1, y2);
g.DrawLine(pen, x2, y1, x2, y2);
g.DrawLine(pen, x1, y2, x2, y2);
Font font = FrmMain.DefaultFont;// "微软雅黑";
Brush bursh = Brushes.Red;
g.DrawString("请将头像放置在框内", font, bursh, 190f, 10f);
try
{
RectangleF rectMan = new RectangleF(120, 65, 260, 260);
g.DrawImage(Image.FromFile(Application.StartupPath + @"\Image\man2.png"), rectMan);
RectangleF rectLeftEye = new RectangleF(210, 135, 20, 20);
g.DrawImage(Image.FromFile(Application.StartupPath + @"\Image\Eye_Invisible_128px.png"), rectLeftEye);
RectangleF rectRightEye = new RectangleF(270, 135, 20, 20);
g.DrawImage(Image.FromFile(Application.StartupPath + @"\Image\Eye_Invisible_128px.png"), rectRightEye);
}
catch (Exception ex)
{
Upgrade.DownloadImageFile();
Common.Common.LOG.WriteErrorLog(ex.Message);
}
}
}
}
using OpenCvSharp;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace IDCard.Service
{
public class CameraHelper
{
public VideoCapture Captuer;
public void OpenCamera(int CameraIndex)
{
Captuer = new VideoCapture(CameraIndex);
try
{
Captuer.Set(CaptureProperty.FrameWidth, 1280);
Captuer.Set(CaptureProperty.FrameHeight, 720);
}
catch
{
Captuer.Set(CaptureProperty.FrameWidth, 640);
Captuer.Set(CaptureProperty.FrameHeight, 480);
}
}
public void CloseCamera()
{
Captuer.Release();
}
}
}