visual Studio MFC 平台实现图像增强中Gray-level slicing,Bit-plane slicing,对比度拉伸三种方法

MFC 实现图像增强–分段式变换

本文使用visual Studio MFC 平台实现图像增强中的第三大类分段式变换中的三种方法,包括Gray-level slicing,Bit-plane slicing,对比度拉伸.
关于其他MFC单文档工程可参考
01-Visual Studio 使用MFC 单文档工程绘制单一颜色直线和绘制渐变颜色的直线

02-visual Studio MFC 绘制单一颜色三角形、渐变颜色边框三角形、渐变填充三角形、边框渐变的正方形与填充渐变的正方形实例
03-visual Studio MFC 平台实现对灰度图添加椒盐噪声,并进行均值滤波与中值滤波

一、 Gray-level slicing 灰度级别切片

1.1 灰度级别切片的原理

灰度级别切片(Gray-level Slicing)是一种图像处理技术,其目标是增强或突出显示特定灰度范围内的像素值。这通常用于突出感兴趣的目标或特定区域,并抑制其他区域的细节。

以下是灰度级别切片的基本原理:

  1. 选择灰度范围: 定义一个或多个灰度范围,这些范围内的像素将被保留或增强,而其他像素将被抑制。

  2. 设置增强值: 对于选择的灰度范围内的像素,可以将它们的灰度值增强为更高的亮度值,以使其在最终图像中更为突出。

  3. 抑制其他灰度范围: 对于未选择的灰度范围,可以将它们的灰度值设置为较低的亮度值,从而降低它们在最终图像中的显著性。

  4. 生成增强后的图像: 根据上述操作生成最终的图像,其中特定灰度范围内的像素得到了增强,而其他像素则被抑制。

这种方法的应用场景包括:

  • 目标突出显示: 当你想突出显示图像中的某个特定区域或目标时,可以使用灰度级别切片来使该区域更为显著。

  • 降低背景噪声: 如果图像中存在噪声或其他不相关的信息,可以通过灰度级别切片来降低这些不相关信息的影响。

1.2 灰度级别切片的代码实现

void CMFCApplication1View::OnGrayLevelSlicing()
{
    if (gray_data != nullptr) {
        unsigned char* enhanced_data = new unsigned char[bmpWidth * bmpHeight];
        int lower_threshold = 100;  // 定义下限阈值
        int upper_threshold = 200;  // 定义上限阈值

        for (int i = 0; i < bmpWidth * bmpHeight; ++i) {
            if (gray_data[i] >= lower_threshold && gray_data[i] <= upper_threshold) {
                // 在选择的灰度范围内增强像素值
                enhanced_data[i] = 255;
            } else {
                // 在其他范围内抑制像素值
                enhanced_data[i] = 0;
            }
        }

        // 获取绘图设备
        CClientDC dc(this);
        CDC* pDC = &dc;

        // 绘制增强后的灰度图
        m_pBmp->drawGrayBmp(pDC, enhanced_data, bmpWidth, bmpHeight, offset_left + 1100, offset_top);

        // 在图片下方添加文字
        GdiplusStartupInput gdiplusStartupInput;
        ULONG_PTR gdiplusToken;
        GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, nullptr);
        {
            Graphics graphics(pDC->m_hDC);
            Gdiplus::Font font(L"Arial", 12);
            SolidBrush brush(Color(255, 128, 0, 128));  // 文字颜色为紫色

            // 文字的位置
            PointF point(offset_left + 1100, offset_top + bmpHeight + 10);

            // 绘制文字
            graphics.DrawString(L"灰度级别切片", -1, &font, point, &brush);
        }
        GdiplusShutdown(gdiplusToken);

        // 释放内存
        delete[] enhanced_data;
    } else {
        // 处理图像未加载的情况
        AfxMessageBox(_T("未加载图片"));
    }
}

上述代码将在图像中选择一个灰度范围进行增强,并在最终图像中突出显示该范围。

1.3 实现效果

在这里插入图片描述

二、 Bit-plane slicing

2.1 Bit-plane slicing 原理

Bit-plane slicing 是一种图像处理技术,它将图像的每个像素的二进制表示按位切割,然后将每个位平面(bit plane)单独显示。这种技术可以用于分析和可视化图像的信息,从而更好地理解图像的结构和特征。

