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;
long x,y;
x = y = 0;
for( int i= row; i< row+height/8; i++ ) {
unsigned char* tmptr = (unsigned char*)(srcImage->imageData + srcImage->widthStep * i );
for( int j = col; j < col+width/8; j++ ) {
x ++;
y = y + (int)tmptr[j];
}
}
if( y / x < 100 ) {
return index;
}
}
}
return 0;
}
//void callTrackbar( int position )
//{
//}
int main(int argc,char *argv[])
{
char tmpbuf[100];
CvCapture* capture ;
cvNamedWindow("cvCanny");
if( argc != 1 )
capture = cvCreateCameraCapture( atoi(argv[1]) ); //从视频设备获取图像
else
capture = cvCreateCameraCapture( 0 ); //从视频设备获取图像
IplImage *frame;
IplImage *out = NULL;
IplImage *grayimg = NULL;
int i = 0;
double length,area,rectArea;
double rectDegree = 0.0; //矩形度
double long2Short = 1.0; //体态比
int DirectFlag = 0; //长宽颠倒标准
//计算边界序列的参数 长度 面积 矩形 最小矩形
//并输出每个边界的参数
CvRect rect;
CvBox2D box;
int imageCnt = 1;
double axisLong = 0.0, axisShort = 0.0;
double temp;
frame = cvQueryFrame( capture ); //读取一帧数据
if( !frame ) {
cerr<<"cvQueryFrame fail!"<<endl;
return -1;
}
out = cvCreateImage(cvGetSize(frame),IPL_DEPTH_8U,1); //深度8bit 单通道
grayimg = cvCreateImage(cvGetSize(frame),IPL_DEPTH_8U,1);
int cannyMax,cannyMin,threshod;
cannyMax = 0;
cannyMin = 150;
threshod = 0;
while(1)
{
frame = cvQueryFrame( capture ); //读取一帧数据
if( !frame ) break;
WarpAffine( frame, 180, 1.0 );
cvCvtColor( frame, grayimg, CV_RGB2GRAY ); //rgb转换为灰度图像
cvCanny( grayimg, out, cannyMin, cannyMax, 3 ); //边缘检测处理
cvShowImage("cvCanny",out); //显示图像
if( !cannyMax ) {
cannyMax = 250;
cannyMin = 150;
cvCreateTrackbar("cannyMax", "cvCanny", &cannyMax, 255, NULL );
cvSetTrackbarPos( "cannyMax", "cvCanny", cannyMax );
cvCreateTrackbar("cannyMin", "cvCanny", &cannyMin, 255, NULL );
cvSetTrackbarPos( "cannyMin", "cvCanny", cannyMin );
}
//寻找轮廓
CvMemStorage *storage = cvCreateMemStorage(0);
CvSeq * seq = cvCreateSeq(CV_SEQ_ELTYPE_POINT, sizeof(CvSeq), sizeof(CvPoint), storage);
CvSeq * tempSeq = cvCreateSeq(CV_SEQ_ELTYPE_POINT, sizeof(CvSeq), sizeof(CvPoint), storage);
int cnt = cvFindContours( out, storage, &seq );//返回轮廓的数目
//cout<<"number of contours "<<cnt<<endl;
int rectCountNum = 0;
for (tempSeq = seq;tempSeq != NULL; tempSeq = tempSeq->h_next)
{
length = cvArcLength(tempSeq);
area = cvContourArea(tempSeq);
//筛选面积比较大的区域
if( area > 5000 ) {
//外接矩形
rect = cvBoundingRect(tempSeq,1);
//绘制轮廓和外接矩形
//cvDrawContours(frame,tempSeq,CV_RGB(255,0,0),CV_RGB(255,0,0),0);
//cvRectangleR(frame,rect,CV_RGB(0,255,0));
//cvShowImage("dst",frame);
//cvWaitKey(0);
//绘制外接最小矩形
CvPoint2D32f pt[4];
box = cvMinAreaRect2(tempSeq,0);
cvBoxPoints(box,pt);
//下面开始分析图形的形状特征
//长轴 短轴
axisLong = sqrt(pow(pt[1].x -pt[0].x,2) + pow(pt[1].y -pt[0].y,2));
axisShort = sqrt(pow(pt[2].x -pt[1].x,2) + pow(pt[2].y -pt[1].y,2));
DirectFlag = 0;
if( axisShort > axisLong ) {
temp = axisLong;
axisLong = axisShort;
axisShort= temp;
DirectFlag = 1;
}
rectArea = axisLong * axisShort;
rectDegree = area / rectArea;
//体态比or长宽比 最下外接矩形的长轴和短轴的比值
long2Short = axisLong/axisShort;
//cout<<"long2Short: "<<long2Short<<endl;
//cout<<"rectDegree: "<<rectDegree<<endl;
//cout<<"rectArea : "<<rectArea<<endl;
if( long2Short> 0.9 && long2Short < 1.1 && rectDegree > 0.9 && rectArea > 5000 )
{
//cout<<"Length: "<<length<<endl;
//cout<<"Area : "<<area<<endl;
//cout<<"long axis : "<<axisLong<<endl;
//cout<<"short axis: "<<axisShort<<endl;
//cout<<"long2Short: "<<long2Short<<endl;
//cout<<"rectArea : "<<rectArea<<endl;
//cout<<"rectDegree: "<<rectDegree<<endl;
//cout<<"rectCountNum: "<<rectCountNum++<<endl;
for(int i = 0;i<4;++i) {
cvLine(frame,cvPointFrom32f(pt[i]),cvPointFrom32f(pt[((i+1)%4)?(i+1):0]),CV_RGB(255,0,0));
}
//cvShowImage("out",frame);
IplImage *cutimg = NULL;
IplImage *cutthreshimg = NULL,*cuttmpimg;
cvSetImageROI(frame,rect);
cutimg = cvCreateImage(cvSize(rect.width,rect.height),IPL_DEPTH_8U,3);
cutthreshimg = cvCreateImage(cvGetSize(cutimg),IPL_DEPTH_8U,1);
cuttmpimg = cvCreateImage(cvGetSize(cutimg),IPL_DEPTH_8U,1);
cvCopy(frame,cutimg,0);
//cvCvtColor( cutimg, cutthreshimg, CV_RGB2GRAY ); //rgb转换为灰度图像
//cvThreshold( cuttmpimg, cutthreshimg, threshod, 255, CV_THRESH_BINARY );//图像二值化
//cvAdaptiveThreshold( cuttmpimg, cutthreshimg, 255, CV_ADAPTIVE_THRESH_GAUSSIAN_C/*CV_ADAPTIVE_THRESH_MEAN_C*/,
// CV_THRESH_BINARY, 3, 5 ); //自适应阈值化
//cout<<"box.angle: "<<abs(box.angle)<<endl;
//图像旋转
//if( abs(box.angle) < 90 ) //旋转角度在0度附近时会出现0<>90 震荡! 实际使用时旋转角度也不会超过30度!
//WarpAffine( cutimg, DirectFlag ?box.angle:box.angle+90, 1.0 );
//WarpAffine( cutthreshimg, DirectFlag ?box.angle:box.angle+90, 1.0 );
cvShowImage("cutimg",cutimg);
IplImage *cutmpimg = NULL;
cutmpimg = cvCreateImage(cvGetSize(cutimg),IPL_DEPTH_8U,1); //深度8bit 单通道
cvCvtColor( cutimg, cutmpimg, CV_RGB2GRAY ); //rgb转换为灰度图像
cvThreshold( cutmpimg, cutmpimg, 105, 255, CV_THRESH_BINARY );//图像自适应二值化
//cvShowImage("threshold",cutmpimg);
cout<<"--------------index="<<GetQPIndex(cutmpimg)<<endl;
if( !threshod ) {
threshod = 150;
cvCreateTrackbar("cvThreshold", "cutimg", &threshod, 255, NULL );
cvSetTrackbarPos( "cvThreshold", "cutimg", threshod );
}
cvResetImageROI(frame);
cvReleaseImage( &cutimg );
cvReleaseImage( &cuttmpimg );
cvReleaseImage( &cutthreshimg );
}
}
}
char c = cvWaitKey(150); //等待按键输入
if( c == 27 ) {
cvReleaseMemStorage( &storage );
break;
}
}
cvReleaseCapture( &capture );
cvReleaseImage( &frame );
cvReleaseImage( &out );
cvReleaseImage( &grayimg );
cvDestroyWindow("out");
return 0;
}