用OpenCVSharp來生成棋盤格圖片


我發現,有很多人都在研究相機的標定,里面會經常用到棋盤,這里我們就用OpencvSharp來自己做一個棋盤函數。

直接上代碼。

 1  /// <summary>
 2         /// 用於生成標准棋盤格的函數
 3         /// </summary>
 4         /// <param name="imgw"></param>
 5         /// <param name="imgh"></param>
 6         /// <param name="chesss_size"></param>單個棋盤格子的大小
 7         /// <param name="board_width"></param>棋盤的寬的格子數量
 8         /// <param name="board_height"></param>棋盤的高的格子數量
 9         /// <param name="savepath"></param>
10         /// <param name="imgformat"></param>代表圖片格式,枚舉型
11         public static void GenChessBoard(int imgw,int imgh,int chesss_size,int board_width_count,int board_height_count,string savename, Imgformat imgformat= Imgformat.Jpg)
12         {
13             try
14             {
15                 Mat Targermat = new Mat(imgh, imgw, MatType.CV_8UC1, Scalar.White);
16                 int startrow = (imgh - board_height_count * chesss_size) / 2;
17                 int startcol = (imgw - board_width_count * chesss_size) / 2;
18                 bool initialvalue = true;
19                 bool col_color = true;
20                 int rowcout = 0;
21                 int colcout = 0;
22                 for (int row = startrow; row < (startrow + board_height_count * chesss_size); row++)
23                 {
24                     rowcout++;
25                     for (int col = startcol; col < (startcol + board_width_count * chesss_size); col++)
26                     {
27                         if (col_color)
28                         {                         
29                             Targermat.SetArray(row, col, new byte[] { 0 });
30                         }                       
31 
32 
33                         colcout++;
34                         if (colcout == chesss_size && col != (startcol + board_width_count * chesss_size - 1))
35                         {
36                             col_color = !col_color;
37                             colcout = 0;
38                         }
39                     }
40 
41                     if (rowcout == chesss_size)
42                     {
43                         initialvalue = !initialvalue;
44                         rowcout = 0;
45 
46                     }
47                     colcout = 0;
48                     col_color = initialvalue;
49                 }
50                 if(imgformat== Imgformat.Jpg)
51                 {
52                     Targermat.SaveImage(savename + ".jpg");
53                 }
54                 else if(imgformat == Imgformat.Bmp)
55                 {
56 
57                     Targermat.SaveImage(savename + ".bmp");
58                 }
59                 Targermat.Dispose():
60 
61             }
62             catch(Exception ex)
63             {
64                 throw (ex);
65 
66             }            
67         }
  public enum Imgformat
    {
        Jpg=0,
        Bmp=1
    }

直接調用,然后生成棋盤圖片

 MyCalibration.GenChessBoard(1280, 720, 50, 10, 8, @"D:\chesstest");

或者生成BMP格式。

MyCalibration.GenChessBoard(1280, 720, 50, 10, 8, @"D:\chesstest",Imgformat.Bmp);

效果圖如下:(jpg)

 

 

 

 

 

 

 

BMP效果也和上面類似。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM