当前位置:文档之家› 彩色图像灰度化并进行高斯滤波

彩色图像灰度化并进行高斯滤波

#include "stdafx.h"
#include "highgui.h"
#include "cv.h"
//所有的以新风格命名的函数都在 cv 命名空间中
//如果希望不要每次都输入 cv:: ,则可使用下面语句
//using namespace cv;

void gaussianfilter(char* data, int width, int height)

{

int i, j, index, sum;

int templates[9] = { 1, 1, 1,

1, 1, 1,

1, 1, 1 };


sum = height * width * sizeof(char);

char *tmpdata = (char*)malloc(sum);

memcpy((char*)tmpdata,(char*)data, sum);

for(i = 1;i < height - 1;i++)

{

for(j = 1;j < width - 1;j++)
{

index = sum = 0;

for(int m = i - 1;m < i + 2;m++)

{

for(int n = j - 1; n < j + 2;n++)

{

sum +=
tmpdata[m * width + n] *

templates[index++];

}

}

data[i * width + j] = sum / 9;

}

}

}

int _tmain(int argc, _TCHAR* argv[])
{
IplImage* ColorImage;
IplImage* GrayImage;

ColorImage = cvLoadImage( "E:\\VSpoj\\left_0.bmp", -1 ); //载入文件
if (ColorImage == NULL)
return -1;

if (ColorImage == NULL)
return -1;

int i = ColorImage->width * ColorImage->height* sizeof(char);
char *ptr = (char*)malloc(i);

for(int j=0; jheight; j++)
{
for(int x=0; xwidth; x++)
ptr[j*ColorImage->width+x]=
(char)(ColorImage->imageData[3*(j*ColorImage->width+x)+1]); //对RGB三个值取最大值
}
GrayImage = cvCreateImageHeader(cvGetSize(ColorImage), ColorImage->depth, 1);
cvSetData(GrayImage, ptr, GrayImage->widthStep);



cvNamedWindow("GrayImage", CV_WINDOW_AUTOSIZE); //显示图像
cvShowImage("GrayImage", GrayImage);

gaussianfilter(GrayImage->imageData,GrayImage->width,GrayImage->height);

cvNamedWindow("GrayImage1", CV_WINDOW_AUTOSIZE); //显示图像
cvShowImage("GrayImage1", GrayImage);

cvNamedWindow("ColorImage", CV_WINDOW_AUTOSIZE); //显示图像
cvShowImage("ColorImage", ColorImage);

cvWaitKey(0);
cvDestroyWindow("GrayImage");
cvDestroyWindow("GrayImage1");
cvDestroyWindow("ColorImage");


}


相关主题
文本预览
相关文档 最新文档