写的一个棋盘格生成小程序,很简单,但是很实用,对于经常使用棋盘格的人来说很实用

vs配好opencv运行即可

运行窗口示意图,将参数写入即可。


生成棋盘格自动保存到工程目录下


代码附上:

#include<opencv2\highgui\highgui.hpp>

#include<opencv2\opencv.hpp>
#include<opencv2\core\core.hpp>
#include <fstream>//ofstream类的头文件
using namespace cv;
using namespace std;
 int length ;      //棋盘格大小像素为单位
 int Background_gray_value;
 int Foreground_gray_value;
 cv::Size size;
 cv::Size resolution;
 int flag = 0;
 char name[20];
 char s1[]="jpg";
 char s2[]="png";
 
int main(int argc,char* argv[])
{   cout<<"每个小方块的像素大小: ";
    cin>>length;
    cout<<"棋盘格纵向个数:" ;
cin>>size.height;
cout<<"棋盘格横向个数:";
cin>>size.width;
cout<<"纵向像素:";
cin>>resolution.height;
cout<<"横向像素:";
cin>>resolution.width;
cout << "深颜色灰度值(0为全黑):";
cin >> Background_gray_value;
cout << "浅颜色灰度值(255为全白):";
cin >> Foreground_gray_value;
cv::Mat image(resolution, CV_8UC1, cv::Scalar::all(Foreground_gray_value));
cv::Mat GenerateChessboardImg(cv::Size &size, int& length, cv::Size &resolution, const char* format);
    int basisHeight = (resolution.height - length*size.height) / 2;  //将棋盘格放置到所需像素分辨率图像的正中央
int basisWidth = (resolution.width - length*size.width) / 2;     //两边均分
if( basisHeight < 0  ||  basisWidth < 0)                        //避免出错
{
cout<<"超出范围!"<<endl;
}
      
    for(int j = 0;j < size.height;j++)
       {
        for(int i = 0;i < size.width;i++)
         {
     flag = (i+j) % 2;
      if(flag == 0)
            {
        for(int n = j * length;n < (j+1) * length;n++)
          for(int m = i * length;m < (i+1) * length;m++)
  image.at<uchar>(n + basisHeight, m + basisWidth) = Background_gray_value;
            }
          }
        }

    cout<<"输出图片格式:";
cin>> name;
    if(strcmp(name,s1)==0)
    {
         cv::imwrite("chessboard.jpg",image);
    }else if (strcmp(name,s2)==0){
       cv::imwrite("chessboard.png",image);
        }else{
        cv::imwrite("chessboard.bmp",image);
        }  
cv::imshow("123",image);
cv::waitKey(0);
  return 0;
}

猜你喜欢

转载自blog.csdn.net/konglingshneg/article/details/80451408