Histogram Equalization OpenCV offical guild

本文探讨了图像直方图的概念及其在图像处理中的应用,特别是直方图均衡化技术如何通过扩展亮度范围来提升图像对比度。通过代码示例和直观的图像对比,解释了直方图均衡化的工作原理及其在实际应用中的效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

What is an Image Histogram?

  • It is a graphical representation of the intensity distribution of an image.
  • It quantifies the number of pixels for each intensity value considered.
../../../../../_images/Histogram_Equalization_Theory_0.jpg

What is Histogram Equalization?

  • It is a method that improves the contrast in an image, in order to stretch out the intensity range.
  • To make it clearer, from the image above, you can see that the pixels seem clustered around the middle of the available range of intensities. What Histogram Equalization does is to stretch out this range. Take a look at the figure below: The green circles indicate the underpopulated intensities. After applying the equalization, we get an histogram like the figure in the center. The resulting image is shown in the picture at right.
../../../../../_images/Histogram_Equalization_Theory_1.jpg

How does it work?

  • Equalization implies mapping one distribution (the given histogram) to another distribution (a wider and more uniform distribution of intensity values) so the intensity values are spreaded over the whole range.

  • To accomplish the equalization effect, the remapping should be the cumulative distribution function (cdf) (more details, refer to Learning OpenCV). For the histogram H(i), its cumulative distribution H^{'}(i) is:

    H^{'}(i) = \sum_{0 \le j < i} H(j)

    To use this as a remapping function, we have to normalize H^{'}(i) such that the maximum value is 255 ( or the maximum value for the intensity of the image ). From the example above, the cumulative function is:

    ../../../../../_images/Histogram_Equalization_Theory_2.jpg
  • Finally, we use a simple remapping procedure to obtain the intensity values of the equalized image:

    equalized( x, y ) = H^{'}( src(x,y) )

Code

  • What does this program do?

    • Loads an image
    • Convert the original image to grayscale
    • Equalize the Histogram by using the OpenCV function EqualizeHist
    • Display the source and equalized images in a window.
  • Downloadable code:Click here

  • Code at glance:

#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <stdio.h>

using namespace cv;
using namespace std;

/**  @function main */
int main( int argc, char** argv )
{
  Mat src, dst;

  char* source_window = "Source image";
  char* equalized_window = "Equalized Image";

  /// Load image
  src = imread( argv[1], 1 );

  if( !src.data )
    { cout<<"Usage: ./Histogram_Demo <path_to_image>"<<endl;
      return -1;}

  /// Convert to grayscale
  cvtColor( src, src, CV_BGR2GRAY );

  /// Apply Histogram Equalization
  equalizeHist( src, dst );

  /// Display results
  namedWindow( source_window, CV_WINDOW_AUTOSIZE );
  namedWindow( equalized_window, CV_WINDOW_AUTOSIZE );

  imshow( source_window, src );
  imshow( equalized_window, dst );

  /// Wait until user exits the program
  waitKey(0);

  return 0;
}

Explanation

  1. Declare the source and destination images as well as the windows names:

    Mat src, dst;
    
    char* source_window = "Source image";
    char* equalized_window = "Equalized Image";
    
  2. Load the source image:

    src = imread( argv[1], 1 );
    
    if( !src.data )
      { cout<<"Usage: ./Histogram_Demo <path_to_image>"<<endl;
        return -1;}
    
  3. Convert it to grayscale:

    cvtColor( src, src, CV_BGR2GRAY );
    
  4. Apply histogram equalization with the function equalizeHist :

    equalizeHist( src, dst );
    

    As it can be easily seen, the only arguments are the original image and the output (equalized) image.

  5. Display both images (original and equalized) :

    namedWindow( source_window, CV_WINDOW_AUTOSIZE );
    namedWindow( equalized_window, CV_WINDOW_AUTOSIZE );
    
    imshow( source_window, src );
    imshow( equalized_window, dst );
    
  6. Wait until user exists the program

    waitKey(0);
    return 0;
    

Results

  1. To appreciate better the results of equalization, let’s introduce an image with not much contrast, such as:

    ../../../../../_images/Histogram_Equalization_Original_Image.jpg

    which, by the way, has this histogram:

    ../../../../../_images/Histogram_Equalization_Original_Histogram.jpg

    notice that the pixels are clustered around the center of the histogram.

  2. After applying the equalization with our program, we get this result:

    ../../../../../_images/Histogram_Equalization_Equalized_Image.jpg

    this image has certainly more contrast. Check out its new histogram like this:

    ../../../../../_images/Histogram_Equalization_Equalized_Histogram.jpg

    Notice how the number of pixels is more distributed through the intensity range.

Note

Are you wondering how did we draw the Histogram figures shown above? Check out the following tutorial!

identity 身份认证 购VIP最低享 7 折! triangle vip 30元优惠券将在58:6:9后过期 去使用 triangle QT+Poppler+PDFviewer.zip 是一个用于在Windows操作系统下,使用QT5框架结合Poppler库开发PDF阅读器的项目。这个项目的核心是利用Poppler库解析PDF文档,并通过QT5进行用户界面的设计和交互。以下将详细介绍相关知识点: 1. **QT5框架**:QT(Qt)是一个跨平台的应用程序开发框架,支持多种操作系统,如Windows、Linux、macOS等。它提供了丰富的库函数和组件,使得开发者可以方便地构建图形用户界面(GUI)应用程序。QT5是QT的第五个主要版本,引入了许多新特性和改进,如QML(Qt Meta Object Language)用于声明式UI设计,以及更好的性能和API优化。 2. **Poppler库**:Poppler是一个开源的PDF文档处理库,源自Xpdf项目,主要用于PDF文件的解析、渲染和提取文本。Poppler提供了C++接口,使得开发者能够方便地在应用程序中集成PDF阅读和处理功能。它可以读取PDF文件,显示页面,提取文本和元数据,甚至支持对PDF文件进行注释和修改(但本项目可能仅涉及阅读功能)。 3. **PDF viewer的实现**:在本项目中,PDF viewer是基于QT5 GUI组件构建的,它利用Poppler库来加载和解析PDF文档。`mainwindow.cpp`和`mainwindow.h`包含了主窗口类的定义和实现,这是用户与应用程序交互的主要界面。`pdfcanvas.cpp`和`pdfcanvas.h`则可能包含了用于显示PDF页面的自定义画布类,该类使用Poppler库来渲染PDF页面到QT的画布上。 4. **项目构建与编译**:`newtime.pro`是QT项目的配置文件,用于指定项目依赖的库(如Poppler)、源代码文件、编译选项等。`.pro.user`文件则保存了用户的特定编译设置,如编译器路径或调试选项。开发者需要使用QT的qmake工具或直接在IDE如Qt Creator中打开此项目,进行编译和链接,确保所有依赖库都正确安装并链接。 5. **文件操作**:`main.cpp`通常是程序的入口点,负责初始化QT应用环境并运行主循环。在PDF viewer中,可能会在`main.cpp`中实例化主窗口,并调用Poppler库的相关函数来加载PDF文件。 6. **使用流程**:用户可以通过QT界面选择PDF文件,然后通过Poppler库读取文件内容,将页面渲染到QT的控件上。用户可以通过滚动、缩放等操作查看PDF内容。 Poppler库的强大功能使得PDF viewer可以支持多页显示、文本搜索、书签管理等高级特性。 7. **优化与扩展**:为了提升用户体验,开发者可能会对PDF viewer进行各种优化,比如添加平滑滚动、快速查找、页面预加载等功能。此外,还可以考虑支持批注、打印、PDF转换等更复杂的操作,以增强软件的功能性和实用性。 QT+Poppler+PDFviewer.zip项目提供了一个基础的PDF阅读器实现,开发者可以在此基础上进一步定制和扩展,以满足特定的PDF处理需求。
直方图均衡化是一种图像增强的方法,它通过对图像像素值的直方图进行调整,使得图像的对比度增强,细节更加清晰。直方图均衡化在图像处理中起到了非常重要的作用。 直方图均衡化的基本原理是将图像像素值的分布调整为更均匀的分布。首先,计算图像的灰度直方图,即统计每个灰度级的像素个数。然后,根据直方图,计算每个灰度级的累计概率分布。接下来,根据累计概率分布,将原图像的每个像素值映射到新的像素值,使得所得到的图像像素值分布更均匀。 直方图均衡化能够有效地增强图像的对比度,使得图像中的细节更加明显。通过调整图像的像素值分布,直方图均衡化能够增加图像中的亮度差异,使得暗区域变亮、亮区域变暗,从而使得整个图像具有更好的视觉效果。 直方图均衡化的应用非常广泛,可以用于图像增强、图像配准、图像压缩等领域。在图像增强中,直方图均衡化可用于改善照明条件差的图像,提升图像的视觉质量。在图像配准中,直方图均衡化可用于改善不同图像之间的亮度差异,使得它们更容易对齐。在图像压缩中,直方图均衡化可用于减小图像中像素值的动态范围,从而提高压缩效果。 综上所述,直方图均衡化是一种重要的图像处理技术,它能够改善图像的对比度,提升图像的视觉效果。在实际应用中,直方图均衡化有着广泛的应用前景,并且可以与其他图像处理方法相结合,进一步提高图像处理的效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值