(学习笔记5)BMP位图照片的灰度变换之二值化

这次我主要在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 -图像大小(宽、高)
  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值