以下是 Bit-plane slicing 的基本原理:

  1. 二进制表示: 对于每个像素,将其灰度值转换为二进制表示。例如,8 位灰度图像的像素值在二进制中有 8 位。

  2. 按位切割: 对每个像素的二进制表示,将其按位进行切割。例如,一个像素的 8 位二进制表示可以分为 8 个位平面,每个平面代表一个二进制位。

  3. 单独显示: 将切割得到的每个位平面单独显示。这意味着显示第一位平面、第二位平面,以此类推。

  4. 可视化: 通过观察每个位平面的图像,可以更好地理解图像的结构和像素之间的关系。低位平面通常包含图像的全局信息,而高位平面包含更多的细节信息。

  5. 合成: 可以选择将其中一些位平面重新合成为新的图像,以突出显示某些特定特征或信息。

Bit-plane slicing 主要用于图像分析、图像压缩和图像增强。在分析中,它可以帮助识别图像中的模式和结构。在压缩中,某些位平面可以舍弃以减小图像的大小。在增强中,选择特定位平面可以突出显示某些特征。

2.2 Bit-plane slicing代码实现

void CMFCApplication1View::OnBitplaneSlicing()
{
    if (gray_data != nullptr) {
        // 获取绘图设备
        CClientDC dc(this);
        CDC* pDC = &dc;

        // 选择要显示的位平面,这里选择第 3 位平面(从右往左数)
        int bit_plane = 3;

        // 创建临时数组用于存储位平面数据
        unsigned char* bit_plane_data = new unsigned char[bmpWidth * bmpHeight];

        // 提取位平面数据
        for (int i = 0; i < bmpWidth * bmpHeight; ++i) {
            bit_plane_data[i] = (gray_data[i] >> bit_plane) & 1;
        }

        // 缩放位平面数据以便显示
        int scale_factor = 255;
        for (int i = 0; i < bmpWidth * bmpHeight; ++i) {
            bit_plane_data[i] *= scale_factor;
        }

        // 绘制位平面图像
        m_pBmp->drawGrayBmp(pDC, bit_plane_data, bmpWidth, bmpHeight, offset_left + 1300, offset_top);

        // 在图片下方添加文字
        GdiplusStartupInput gdiplusStartupInput;
        ULONG_PTR gdiplusToken;
        GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, nullptr);
        {
            Graphics graphics(pDC->m_hDC);
            Gdiplus::Font font(L"Arial", 12);
            SolidBrush brush(Color(255, 128, 0, 128));  // 文字颜色为紫色

            // 文字的位置
            PointF point(offset_left + 1300, offset_top + bmpHeight + 10);

            // 绘制文字
            CString text;
            text.Format(L"Bit-plane Slicing (Bit %d)", bit_plane);
            graphics.DrawString(text, -1, &font, point, &brush);
        }
        GdiplusShutdown(gdiplusToken);

        // 释放内存
        delete[] bit_plane_data;
    } else {
        // 处理图像未加载的情况
        AfxMessageBox(_T("未加载图片"));
    }
}

上述代码演示了如何选择特定位平面进行切割并显示。在示例中,选择了第 3 位平面,并在图像下方显示了相应的文字。请注意,位平面的选择从右向左进行,从 0 开始。

2.3 实现效果

 

三、 对比度拉伸

3.1 对比度拉伸的原理

图像的对比度拉伸是一种调整图像对比度的方法,其原理涉及到像素值的映射。对比度表示图像中不同亮度级别之间的差异程度。拉伸对比度的目的是增强图像中的亮度差异,使图像中的细节更加明显。

对比度拉伸的一般原理如下:

  1. 找到图像的最小和最大像素值: 遍历整个图像,找到最小和最大的像素值。

  2. 定义拉伸函数: 根据找到的最小和最大像素值,定义一个拉伸函数,将原始像素值映射到一个新的范围内。这个映射通常使用线性函数完成,将原始像素值拉伸到一个更广泛的范围。

    公式示例: 新像素值 = ( 原始像素值 − 最小值 最大值 − 最小值 ) × 新范围大小 \text{新像素值} = \left( \frac{\text{原始像素值} - \text{最小值}}{\text{最大值} - \text{最小值}} \right) \times \text{新范围大小} 新像素值=(最大值最小值原始像素值最小值)×新范围大小

  3. 应用拉伸函数: 将拉伸函数应用于整个图像,调整每个像素的值。这样,原始图像中的亮度差异将在新的范围内更为明显。

对比度拉伸常用于增强图像中的细节,特别是在图像中存在大量像素集中在低对比度范围内的情况。拉伸过程不改变图像的相对亮度顺序,只是将亮度范围映射到更广泛的范围,以便更好地显示图像的细节。

