|
#ifndef SPATIALDOMAIN_H
#define SPATIALDOMAIN_H
#include <cmath>
#include <string>
namespace nsimgtk
{
// 8阶灰度图的灰度反转算法
class NegativeGray8
{
public:
void operator()(unsigned char *const p_value)
{
*p_value ^= 0xff;
}
};
// 8阶灰度图的Gamma校正算法
class GammaCorrectGray8
{
private:
unsigned char d_s[256];
public:
GammaCorrectGray8::GammaCorrectGray8(double c, double gamma);
void operator()(unsigned char*const p_value)
{
*p_value = d_s[*p_value];
}
};
// 8阶灰度图的饱和度拉伸算法
class ContrastStretchingGray8
{
private:
unsigned char d_s[256];
public:
ContrastStretchingGray8::ContrastStretchingGray8(double a1, double b1, unsigned int x1,
double a2, double b2, unsigned int x2, double a3, double b3);
void operator()(unsigned char*const p_value)
{
*p_value = d_s[*p_value];
}
};
// 8阶灰度图的位平面分割,构造函数指定位平面号
class BitPlaneSliceGray8
{
private:
unsigned char d_s[256];
public:
BitPlaneSliceGray8(unsigned char bitPlaneNum);
void operator()(unsigned char* const p_value)
{
*p_value = d_s[*p_value];
}
};
}
#endif
// 上述类中各构造函数的实现代码,应该分在另一个文件中,此处为说明方便,一并列出
#include "SpatialDomain/spatialDomain.h"
namespace nsimgtk
{
GammaCorrectGray8::GammaCorrectGray8(double c, double gamma)
{
double temp;
for (unsigned int i=0; i<256; ++i)
{
temp = ceil(c * 255.0 * pow(double(i)/255.0, gamma));
d_s[i] = unsigned char(temp);
}
}
ContrastStretchingGray8::ContrastStretchingGray8(double a1, double b1, unsigned int x1,
double a2, double b2, unsigned int x2, double a3, double b3)
{
if (x1 > 255 || x2 > 255 || x1 > x1)
{
for (unsigned int i=0; i<256; ++i)
d_s[i] = i;
}
else
{
double tmp;
for (unsigned int i=0; i<x1; ++i)
{
tmp = ceil(a1*double(i)+b1);
d_s[i] = (unsigned char)tmp;
}
for (unsigned int i=x1; i<x2; ++i)
{
tmp = ceil(a2*double(i)+b2);
d_s[i] = (unsigned char)tmp;
}
for (unsigned int i=x2; i<256; ++i)
{
tmp = ceil(a3*double(i)+b3);
d_s[i] = (unsigned char)tmp;
}
}
}
BitPlaneSliceGray8::BitPlaneSliceGray8(unsigned char bitPlaneNum)
{
unsigned char bitMaskArray[8] =
{
0x01, 0x02, 0x04, 0x08,
0x10, 0x20, 0x40, 0x80
};
for (unsigned int i=0; i<256; ++i)
{
unsigned char tmp = i;
tmp &= bitMaskArray[bitPlaneNum];
tmp = (tmp >> bitPlaneNum) * 255;
d_s[i] = tmp;
}
}
} |