📄 grayoperator.cpp
字号:
#include "StdAfx.h"
#include "GrayOperator.h"
#include <cmath>
#include <iostream>
#include <fstream>
using namespace std;
/******************************************************************************
* 作用: 线性变换函数
* 参数:
* pixel 原始像素数组
* tempPixel 保存变换后图像的像素数组
* width 原始图像宽度
* height 原始图像高度
* slope 线性函数的斜率
* inter 线性函数的截距
* 备注: 此函数对于彩色图同样适用
******************************************************************************/
void LineTrans(BYTE* pixel, BYTE* tempPixel, UINT width, UINT height,
double slope, double inter)
{
BYTE map[256];
double dTemp;
for (int i = 0; i < 256; i++)
{
dTemp = slope * i + inter;
if (dTemp < 0)
dTemp = 0.0;
else if (dTemp > 255)
dTemp = 255;
map[i] = int(dTemp + 0.5);
}
for (UINT i = 0; i < width * height; i++)
{
for (int j = 0; j < 4; j++)
tempPixel[i*4+j] = map[ pixel[i*4+j] ];
}
}
/******************************************************************************
* 作用: 对数变换函数
* 参数:
* pixel 原始像素数组
* tempPixel 保存变换后图像的像素数组
* width 原始图像宽度
* height 原始图像高度
* a 控制参数,表示曲线的上下偏移量
* b 控制参数,表示曲线的弯曲程度
* 备注: 此函数对于彩色图同样适用
******************************************************************************/
void LogTrans(BYTE* pixel, BYTE* tempPixel, UINT width, UINT height,
double a, double b)
{
BYTE map[256];
double dTemp;
for (int i = 0; i < 256; i++)
{
dTemp = log((double)i + 1.0) / b + a;
if (dTemp < 0)
dTemp = 0.0;
else if (dTemp > 255)
dTemp = 255;
map[i] = int(dTemp + 0.5);
}
for (UINT i = 0; i < width * height; i++)
{
for (int j = 0; j < 4; j++)
tempPixel[i*4+j] = map[ pixel[i*4+j] ];
}
}
/******************************************************************************
* 作用: 灰度阈值变换函数
* 参数:
* pixel 原始像素数组
* tempPixel 保存变换后图像的像素数组
* width 原始图像宽度
* height 原始图像高度
* thr 阈值
******************************************************************************/
void ThresholdTrans(BYTE* pixel, BYTE* tempPixel, UINT width, UINT height, int thr)
{
BYTE map[256];
for (int i = 0; i < 256; i++)
{
if (i >= thr)
map[i] = 255;
else
map[i] = 0;
}
for (UINT i = 0; i < width * height; i++)
{
int x = pixel[i * 4];
tempPixel[i*4] = tempPixel[i*4+1] = tempPixel[i*4+2] = map[x];
tempPixel[i*4+3] = 255;
}
}
/******************************************************************************
* 作用: 灰度均衡函数
* 参数:
* pixel 原始像素数组
* tempPixel 保存变换后图像的像素数组
* width 原始图像宽度
******************************************************************************/
void GrayEqualize(BYTE* pixel, BYTE* tempPixel, UINT width, UINT height)
{
// 灰度映射表
BYTE map[256];
long lCounts[256];
memset(lCounts, 0, sizeof(long) * 256);
// 计算各灰度值个数
for (UINT i = 0; i < width * height; i++)
{
int x = pixel[i * 4];
lCounts[x]++;
}
// 保存运算中的临时值
long lTemp;
for (int i = 0; i < 256; i++)
{
lTemp = 0;
for (int j = 0; j <= i; j++)
lTemp += lCounts[j];
map[i] = (BYTE)(lTemp * 255.0f / width / height);
}
// 变换后的值直接在映射表中查找
for (UINT i = 0; i < width * height; i++)
{
int x = pixel[i * 4];
tempPixel[i*4] = tempPixel[i*4+1] = tempPixel[i*4+2] = map[x];
tempPixel[i*4+3] = 255;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -