ImageUtil.cs
1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
using OpenCvSharp;
using OpenCvSharp.Blob;
using OpenCvSharp.Extensions;
using OpenCvSharp.XFeatures2D;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
namespace AOI
{
public class ImageUtil
{
public static Mat ToMat(Image image)
{
return BitmapConverter.ToMat(new Bitmap(image));
}
public static Image ToImage(Mat mat)
{
return BitmapConverter.ToBitmap(mat);
}
public static Mat GetGrayHist(Mat srcMat)
{
Mat grayMat = new Mat();
Cv2.CvtColor(srcMat, grayMat, ColorConversionCodes.BGRA2GRAY);
Mat[] mats = new Mat[] { grayMat };//一张图片,初始化为panda
Mat hist = new Mat();//用来接收直方图
int[] channels = new int[] { 0 };//一个通道,初始化为通道0
int[] histsize = new int[] { 256 };//一个通道,初始化为256箱子
Rangef[] range = new Rangef[1];//一个通道,值范围
range[0].Start = 0.0F;//从0开始(含)
range[0].End = 256.0F;//到256结束(不含)
Mat mask = new Mat();//不做掩码
Cv2.CalcHist(mats, channels, mask, hist, 1, histsize, range);//计算灰度图,dim为1 1维
return hist;
}
}
}