【原创】使用棋盘格实现摄像头序号标定
Author: chad
Mail: linczone@163.com
项目中遇到一个问题,通过usb接口在一个工控机上安装4个相同的摄像头,但是usb摄像头却没有设备ID等唯一性编码,导致无法很好的识别每个摄像头对应的工位序号.
为此,使用了类似棋盘格的方格实现摄像头序号标定.使用的序号板如下所示:
序号板设计为外框粗,内框细是为了方便轮廓查找。
由于序号板相对摄像头图像放置如果出现大幅度的旋转时, 摄像头无法确定图片的旋转方向,所以,程序中不考虑序号板安装偏转问题,实际使用时也要求安装角度必须小于10度.
运行效果如下:
如图所示,程序正确识别出序号板对应的序号.
源码如下:
/*
2015-07-24 linczone@163.com
编译命令如下:
g++ `pkg-config opencv --cflags` cam_seq.c -o cam_seq `pkg-config opencv --libs`
*/
#include <cstdio>
#include <cstring>
#include <iostream>
#include <cv.h>
#include <highgui.h>
#include <cmath>
#include <opencv/highgui.h>
#include <stdio.h>
using namespace std;
//旋转,缩放
int WarpAffine(IplImage *src, double angle, double scale )
{
CvPoint2D32f srcTri[3], dstTri[3];
CvMat* rot_mat = cvCreateMat(2,3,CV_32FC1);
IplImage *dst;
dst = cvCloneImage(src);
dst->origin = src->origin;
cvZero(dst);
//COMPUTE ROTATION MATRIX
CvPoint2D32f center = cvPoint2D32f(src->width/2,
src->height/2);
cv2DRotationMatrix(center,angle,scale,rot_mat);
cvWarpAffine(src,dst,rot_mat);
cvCopy(dst,src);
cvReleaseImage(&dst);
cvReleaseMat(&rot_mat);
return 0;
}
//获取4*4棋盘个中黑色方块对应的索引
int GetQPIndex(IplImage *srcImage)
{
int width=srcImage->width;
int height=srcImage->height;
int index = 0;
for(int row = height/8; row < height; row+=height/4)
{
//取图像的每一行的首地址
unsigned char* ptr = (unsigned char*)(srcImage->imageData + srcImage->widthStep * row );
//处理当前行
for(int col = width/8; col < srcImage->widthStep; col+=width/4)
{
index ++;
//对当前像素进行判断
ptr += col;