|
@@ -1,15 +1,105 @@
|
|
|
using OpenCvSharp;
|
|
|
using System;
|
|
|
+using System.Drawing;
|
|
|
+using System.Drawing.Drawing2D;
|
|
|
+using System.IO;
|
|
|
|
|
|
namespace OpenCVTest
|
|
|
{
|
|
|
class Program
|
|
|
{
|
|
|
+ /// <summary>
|
|
|
+ /// http://wiki.opencv.org.cn/index.php/Cxcore%E6%95%B0%E7%BB%84%E6%93%8D%E4%BD%9C#CreateImage 教程
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="args"></param>
|
|
|
static void Main(string[] args)
|
|
|
{
|
|
|
+ Color FgClr= ColorTranslator.FromHtml("#FFBF53");
|
|
|
+ Color BgClr=ColorTranslator.FromHtml("#0040AC");
|
|
|
+ Bitmap bm = new System.Drawing.Bitmap(8, 8);
|
|
|
+ bm.SetPixel(0, 0, FgClr);
|
|
|
+ bm.SetPixel(0, 1, BgClr);
|
|
|
+ bm.SetPixel(0, 2, BgClr);
|
|
|
+ bm.SetPixel(0, 3, BgClr);
|
|
|
+ bm.SetPixel(0, 4, FgClr);
|
|
|
+ bm.SetPixel(0, 5, BgClr);
|
|
|
+ bm.SetPixel(0, 6, BgClr);
|
|
|
+ bm.SetPixel(0, 7, BgClr);
|
|
|
+ bm.SetPixel(1, 0, BgClr);
|
|
|
+ bm.SetPixel(1, 1, BgClr);
|
|
|
+ bm.SetPixel(1, 2, BgClr);
|
|
|
+ bm.SetPixel(1, 3, BgClr);
|
|
|
+ bm.SetPixel(1, 4, BgClr);
|
|
|
+ bm.SetPixel(1, 5, BgClr);
|
|
|
+ bm.SetPixel(1, 6, BgClr);
|
|
|
+ bm.SetPixel(1, 7, BgClr);
|
|
|
+ bm.SetPixel(2, 0, BgClr);
|
|
|
+ bm.SetPixel(2, 1, BgClr);
|
|
|
+ bm.SetPixel(2, 2, FgClr);
|
|
|
+ bm.SetPixel(2, 3, BgClr);
|
|
|
+ bm.SetPixel(2, 4, BgClr);
|
|
|
+ bm.SetPixel(2, 5, BgClr);
|
|
|
+ bm.SetPixel(2, 6, FgClr);
|
|
|
+ bm.SetPixel(2, 7, BgClr);
|
|
|
+ bm.SetPixel(3, 0, BgClr);
|
|
|
+ bm.SetPixel(3, 1, BgClr);
|
|
|
+ bm.SetPixel(3, 2, BgClr);
|
|
|
+ bm.SetPixel(3, 3, BgClr);
|
|
|
+ bm.SetPixel(3, 4, BgClr);
|
|
|
+ bm.SetPixel(3, 5, BgClr);
|
|
|
+ bm.SetPixel(3, 6, BgClr);
|
|
|
+ bm.SetPixel(3, 7, BgClr);
|
|
|
+ bm.SetPixel(4, 0, FgClr);
|
|
|
+ bm.SetPixel(4, 1, BgClr);
|
|
|
+ bm.SetPixel(4, 2, BgClr);
|
|
|
+ bm.SetPixel(4, 3, BgClr);
|
|
|
+ bm.SetPixel(4, 4, FgClr);
|
|
|
+ bm.SetPixel(4, 5, BgClr);
|
|
|
+ bm.SetPixel(4, 6, BgClr);
|
|
|
+ bm.SetPixel(4, 7, BgClr);
|
|
|
+ bm.SetPixel(5, 0, BgClr);
|
|
|
+ bm.SetPixel(5, 1, BgClr);
|
|
|
+ bm.SetPixel(5, 2, BgClr);
|
|
|
+ bm.SetPixel(5, 3, BgClr);
|
|
|
+ bm.SetPixel(5, 4, BgClr);
|
|
|
+ bm.SetPixel(5, 5, BgClr);
|
|
|
+ bm.SetPixel(5, 6, BgClr);
|
|
|
+ bm.SetPixel(5, 7, BgClr);
|
|
|
+ bm.SetPixel(6, 0, BgClr);
|
|
|
+ bm.SetPixel(6, 1, BgClr);
|
|
|
+ bm.SetPixel(6, 2, FgClr);
|
|
|
+ bm.SetPixel(6, 3, BgClr);
|
|
|
+ bm.SetPixel(6, 4, BgClr);
|
|
|
+ bm.SetPixel(6, 5, BgClr);
|
|
|
+ bm.SetPixel(6, 6, FgClr);
|
|
|
+ bm.SetPixel(6, 7, BgClr);
|
|
|
+ bm.SetPixel(7, 0, BgClr);
|
|
|
+ bm.SetPixel(7, 1, BgClr);
|
|
|
+ bm.SetPixel(7, 2, BgClr);
|
|
|
+ bm.SetPixel(7, 3, BgClr);
|
|
|
+ bm.SetPixel(7, 4, BgClr);
|
|
|
+ bm.SetPixel(7, 5, BgClr);
|
|
|
+ bm.SetPixel(7, 6, BgClr);
|
|
|
+ bm.SetPixel(7, 7, BgClr);
|
|
|
+ Graphics graphics = Graphics.FromImage(bm);
|
|
|
+ //graphics.Clear(Color.Transparent);
|
|
|
+ //HatchBrush myHatchBrush = new HatchBrush(HatchStyle.Percent10, Color.Blue, Color.Transparent);
|
|
|
+ //graphics.FillRectangle(myHatchBrush, 0, 0,300, 300);
|
|
|
+ graphics.Save();
|
|
|
+ graphics.Dispose();
|
|
|
+ MemoryStream ms = new MemoryStream();
|
|
|
+ bm.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
|
|
|
+ byte[] arr = new byte[ms.Length];
|
|
|
+ ms.Position = 0;
|
|
|
+ ms.Read(arr, 0, (int)ms.Length);
|
|
|
+ ms.Close();
|
|
|
+ var a = "data:image/png;base64," + Convert.ToBase64String(arr);
|
|
|
+ //Graphics 类还有很多绘图方法可以绘制 直线、曲线、圆等等
|
|
|
+ //image.Save(sIconFileName, System.Drawing.Imaging.ImageFormat.Png);
|
|
|
+
|
|
|
//源码 https://gitee.com/CrazyIterBin/opencvsharp.git
|
|
|
- Mat base_img = Cv2.ImRead(@"F:/20191204104138.jpg");
|
|
|
- get_answer_from_sheet(base_img);
|
|
|
+ // Mat base_img = Cv2.ImRead(@"F:/20191204104138.jpg");
|
|
|
+ //get_answer_from_sheet(base_img);
|
|
|
}
|
|
|
public static void get_answer_from_sheet(Mat base_img)
|
|
|
{
|
|
@@ -25,8 +115,26 @@ namespace OpenCVTest
|
|
|
Mat img = new Mat();
|
|
|
Cv2.Add(h, v,img);
|
|
|
Cv2.ConvertScaleAbs(img,img);
|
|
|
- Cv2.GaussianBlur(img, img, new Size { Width = 3, Height = 3 }, 0);
|
|
|
- //Cv2.Threshold(img, img, 120, 255, cv2);
|
|
|
+ Cv2.GaussianBlur(img, img, new OpenCvSharp.Size { Width = 3, Height = 3 }, 0);
|
|
|
+ Cv2.Threshold(img, img, 120, 255, ThresholdTypes.Binary);
|
|
|
+ Cv2.Erode(img, img, InputArray.Create(new int[1, 1]),iterations:1);
|
|
|
+ Cv2.Dilate(img, img, InputArray.Create(new int[1, 1]), iterations: 2);
|
|
|
+ Cv2.Erode(img, img, InputArray.Create(new int[1, 1]), iterations: 1);
|
|
|
+ Cv2.Dilate(img, img, InputArray.Create(new int[1, 1]), iterations:2);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void auto_canny(Mat image, double sigma = 0.33) {
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void median(Mat image) {
|
|
|
+ float[] buf = new float[image.Rows* image.Cols];
|
|
|
+ for (int i = 0; i < image.Rows; i++) {
|
|
|
+ for (int j = 0; j < image.Cols; j++)
|
|
|
+ {
|
|
|
+ buf[i * image.Cols + j] = (float)image.Ptr(i, j);
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
}
|