OpenCv圖像差分(算法自己實現)
效果杠杠的!!
//圖像差分
#include <stdio.h>
#include <stdlib.h>
#include "cv.h"
#include "highgui.h"
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);
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]);
}
int main()
{
IplImage* pImgX;
IplImage* pImgY;
IplImage* pImgX_Y;
CvSize dest_size;
pImgX = cvLoadImage("D://bishe//07_31_10_09_41_08.bmp", -1);
pImgY = cvLoadImage("D://bishe//07_31_10_11_40_48.bmp", -1);
if(pImgX==0 || pImgY==0)
{
printf("載入文件失敗!/n");
return -1;
}
dest_size.width = pImgX->width;
dest_size.height = pImgX->height;
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;
}
加注釋版:
//圖像差分
#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);
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]);
}
int main()
{
IplImage* pImgX;
IplImage* pImgY;
IplImage* pImgX_Y;
CvSize dest_size;
pImgX = cvLoadImage("mmr_out.jpg", -1);
pImgY = cvLoadImage("zcr_out.jpg", -1);
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;
}
最後更新:2017-04-03 05:40:06