Image enhancement algorithm based on histogram (HE, CLAHE, Retinex) (II.)

Source: Internet
Author: User

as the second article in the series of image enhancement algorithms, we will introduce the powerful, versatile, far-reaching contrast-limited adaptive histogram equalization (Clahe,contrast Limited Adaptive histogram Equalization) algorithm. Although it was initially presented as an image enhancement algorithm, it is now used in image fog, low illumination image enhancement, water effect adjustment, and digital photo improvement. The algorithmic principle of this algorithm seems simple, but it is not so easy to implement. We will explain it in conjunction with the corresponding MATLAB code. I hope you have a better understanding of the simple histogram equalization algorithm before reading this article, which can be found in the previous article in this series: histogram-based image enhancement algorithm (HE, CLAHE, Retinex) (a)(http:// blog.csdn.net/baimafujinji/article/details/50614332).


First look at the image effects to be processed:



The following are the two effects that are processed with the Clahe algorithm (we'll describe the strategy we're using later):



A B

For an image, the contrast of different regions may vary greatly. There may be some places that are bright, and in some places they are dim. It is obviously not the best option to use a single histogram to adjust it. Therefore, based on the idea of block processing, the Adaptive Histogram Equalization Algorithm (AHE) is proposed. Wikipedia says it's more clear: AHE improves on the transforming each pixel with a transformation function derived from a neighbourhood R Egion. But this method sometimes amplifies some noise, which is something we don't want to see. Then Professor Zuiderveld of Utrecht University in Holland introduced Clahe, using a contrast threshold to remove the effects of noise. In particular, in order to increase the computational speed and eliminate the block edge transition imbalance caused by block processing, he suggested bilinear interpolation method. About the introduction and description of the algorithm, the following two resources have been more clearly explained.

[1] Https://en.wikipedia.org/wiki/Adaptive_histogram_equalization#Contrast_Limited_AHE

[2] K. Zuiderveld: contrast Limited Adaptive histogram equalization. In:p. Heckbert: Graphics Gems IV, Academic Press 1994 (http://www.docin.com/p-119164091.html)

In fact, despite the principle of the algorithm, it still has a lot of obstacles in its implementation. But before this, I also need to explain that MATLAB has integrated the implementation of Clahe function Adapthisteq (), if you just need a result, actually directly use this function is the best choice. I'll give you some sample code to generate the effect shown earlier. The function Adapthisteq () can only be used to work with grayscale graphs, and if you want to work with color images, you need to combine your own code to complete them. The previous article introduced two main strategies for histogram equalization of color images: one is to process R, G and b three channels, and another is to convert to another color space for processing, such as HSV (only the V-channel is processed after the conversion).

First, we give sample code that uses the adapthisteq () function for the R, G, and b three channels, respectively :

img = imread (' space.jpg '); rimg = IMG (:,:, 1); gimg = IMG (:,:, 2); bimg = IMG (:,:, 3); resultr = Adapthisteq (rimg); RESULTG = Adap Thisteq (gimg); RESULTB = Adapthisteq (bimg); result = Cat (3, resultr, RESULTG, RESULTB); Imshow (result);
Shown in result a of the above procedure.

The following program transforms the color space of the original image into lab space and processes the L channel.

clear;img = Imread (' space.jpg '); Cform2lab = Makecform (' Srgb2lab '); LAB = Applycform (img, Cform2lab); L = LAB (:,:, 1); LAB (:,:, 1) = Adapthisteq (L); Cform2srgb = Makecform (' Lab2srgb '); j = Applycform (LAB, Cform2srgb); Imshow (j);

The result of the above procedure is shown in B.


If you want to further promote and promote this algorithm, use for Image de-fog, low-light image improvement and water-like processing, then it is clearly not enough to know, you must know why. I hope that the following step-by-step implementation of the code can help you solve this confusion. Given the detailed description of the algorithm presented in the previous document, this section will not be repeated below, instead using MATLAB code to demonstrate some of the details.


first of all, we begin our discussion from the Clahe processing of grayscale graphs. Clean up the MATLAB environment for this . then, read a picture (and convert it to grayscale), get the picture's length, width, pixel grayscale maximum, minimum value and other information.

Clc;clear all;img = Rgb2gray (Imread (' space.jpg ')); [H,w] = Size (img); MINV = double (min (img)); MAXV = double (Max (IMG)); Imshow (IMG);

The initial state of the image is shown below. Also the Height of the graph = 395,width = 590, the grayscale maximum is 255, and the minimum value is 8.


We want to divide the original image horizontally into 8 parts, dividing the vertical direction into 4 parts, i.e. the original will be divided into 4x8 = 32 subimage. You can then calculate the height of each block (tile) = 99,width = 74. Note that since the length and width of the original image is not likely to be divisible, so my approach here is to create a slightly larger picture, and its width and length are deltax and deltay to ensure that both long and wide can be divisible.

NrX = 8; Nry = 4; Hsize = Ceil (h/nry); Wsize = Ceil (W/NRX);d Eltay = Nry*hsize-h;deltax = nrx*wsize-w;tmpimg = Zeros (h+deltay,w+deltax); tmpImg (1:h,1:w) = IMG;

After the length and width are filled, some of the necessary information for the new image is updated.


New_w = w + deltax;new_h = h + deltay; Nrpixels = wsize * wsize;

then specify the number of values on the histogram horizontal axis of the image (also specifying the accuracy of the interval or count of the horizontal values on the statistical histogram), we generally require this value to be greater than 128 for images with richer colors.

% Nrbins-number of greybins for histogram ("dynamic range") Nrbins = 256;


Then a look-up table is remapped with the grayscale range of the original image (you can also use the 0~255 range directly, depending on how you follow up the histogram), and build a histogram for each block (tile) based on this.

Lut = zeros (maxv+1,1); for I=MINV:MAXV    LUT (i+1) = fix (I-MINV);%i+1endbin = Zeros (New_h, new_w); for m = 1:new_h    For n = 1:new_w        Bin (m,n) = 1 + LUT (tmpimg (m,n) + 1);    Endendhist = Zeros (Nry, NrX, n); for i=1:nry for    j=1:nrx        tmp = uint8 (Bin (1+ (i-1) *hsize:i*hsize, 1+ (j-1) *wsize: j*wsize));        %tmp = Tmpimg (1+ (i-1) *hsize:i*hsize,1+ (j-1) *wsize:j*wsize);        [Hist (I, J,:), x] = imhist (TMP, n);    Endendhist = Circshift (hist,[0, 0,-1]);

Note: As a general rule, the histogram (set) that we should build in this step should be a 4x8=32 with a vector length of 256 (you can of course do the same). But because of some subsequent processing, I have generated a 4x8 matrix of length 256. The matrix of Index = 1 is actually a count of the number of pixels =0 the grayscale value on each tile of the entire image. For example, we get the Hist (:,:, 18) as follows. This indicates that there are 0 pixels of gray value =17 in the tile at the top left corner of the image. In the same vein, a tile on its right has 46 pixels with a grayscale value of =17 .


Hist (:,:,) =     0   218     7     0     0   114     0     1     0     0     2   124     (0 0 0)     0     0     1)     9     2

The histogram is then cropped. The value range of the Cliplimit parameter in the function Adapthisteq () built into Matlab is 0~1. The method we write here requires the value to be >1. Of course, this is entirely up to your algorithm implementation strategy, they do not differ in nature. Then we will get a new (cropped) mapping histogram.

Cliplimit = 2.5; Cliplimit = Max (1,cliplimit * hsize * wsize/nrbins); Hist = Cliphistogram (HIST,NRBINS,CLIPLIMIT,NRY,NRX); Map=maphistogram (Hist, MINV, MAXV, Nrbins, Nrpixels, Nry, NrX);

Finally, and the most critical step, we need to interpolate the result process. This is also the most complex part of the Zuiderveld design algorithm.

YI = 1;for i = 1:nry+1 if i = = 1 Suby = floor (HSIZE/2);        YU = 1;    YB = 1;        ElseIf i = = nry+1 Suby = Floor (HSIZE/2);        YU = Nry;    YB = Nry;        else Suby = hsize;        YU = i-1;    YB = i;    End xI = 1;            For j = 1:nrx+1 if J = = 1 SUBX = floor (WSIZE/2);            XL = 1;        XR = 1;            ElseIf j = = nrx+1 SUBX = Floor (WSIZE/2);            XL = NrX;        XR = NrX;            else SUBX = wsize;            XL = j-1;        XR = j;        End UL = Map (YU,XL,:);        UR = Map (YU,XR,:);        BL = Map (YB,XL,:);        BR = Map (YB,XR,:);        Subimage = Bin (yi:yi+suby-1,xi:xi+subx-1);        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% simage = zeros (Size (subimage));        num = Suby * SUBX;            For i = 0:suby-1 Inversei = suby-i;                For j = 0:subx-1 Inversej = subx-j; val = Subimage (I+1,J+1); Simage (i+1, j+1) = (inversei* (Inversej*ul (val) + J*ur (val)) ... + i* (INVERSEJ*BL (val) + j*b            R (val)))/num; End end%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% output (yi:yi+suby-1, Xi:xi        +subx-1) = Simage;    Xi = xi + subx; End yi = yi + suby;end

This place, the author of the original text has been more clear, I feel I do not need to incompatible, swim. The following section is a description of the author's original text, sufficient to illustrate the problem.


Finally, let's see what we've got to deal with (and, of course, we need to cut out the parts we've filled before).


Output = output (1:h, 1:w); figure, Imshow (output, []);

Let's look at the results. You can compare the previous grayscale image, it is not difficult to find that the image quality has been greatly improved.



=========== never look at the homepage of the blog private messages June said: Usually I know the algorithm as much as possible, but in addition to the necessary auxiliary demo code, I will not provide additional source download, No matter when people should rely on their own, only when you can really write code, the code is yours. There is no ===========, no Zhao Zhao, no Ngan Ngan, no illustrious work. =========== If you are a fellow of image processing, welcome to the image Processing Learning Group (529549320). in order to avoid harassment information such as advertising, the group needs to answer the threshold question (for example: What is the first derivative of the X-squared equals?) There is wood feel so easy, but do not laugh, administrators receive the answer is multifarious! Note that each person has only one chance to try it out.) cheers~


more interesting and useful image processing algorithms can also refer to my the principle and practice of Digital Image Processing (MATLAB edition)



Image enhancement algorithm based on histogram (HE, CLAHE, Retinex) (II.)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.