1.均值滤波
含义:把每个像素都用周围的8个像素来做均值操作 。
均值滤波器有什么用处呢?
主要还是平滑图像的用处, 有的图像的锐度很高,用这样的均值算法,可以把锐度降低。使得图像看上去更加自然。(只能微弱的降低噪声,不能剔除噪声,对降噪几乎没用)
源码:
#include "highgui.h"
#include "cv.h"
void MeanFilter(uchar* smooth, int width, int height)
{
int sum = height * width * sizeof(uchar);//图像所占内存容量
uchar *corrupted = (uchar*)malloc(sum);
memcpy((char*)corrupted, (char*)smooth, sum);
for (int j = 1; jimageData, pImg->width, pImg->height);
cvShowImage("image-out", pImg);
cvReleaseImage(&pGrayImg);
cvWaitKey(0);
cvDestroyWindow("image-in");
cvDestroyWindow("image-out");
}
int main()
{
IplImage* img = cvLoadImage("E:\\11.jpg");
imgOperate(img);
cvReleaseImage(&img);
return 0;
}
2.中值滤波
均值滤波是像素周围的3*3的像素做平均值操作, 那么中值就是在3*3中的像素中寻找中值。
可以清晰地看到, 这里的6,2,0,3,97,4,19,3,10这些像素最终都被中值4取代。
源码:
#include "highgui.h"
#include "cv.h"
void MedianFilter(uchar* smooth, int width, int height)
{
int sum = height * width * sizeof(uchar);//图像所占内存容量
uchar *corrupted = (uchar*)malloc(sum);
memcpy((char*)corrupted, (char*)smooth, sum);
for (int j=1;jimageData, pImg->width, pImg->height);
cvShowImage("image-out", pImg);
cvReleaseImage(&pGrayImg);
cvWaitKey(0);
cvDestroyWindow("image-in");
cvDestroyWindow("image-out");
}
int main()
{
IplImage* img = cvLoadImage("E:\\11.jpg");
imgOperate(img);
cvReleaseImage(&img);
return 0;
}
3.混合中值滤波器(Hybrid Median Filter)
首先当前像素的上下左右和自身取中值 , 然后左上右上左下右下和自身取中值 , 完了前面的两个值和当前像素值再取一次中值 , 得到的值就是最后的终极像素值了。
#include "highgui.h"
#include "cv.h"
unsigned char median(unsigned char* elements, int width)
{
// Order elements (only half of them)
for (int i = 0; i < (width >> 1) + 1; ++i)
{
// Find position of minimum element
int min = i;
for (int j = i + 1; j < width; ++j)
if (elements[j] < elements[min])
min = j;
// Put found minimum element in its place
unsigned char temp = elements[i];
elements[i] = elements[min];
elements[min] = temp;
}
// Get result - the middle element
return elements[width >> 1];
}
void hybridMedianFilter(uchar* smooth, int width, int height)
{
int sum = height * width * sizeof(uchar);//图像所占内存容量
uchar *corrupted = (uchar*)malloc(sum);
memcpy((char*)corrupted, (char*)smooth, sum);
for (int j = 1; jimageData, pImg->width, pImg->height);
cvShowImage("image-out", pImg);
cvReleaseImage(&pGrayImg);
cvWaitKey(0);
cvDestroyWindow("image-in");
cvDestroyWindow("image-out");
}
int main()
{
IplImage* img = cvLoadImage("E:\\11.jpg");
imgOperate(img);
cvReleaseImage(&img);
return 0;
}