|
发表于 2012-8-17 11:11:45
|
显示全部楼层
opencv排错总结
本人刚刚学习opencv,现在刚刚有点眉目,但是现在又遇到一道坎,就是遇到这个编译问题(每次都是卡在这些地方),报错error C2065: “BwImage”: 未声明的标识符等等,而opencv2.3.1在vs2010我已经配置好(之前编译的小程序都没问题),本人在网上找了N久都找不到问题,有请知道的人能指教指教,至于源代码如下:
#include <stdio.h>
#include<stdlib.h>
#include "highgui.h"
#include "cv.h"
int main(int argc,char**argv)
{
//声明指针,矩阵
IplImage*image=NULL;
IplImage*backimage=NULL;
IplImage*foreimage=NULL;
IplImage*tank=NULL;
CvMat*imagemat=NULL;
CvMat*foremat=NULL;
CvMat*backmat=NULL;
CvCapture*capture=NULL;
int num=0;
//创建窗口
cvNamedWindow("cvcamwindow",CV_WINDOW_AUTOSIZE);
cvNamedWindow("foreground",1);
cvNamedWindow("background",1);
cvNamedWindow("wo",1);
//读取摄像头
if(argc==1||(argc==2&&strlen(argv[1])==1&&isdigit(argv[1][0])))
capture=cvCaptureFromCAM(argc==2?argv[1][0]-\'0\':0);
else if(argc==2)
capture=cvCaptureFromAVI(argv[1]);
//声明视频结构
int isColor = 1;
int frameW=160;
int frameH=120;
int fps=8;
CvVideoWriter* camWriter=cvCreateVideoWriter("E:\\\\directx\\\\jianhua12.avi",-1,fps,cvSize(frameW,frameH),isColor);
if(!capture)
{
fprintf(stderr,"could not initialize capturing...\\n");
return 0;
}
//处理图像
while(image=cvQueryFrame(capture))
{
num++;
if(num==1)//第一针申请内存
{
foreimage=cvCreateImage(cvGetSize(image),8,1);
backimage=cvCreateImage(cvGetSize(image),8,1);
tank=cvCreateImage(cvGetSize(image),8,3);
backmat=cvCreateMat(image->height,image->width,CV_32FC1);
foremat=cvCreateMat(image->height,image->width,CV_32FC1);
imagemat=cvCreateMat(image->height,image->width,CV_32FC1);
//转换成单通道处理
cvCvtColor(image,backimage,CV_BGR2GRAY);
cvCvtColor(image,foreimage,CV_BGR2GRAY);
//矩阵初始化
cvConvert(foreimage,foremat);
cvConvert(foreimage,backmat);
cvConvert(foreimage,imagemat);
}
else
{
cvCvtColor(image,foreimage,CV_BGR2GRAY);
cvConvert(foreimage,imagemat);
//高斯滤波,平滑图像
cvSmooth(imagemat,imagemat,CV_GAUSSIAN,3,0,0);
//当前跟背景差分
cvAbsDiff(imagemat,backmat,foremat);
cvConvert(foremat,foreimage);
//二值化
cvThreshold(foreimage,foreimage,60,255.0,CV_THRESH_BINARY);
//进行形态学滤波,去掉噪音
cvDilate(foreimage, foreimage, 0, 1);
cvErode(foreimage, foreimage, 0, 1);
cvErode(foreimage, foreimage, 0, 1);
cvDilate(foreimage, foreimage, 0, 1);
int nl=foreimage->height;
int nc=foreimage->width;
BwImage foreimageA(foreimage);
RgbImage imageA(image);
RgbImage tankA(tank);
for(int i=0;i<nl;i++)
{
for(int j=0;j<nc;j++)
{
if (foreimageA[j]==0)
{
tankA[j].b=0;
tankA[j].g=0;
tankA[j].r=0;
}
else
tankA[j]=imageA[j];
}
}
tank->origin=image->origin;
//保存前景
cvWriteFrame(camWriter,tank);
//更新背景
cvRunningAvg(imagemat,backmat,0.005,0);
//将背景转化为图像格式
cvConvert(backmat,backimage);
//使图像正立
backimage->origin=image->origin;
foreimage->origin=image->origin;
//显示图像
cvShowImage("cvcamwindow", image);
cvShowImage("background", backimage);
cvShowImage("foreground" ,foreimage);
cvShowImage("wo",tank);
//时间缓冲
int key=cvWaitKey(90);
if(cvWaitKey(2)>=0)
break;
}
}
cvDestroyWindow("cvcam window");
cvDestroyWindow("background");
cvDestroyWindow("foreground");
cvDestroyWindow("wo");
cvReleaseImage(&foreimage);
cvReleaseImage(&backimage);
cvReleaseImage(&tank);
cvReleaseMat(&imagemat);
cvReleaseMat(&foremat);
cvReleaseMat(&backmat);
cvReleaseVideoWriter (&camWriter);
return 0;
} |
|