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)。
就想在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)。
相关文章推荐
- 第二章 SSH远程登录
- 【转】eclipse中window->preference选项中没有tomcat的解决方法
- datasheet下载网站 tps65951中文版手册
- red hat enterprise linux 6 改为中文
- Centos 窗口最小化后,找不到在哪里还原
- 【Linux】various questions
- Centos 改变中文时出现乱码
- linux下aio异步读写详解与实例
- Mac terminal从bash切换到zsh
- 【Linux高频命令专题(4)】sed
- 自定义Linux登录后的界面信息
- Linux密钥登录
- 第一章 安装CentOS 7系统
- Hadoop笔记之core-site.xml && hdfs-site.xml&&mapred-site.xml分析&&hadoop等集群环境搭建梳理
- linux上编译C
- shell script基础编程
- hadoop2集群安装和测试之demo测试
- linux下 链接 sqlserver数据库 驱动的安装
- 第 十 六 天 : 关 于 root 和 用 户 登 陆 的 问 题
- hadoop2集群安装和测试之window开发环境