您的位置:首页 > 运维架构

opencv2-3生成标定用的方格图

2015-09-27 11:05 393 查看
opencv2-3生成标定用的方格图
就想在A4纸上山寨打印一个标定图,就是黑白方格相间的那种。A4纸的标准大小为210*297mm。从每个像素的角度考虑,考虑其最终落入哪个小方块,如果落入小方块的行列之和为奇数,则设置为白色。首先准备一张跟A4纸比例相同的纯黑图片,可以用电脑自带的画图生成。然后该设置为白的地方设置为白就行了。

#include <opencv\cv.h>

#include <opencv\highgui.h>

#include <iostream>

using namespace cv;

using namespace std;

int main()

{

Scalar s(0);

Mat frame(630,891,CV_8UC1,s);

//Mat frame = imread("1.jpg"); // cols*rows = 630*891

int nc = frame.channels();

int nWidthOfROI = 90;

for (int j=0;j<frame.rows;j++)

{

uchar* data= frame.ptr<uchar>(j);

for(int i=0;i<frame.cols*nc;i+=nc)

{

if( (i/nc/nWidthOfROI + j/nWidthOfROI) % 2)

{

// bgr

data[i/nc*nc + 0] = 255 ;

data[i/nc*nc + 1] = 255 ;

data[i/nc*nc + 2] = 255 ;

}

}

}

imshow("test",frame);

waitKey(0);

return 0;

}



解析说明:

1)Scalar s(0);

template<typename _Tp> class CV_EXPORTS Scalar_ : public Vec<_Tp, 4>

{

public:

//! various constructors

Scalar_();

Scalar_(_Tp v0, _Tp v1, _Tp v2=0, _Tp v3=0);

Scalar_(const CvScalar& s);

Scalar_(_Tp v0);

... ...}

typedef Scalar_<double> Scalar;

选择Scalar_(_Tp v0);这个成员函数,赋值给s为全0。意思是图片的全黑色底色。

2)Mat frame(630,891,CV_8UC1,s);

class CV_EXPORTS Mat

{

public:

//! default constructor

Mat();

//! constructs 2D matrix of the specified size and type

// (_type is CV_8UC1, CV_64FC3, CV_32SC(12) etc.)

Mat(int _rows, int _cols, int _type);

Mat(Size _size, int _type);

//! constucts 2D matrix and fills it with the specified value _s.

Mat(int _rows, int _cols, int _type, const Scalar& _s);

... ...}

选择这个Mat(int _rows, int _cols, int _type, const Scalar& _s)成员函数,创建一个630*891大小的矩阵,数据类型为CV_8UC1,元素初始化为s的值,s为全0即为全黑底色。

3) for (int j=0;j<frame.rows;j++)

{

uchar* data= frame.ptr<uchar>(j);

for(int i=0;i<frame.cols*nc;i+=nc)

{

if( (i/nc/nWidthOfROI + j/nWidthOfROI) % 2)

{

// bgr

data[i/nc*nc + 0] = 255 ;

data[i/nc*nc + 1] = 255 ;

data[i/nc*nc + 2] = 255 ;

}

}

}

这是比较经典的利用头指针的元素赋值方法。关键的地方在于:i/nc/nWidthOfROI + j/nWidthOfROI) % 2,即如果落入小方块的行列之和为奇数,则设置为白色(255)。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: