这次我主要在BMP位图照片的读取和显示的基础上,加上了对BMP位图照片的灰度变换。都是一层层上来的,这次不打算一次性更新完灰度变换,我会一个个来更新,这来更新的节奏会快一点,避免一次内容太多,撰写时间过长。
废话不多说,直接上干货。
灰度变换包括以下几个操作:
二值化
直方图
直方图均衡
反转
阀值变换
分段线性拉伸
step1. 绘制菜单栏
step2. 接下来,我们对”灰度变换“下的”二值化“进行修改ID。如下图所示:
step3.添加GrayTrans这个类。如下图:
GrayTrans.h中的代码如下:
// GrayTrans.h: interface for the GrayTrans class.
//
//////////////////////////////////////////////////////////////////////
#if !defined(AFX_GRAYTRANS_H__8F98F0EE_4F7D_4138_8573_4E4A065784C2__INCLUDED_)
#define AFX_GRAYTRANS_H__8F98F0EE_4F7D_4138_8573_4E4A065784C2__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include "ImageDib.h"
class GrayTrans : public ImageDib
{
public:
//输出图像每像素位数
int m_nBitCountOut;
//输出图像位图数据指针
unsigned char * m_pImgDataOut;
//输出图像颜色表
LPRGBQUAD m_lpColorTableOut;
//输出图像的宽,像素为单位
int m_imgWidthOut;
//输出图像的高,像素为单位
int m_imgHeightOut;
//输出图像颜色表长度
int m_nColorTableLengthOut;
public:
//不带参数的构造函数
GrayTrans();
//带参数的构造函数
GrayTrans(CSize size, int nBitCount, LPRGBQUAD lpColorTable,
unsigned char *pImgData);
//析构函数
~GrayTrans();
//以像素为单位返回输出图像的宽和高
CSize GetDimensions();
//二值化
void BinaryImage(int threshold=128);
//反转
void RevImage();
//窗口变换
void ThresholdWindow(int bTop, int bBottom);
//线性拉伸
void LinearStrech(CPoint point1,CPoint point2);
private:
//单通道数据线性拉伸
void LinearStrechForSnglChannel(unsigned char *pImgDataIn,
unsigned char *pImgDataOut,int imgWidth,int imgHeight,
CPoint point1,CPoint point2);
};
#endif // !defined(AFX_GRAYTRANS_H__8F98F0EE_4F7D_4138_8573_4E4A065784C2__INCLUDED_)
GrayTrans.cpp中的代码如下:
// GrayTrans.cpp: implementation of the GrayTrans class.
//
//
#include "stdio.h"
#include "stdafx.h"
#include "demo1.h"
#include "GrayTrans.h"
#include "math.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
/***********************************************************************
* 函数名称: GrayTrans()
* 说明:无参数的构造函数,对成员变量进行初始化
***********************************************************************/
GrayTrans::GrayTrans()
{
m_pImgDataOut=NULL;//输出图像位图数据指针为空
m_lpColorTableOut=NULL;//输出图像颜色表指针为空
m_nColorTableLengthOut=0;//输出图像颜色表长度为0
m_nBitCountOut=0;//输出图像每像素位数为0
m_imgWidthOut=0;//输出图像的宽为0
m_imgHeightOut=0;//输出图像的高为0
}
/***********************************************************************
* 函数名称: GrayTrans()
* 函数参数: CSize size -图像大小(宽、高)