用OpenCVSharp来生成棋盘格图片

◇◆丶佛笑我妖孽 提交于 2021-01-30 09:03:29

我发现,有很多人都在研究相机的标定,里面会经常用到棋盘,这里我们就用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(720, 1280, 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效果也和上面类似。

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!