本文主要是介绍图像差分 (IplImage *) 版,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
//图像差分
#include <stdio.h>
#include <stdlib.h>
#include "cv.h"
#include "highgui.h"
#include <iostream>using namespace std;void 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);step = X->widthStep/sizeof(uchar);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]);//255是白色if(dataX[i*step+j] != dataY[i*step+j])dataX_Y[i*step+j]=150;elsedataX_Y[i*step+j]=dataX[i*step+j];}
}int main()
{IplImage* pImgX;IplImage* pImgY;IplImage* pImgX_Y;CvSize dest_size;pImgX = cvLoadImage("mmr_out.jpg", -1);pImgY = cvLoadImage("zcr_out.jpg", -1);//cvShowImage("先看看", pImgX);if(pImgX==0 || pImgY==0){printf("载入文件失败!/n");return -1;}dest_size.width = pImgX->width;dest_size.height = pImgX->height;cout<<"width == "<<dest_size.width<<endl;cout<<"height == "<<dest_size.height<<endl;pImgX_Y = cvCreateImage(dest_size, pImgX->depth, pImgX->nChannels);//图像差分,最最关键的一步Image_Minus(pImgX, pImgY, pImgX_Y);//创建窗口cvNamedWindow("Picture X:", 1);cvNamedWindow("Picture Y:", 1);cvNamedWindow("Picture X-Y:", 1);//显示图像cvShowImage("Picture X:", pImgX);cvShowImage("Picture Y:", pImgY);cvShowImage("Picture X-Y:", pImgX_Y);cvWaitKey(0);//销毁窗口cvDestroyWindow("Picture X:");cvDestroyWindow("Picture Y:");cvDestroyWindow("Picture X-Y:");//释放图像cvReleaseImage(&pImgX);cvReleaseImage(&pImgY);cvReleaseImage(&pImgX_Y);return 0;
}
这篇关于图像差分 (IplImage *) 版的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!