3.2 对比度拉伸代码实现

//对比度拉伸代码
void CMFCApplication1View::OnContraststretching()
{
 
 CClientDC dc(this);
 CDC* pDC = &dc;
 if (gray_data != nullptr) {
  // 寻找图像的最小和最大像素值
  unsigned int minPixelValue = gray_data[0];
  unsigned int maxPixelValue = gray_data[0];
  // 创建临时数组用于对比度拉伸处理
  unsigned char* temp_data = new unsigned char[bmpWidth * bmpHeight];
  for (int i = 0; i < bmpWidth * bmpHeight; ++i) {
   if (gray_data[i] < minPixelValue) {
    minPixelValue = gray_data[i];
   }

   if (gray_data[i] > maxPixelValue) {
    maxPixelValue = gray_data[i];
   }
  }

  // 对比度拉伸的参数
  const int newMinPixelValue = 0;
  const int newMaxPixelValue = 255;

  // 应用对比度拉伸公式到每个像素
  for (int i = 0; i < bmpWidth * bmpHeight; ++i) {
   temp_data[i] = static_cast<unsigned char>(
    (gray_data[i] - minPixelValue) * (newMaxPixelValue - newMinPixelValue) /
    (maxPixelValue - minPixelValue) + newMinPixelValue);
  }

  // 绘制拉伸后的灰度图
  m_pBmp->drawGrayBmp(pDC, temp_data, bmpWidth, bmpHeight, offset_left + 450, offset_top);
  // 释放临时数组内存
  delete[] temp_data;
  // 更新视图,显示修改后的图像
  //Invalidate();
  // 在图片下方添加文字---只为方便查看
  GdiplusStartupInput gdiplusStartupInput;
  ULONG_PTR gdiplusToken;
  GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, nullptr);
  {
  Graphics graphics(pDC->m_hDC);
  Gdiplus::Font font(L"Arial", 12);
  SolidBrush brush(Color(255,128, 0, 128));  // 文字颜色为紫色

  // 文字的位置
  PointF point(offset_left + 450, offset_top + bmpHeight + 10);

  // 绘制文字
  graphics.DrawString(L"对比度拉伸后的图", -1, &font, point, &brush);
  }
  GdiplusShutdown(gdiplusToken);

 }
 else {
  // 处理图像未加载的情况
  AfxMessageBox(_T("未加载图片"));
  return;
 }
}

3.3 对比度拉伸的实现图

在这里插入图片描述

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
There are a few issues with this code: 1. The `bit_plane_slicing` function takes in a parameter `src`, but the code inside the function uses `img` instead. This will cause an error as `img` is not defined inside the function. 2. The `cv2.normalize` function is being used incorrectly. It should be `cv2.normalize(src, dst, alpha=0, beta=1.0, norm_type=cv2.NORM_MINMAX)`. 3. The `image1`, `image2`, and `image3` variables are being calculated incorrectly. The correct calculations should be: ``` image1 = img1*(2**7) + img2*(2**6) image2 = img1*(2**7) + img2*(2**6) + img3*(2**5) image3 = img1*(2**7) + img2*(2**6) + img3*(2**5) + img4*(2**4) ``` Here's the corrected code: ``` import cv2 import numpy as np import matplotlib.pyplot as plt img = cv2.imread(r'E:\\postgraduate\\three\\DIP3E_Original_Images_CH03\\3.14.tif',0) def bit_plane_slicing(src,z): height, width = src.shape dst = np.zeros((height, width), np.uint8) cv2.normalize(src, dst, alpha=0, beta=1.0, norm_type=cv2.NORM_MINMAX) for i in range(0, height): for j in range(0, width): pixel = format(src[i,j], '08b') if pixel[8-z] == '0': dst[i, j] = 0 else: dst[i, j] = 255 return dst img1 = bit_plane_slicing(img,8) img2 = bit_plane_slicing(img,7) img3 = bit_plane_slicing(img,6) img4 = bit_plane_slicing(img,5) image1 = img1*(2**7) + img2*(2**6) image2 = img1*(2**7) + img2*(2**6) + img3*(2**5) image3 = img1*(2**7) + img2*(2**6) + img3*(2**5) + img4*(2**4) plt.figure(figsize=(100,100)) plt.subplot(131) plt.imshow(image1,cmap='gray') plt.axis('off') plt.subplot(132) plt.imshow(image2,cmap='gray') plt.axis('off') plt.subplot(133) plt.imshow(image3,cmap='gray') plt.axis('off') plt.show() ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

写的什么石山代码

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值