OPENCV常用小代码


opencv抓取帧:

OpenCV支持从摄像头或视频文件(AVI)中抓取图像并保存为另一视频文件.

 一、

    从摄像头获取初始化:

    CvCapture* capture = cvCaptureFromCAM(0); // capture from video device #0

    从视频文件filename.avi获取初始化:

    CvCapture* capture = cvCaptureFromAVI("infile.avi");

    抓取帧:

    IplImage* img = 0; 
    if(!cvGrabFrame(capture)){               // 抓取一帧,失败退出 
       printf("Could not grab a frame\n");
       exit(0);
    }
    img=cvRetrieveFrame(capture);            // 恢复获取的帧图像

    要从多个摄像头同时获取图像, 首先从每个摄像头抓取一帧. 在抓取动作都结束后再恢复帧图像.  
    释放抓取源(和释放单幅图像时类似):

    cvReleaseCapture(&capture);

      注意由设备抓取的图像是由capture函数自动分配和释放的. 不要试图自己释放它.

    获取设备特性:

    cvQueryFrame(capture); // this call is necessary to get correct 
                            // capture properties
    int frameH     = (int) cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_HEIGHT);
    int frameW     = (int) cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_WIDTH);
    int fps        = (int) cvGetCaptureProperty(capture, CV_CAP_PROP_FPS);
    int numFrames  = (int) cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_COUNT);

    所有帧数似乎只与视频文件有关. 用摄像头时不对,奇怪!!!.  
    获取帧信息:

    float posMsec    =        cvGetCaptureProperty(capture, CV_CAP_PROP_POS_MSEC);
    int posFrames    =  (int) cvGetCaptureProperty(capture, CV_CAP_PROP_POS_FRAMES);
    float posRatio   =        cvGetCaptureProperty(capture, CV_CAP_PROP_POS_AVI_RATIO);

    获取所抓取帧在视频序列中的位置, 从首帧开始按[毫秒]算. 或者从首帧开始从0标号, 获取所抓取帧的标号. 或者取相对位置,首帧为0,末帧为1, 只对视频文件有效. 
    设定所抓取的第一帧标号:

    // 从视频文件相对位置0.9处开始抓取
    cvSetCaptureProperty(capture, CV_CAP_PROP_POS_AVI_RATIO, (double)0.9);

    只对从视频文件抓取有效. 不过似乎也不成功!!! 




图像差分


void imgDifferUtil::Image_Minus(IplImage *X, IplImage *Y, IplImage *X_Y)
{
    //图像差分函数,将图像1中像素和图像2中对应像素想减,要求X、Y、X_Y大小相同
    int i,j,width,height,step,chanel;
    unsigned char *dataX, *dataY, *dataX_Y;
    width = X->width;
    height = X->height;
    dataX = (unsigned char *)X->imageData;
    dataY = (unsigned char *)Y->imageData;
    dataX_Y = (unsigned char *)X_Y->imageData;
    step = X->widthStep/sizeof(char);
    chanel = X->nChannels;
    for(i=0; i<height; i++)
        for(j=0; j<width*chanel; j++)
            dataX_Y[i*step+j] = abs( dataX[i*step+j] - dataY[i*step+j]);
}


opencv变为灰度图及新定义大小

IplImage* toGray(IplImage* img)
{
	IplImage* dst = cvCreateImage(cvGetSize(img),img->depth,1);
	IplImage* tmp =dst ;
	if(NULL == dst)
		return NULL;
	cvCvtColor(img, dst, CV_BGR2GRAY);
	//cvReleaseImage(&dst);

	return dst;
}
IplImage* toNewSize(IplImage* pFrame)
{
    IplImage* dst = cvCreateImage(cv::Size(EVALUATE_IMG_WIDTH,EVALUATE_IMG_HEIGHT),pFrame->depth,pFrame->nChannels); //构造目标图象
    if(dst==NULL)
    	return NULL ;
    cvResize(pFrame,dst,CV_INTER_NN);
    return dst ;
}

显示灰度


void printGrayScale(IplImage *src)
{
	int width=src->width;//图片宽度
	int height = src->height;//图片高度

	for (size_t row=0;row<height;row++)
	{
		uchar* ptr = (uchar*)src->imageData+row*src->width;//获得灰度值数据指针
		for (size_t cols=0;cols<width;cols++)
		{
			int intensity=ptr[cols];
			printf("%03d  ",intensity);
		}
		printf("\n");
	}
}

图像灰度,均值,指纹,方差


unsigned int getAverageWithPrintGrayScale(IplImage *src,bool printflag)
{
	unsigned int average=0 ;
	int width=src->width;//图片宽度
	int height = src->height;//图片高度
	// 打印图像灰度
	for (size_t row=0;row<height;row++)
	{
		uchar* ptr = (uchar*)src->imageData+row*src->width;//获得灰度值数据指针
		for (size_t cols=0;cols<width;cols++)
		{
			int intensity=ptr[cols];
			if(printflag)
				printf("%03d  ",intensity);
			average +=intensity ;
		}
		if(printflag)
			printf("\n");
	}
	average /= width*height;
	if(printflag)
		printf("average:%d  \n",average);
	return average ;
}
void  getFingerPrint(IplImage *src,unsigned int average ,bool printflag)
{
	int width=src->width;//图片宽度
	int height = src->height;//图片高度
	// 打印图像指纹
	for (size_t row=0;row<height;row++)
	{
		uchar* ptr = (uchar*)src->imageData+row*src->width;//获得灰度值数据指针
		for (size_t cols=0;cols<width;cols++)
		{
			int intensity= ( ptr[cols] > average) ? 1 : 0 ;
			printf("%d  ",intensity);
		}
		printf("\n");
	}
}
float getVariance(IplImage *src,unsigned int average,bool printflag)
{
	int width=src->width;//图片宽度
	int height = src->height;//图片高度
	//求图像方差
	float variance=0 ;int val =0 ; int tmp =0 ;
	for (size_t row=0;row<height;row++)
	{
		uchar* ptr = (uchar*)src->imageData+row*src->width;//获得灰度值数据指针
		for (size_t cols=0;cols<width;cols++)
		{
			val = ptr[cols] ;
			tmp = ( val- average)*( val- average) ;
			//printf("%03d %d",ptr[cols],tmp);
			variance = variance + (float)tmp ;
			//printf(" v:%f **",variance);
		}
		//printf("\n");
	}
	//printf("varianceALL:%f \n",variance);
	variance/=width*height ;
	//printf("varianceOne:%f \n",variance);
	variance = sqrtf(variance);
	if(printflag)
		printf("variance:%f \n",variance);
	return variance ;
}






你可能感兴趣的:(opencv,常用小算法)