タイトルの通りですが、
下記カレンダー画像の枠線をハフ変換による抽出をしたいです。
▼コード
c#
1 private void Form1_Load(object sender, EventArgs e) 2 { 3 cvTest(); 4 } 5 6 private void cvTest() 7 { 8 string fileName = INPUT_DIR + "calendar.png"; 9 Mat mat = new Mat(fileName); 10 // 切り出し 11 Mat dst = cutOut(mat); 12 } 13 14 private Mat cutOut(Mat src) 15 { 16 Mat dst = new Mat(); 17 18 // グレースケール 19 Cv2.CvtColor(src, dst, ColorConversionCodes.BGRA2GRAY); 20 Cv2.ImWrite(OUTPUT_DIR + "grayScale_" + getNowString() + ".png", dst); 21 22 // コントラストを上げる 23 changeContrust(dst, dst, 50f); // 0~100 24 Cv2.ImWrite(OUTPUT_DIR + "contrustUP_" + getNowString() + ".png", dst); 25 26 // 二値化 27 Cv2.Threshold(dst, dst, 30, 255, ThresholdTypes.Binary); 28 Cv2.ImWrite(OUTPUT_DIR + "binary_" + getNowString() + ".png", dst); 29 30 // エッジ検出 31 Cv2.Canny(dst, dst, 50, 200); // TODO パラメータ調査 32 Cv2.ImWrite(OUTPUT_DIR + "canny_" + getNowString() + ".png", dst); 33 34 // 標準ハフ変換 35 double paramRho = 10d; 36 double paramTheta = Math.PI / 180 * 10; 37 int threshold = 0; 38 Mat dstHoughLines = src.Clone(); 39 LineSegmentPolar[] lines = Cv2.HoughLines(dst, paramRho, paramTheta, threshold); 40 Debug.WriteLine("lines.Count:" + lines.Count().ToString()); 41 foreach (LineSegmentPolar lsp in lines) { 42 float rho = lsp.Rho; 43 float theta = lsp.Theta; 44 double a = Math.Cos(theta); 45 double b = Math.Sin(theta); 46 double x0 = a * rho; 47 double y0 = b * rho; 48 int x1 = (int)Math.Round(x0 + 1000 * (-b)); 49 int y1 = (int)Math.Round(y0 + 1000 * (a)); 50 int x2 = (int)Math.Round(x0 - 1000 * (-b)); 51 int y2 = (int)Math.Round(y0 - 1000 * (a)); 52 OpenCvSharp.Point pt1 = new OpenCvSharp.Point(x1, y1); 53 OpenCvSharp.Point pt2 = new OpenCvSharp.Point(x2, y2); 54 Cv2.Line(dstHoughLines, pt1, pt2, new Scalar(255, 0, 0), 1, LineTypes.AntiAlias); 55 } 56 Cv2.ImWrite(OUTPUT_DIR + "houghlines_" + getNowString() + ".png", dstHoughLines); 57 58 //using (new Window("dst image", dst)) 59 //{ 60 // Cv2.WaitKey(); 61 //} 62 return dst; 63 } 64 65 /// <summary> 66 /// コントラスト変更 67 /// </summary> 68 /// <param name="src"></param> 69 /// <param name="dst"></param> 70 /// <param name="contrust"></param> 71 void changeContrust(Mat src, Mat dst, float contrust) 72 { 73 byte[] lut = new byte[256]; 74 for (int i = 0; i < lut.Length; i++) 75 { 76 lut[i] = (byte)(255 / (1.0f + Math.Exp(-contrust * (i - 128) / 255))); 77 } 78 Cv2.LUT(src, lut, dst); 79 } 80 81 /// <summary> 82 /// 現在日時文字列を返す 83 /// </summary> 84 /// <returns></returns> 85 private string getNowString() 86 { 87 DateTime dt = System.DateTime.Now; 88 string ret = dt.ToString("yyyyMMdd_HHmmss"); 89 return ret; 90 }
◆環境
OpenCv 4.0.0
VisualStudio2017
回答1件
あなたの回答
tips
プレビュー