Histogram Adjustments in MATLAB

Embed Size (px)

Citation preview

  • 8/12/2019 Histogram Adjustments in MATLAB

    1/9

    Histogram Adjustments in MATLAB Part I StretchingNick Loomis July 12, 2013 Image Enhancement,Recent Posts 6 Comments

    Obtaining an image with the right contrast for your application is never easy. Whether it is for art,

    detection, recognition, or improving your own photographs, contrast adjustment is a common

    task. Industrial setups can adjust the lighting and camera parameters before image capturebut

    what about after the image has been captured? Histogram processing is one answer for

    computationally adjusting the contrast, and can also be applied to a number of problems

    involving color balance or tone transfer.

    This three-part post focuses on different methods for post-processing an image to modify itsunderlying histogram, affecting contrast and color balance in some amazing ways. The first part

    introduces basic pixel operations and how they affect histograms, and then shows several

    schemes for how to stretch a histogram to cover the full range of available pixel values.The

    second partdiscusses how to spread out the pixel values evenly over some range, a process

    known as histogram equalization.The third partextends the idea of controllably modifying the

    pixel values so that the resulting histogram matches any arbitrary distribution.

    To begin, well look at the basics of working with image histograms using MATLAB and how to

    perform histogram stretching to modify contrast and color.

    Histogram math

    A histogram is the probability distribution of pixel values in an image. (For RGB images, the

    histogram is usually broken into three histograms of the three component channels.) Like any

    other distribution, histograms have simple mathematical rules. Two operations that affect the

    pixel values, and thus the histograms, will be used extensively through these posts:

    1) Adding a value to all the pixels adds that amount to the histogram; visually, this shifts the

    histogram

    2) Multiplying all the pixel values by a certain amount scales where the histogram data appears;

    visually, this stretches the histogram

    http://imageprocessingblog.com/author/nick-loomis/http://imageprocessingblog.com/category/image-enhancement/http://imageprocessingblog.com/category/recent-posts/http://imageprocessingblog.com/histogram-adjustments-in-matlab-part-i/#commentshttp://imageprocessingblog.com/histogram-adjustments-in-matlab-part-ii-equalization/http://imageprocessingblog.com/histogram-adjustments-in-matlab-part-ii-equalization/http://imageprocessingblog.com/histogram-adjustments-in-matlab-part-ii-equalization/http://imageprocessingblog.com/histogram-adjustments-in-matlab-part-ii-equalization/http://imageprocessingblog.com/histogram-adjustments-in-matlab-part-iii-matching/http://imageprocessingblog.com/histogram-adjustments-in-matlab-part-iii-matching/http://imageprocessingblog.com/histogram-adjustments-in-matlab-part-iii-matching/http://imageprocessingblog.com/histogram-adjustments-in-matlab-part-ii-equalization/http://imageprocessingblog.com/histogram-adjustments-in-matlab-part-ii-equalization/http://imageprocessingblog.com/histogram-adjustments-in-matlab-part-i/#commentshttp://imageprocessingblog.com/category/recent-posts/http://imageprocessingblog.com/category/image-enhancement/http://imageprocessingblog.com/author/nick-loomis/
  • 8/12/2019 Histogram Adjustments in MATLAB

    2/9

    To plot the histogram of an image, there are several options. If you have the Image Processing

    Toolbox, the imhistfunction is the easiest to use, but only works on single color channels. For

    example, to plot the green channel of an image,

    img = imread('Flowers.jpg');

    imhist(img(:,:,2))

    If you dont have the Toolbox or want to have greater control over the histogram data, you can

    compute the values directly using the hist function and plot using your desired methods:

    http://imageprocessingblog.com/wp-content/uploads/2013/07/flower-green-hist.jpghttp://imageprocessingblog.com/wp-content/uploads/2013/07/histogram-math-copy.jpghttp://imageprocessingblog.com/wp-content/uploads/2013/07/flower-green-hist.jpghttp://imageprocessingblog.com/wp-content/uploads/2013/07/histogram-math-copy.jpg
  • 8/12/2019 Histogram Adjustments in MATLAB

    3/9

    img = imread('Flowers.jpg');

    g = img(:,:,2);

    bins = 0:1:255;

    H = hist(g(:), bins);plot(bins, H, 'linewidth',2, 'color', 'g');

    This second example assumes that the image uses eight bits per color channel, or a maximum

    value of 2^8-1 = 255. The remainder of the examples in these posts will use the assumption that

    pixel values range from 0 to 255.

    Histogram stretching

    Contrastis a measure of how much the pixel brightness changes relative to the average

    brightness. A technique known as histogram stretchingcan be used to shift the pixel values to fill

    the entire brightness range, resulting in high contrast.

    The first step is to find the pixel values that should get mapped to 0% and 100% brightness. Any

    real-world image has noise, however. To keep the noise from unduly influencing the stretching,

    an assumption is made: a small percentage of the brightest and darkest pixels are ignored, writing

    them off to sensor noise. If you have the Image Processing Toolbox (well cover what you can do

    if you dont have the toolbox in a moment), you can use

    https://en.wikipedia.org/wiki/Contrast_(vision)https://en.wikipedia.org/wiki/Contrast_(vision)http://imageprocessingblog.com/wp-content/uploads/2013/07/flower-green-hist-direct.jpghttps://en.wikipedia.org/wiki/Contrast_(vision)
  • 8/12/2019 Histogram Adjustments in MATLAB

    4/9

  • 8/12/2019 Histogram Adjustments in MATLAB

    5/9

    There are several assumptions here. The first is that the image, img, is assumed to have pixels

    which could range in value from 0 to 255. The second is that the histogram, H, could have bins

    with 0 counts. Theproblem is that, in the next step, well be doing an inverse look-up, which

    means that we need an invertible function. If H has zeros, then cumsum(H) will have at least two

    identical values and will not be invertible. Setting the zero-count values in H

    to eps(sum(H))ensures that H does not have non-zero values but only has a trivial effect on the

    CDF. Using the CDF for the inverse look-up can be as simple as this:

    h_low = interp1(cdf, [0,bins], pct);

    h_high = interp1(cdf, [0,bins], 1-pct);

    where pct is the percent of pixels to ignore. This will give similar limits as stretchlim.

    Figure: Interpolating the CDF at pct = 0.05 to find the corresponding h_low and h_high pixelvalues.

    http://imageprocessingblog.com/wp-content/uploads/2013/07/histogram-cdf-of-treeArt-interp.jpghttp://imageprocessingblog.com/wp-content/uploads/2013/07/histogram-cdf-of-treeArt-v2.jpghttp://imageprocessingblog.com/wp-content/uploads/2013/07/histogram-cdf-of-treeArt-interp.jpghttp://imageprocessingblog.com/wp-content/uploads/2013/07/histogram-cdf-of-treeArt-v2.jpg
  • 8/12/2019 Histogram Adjustments in MATLAB

    6/9

    The next step is to adjust the image values so that the h_low and h_high values are remapped to

    the 0% and 100% brightness levels. Using simple histogram math,

    img_adjusted2 = uint8((double(img)h_low)/(h_high-h_low)* 255);

    Note that this includes both a shift, double(img)h_low, and a stretch, 1/(h_high-h_low) of the

    original histogram.

    In general, histogram stretching tends to work well on images that have a poor image contrast to

    start with and which should have full contrast. You can limit the amount of stretching that occurs

    by not mapping h_low and h_high to the 0% and 100% brightness levels, but instead by only

    scaling them a certain amount.

    Multi-channel Histogram Stretching

    The histogram stretching that weve looked at so far has focused on single-channel images.

    Whats useful is that the same functions can be applied tomulti-channel images, like RGB

    images, by applying the codes to each channel independently.

    MATLABsstretchlimand imadjustfunctions are both written to be able to take in RGB images

    in addition to single-channel images using the exact same code as we used earlier.

    Again, if you dont have the Image Processing Toolbox or want to use multi-channel images that

    dont have three channels (for example, some microscopy or satellite imagery), youll need to

    take a few extra steps. For example, say you have a multi-channel image, img, and you plan to

    adjust each channel. Then you could consider using something like this:

    img_adjusted = zeros(size(img), 'uint8');

    forch = 1:size(img,3)

    img_channel = img(:,:,ch);

    limits = stretchlim(img_channel, 0.01);

    img_adjusted(:,:,ch)= imadjust(img_channel, limits, []);

    end

    Variations on Histogram Stretching

    The RGB color space is the de-facto standard for images. It doesnt always result in the best

    processing, however, especially when the image is intended for human viewing. A color space

    that better represents the human visual system, likeL*a*b* orLuvcan provide more natural

    stretching in some cases. In both of these color spaces, the L channel represents the brightness,while the (a*, b*) or (u, v) channels represent the color. (This is conceptually similar toYCbCr,

    https://en.wikipedia.org/wiki/Lab_color_spacehttps://en.wikipedia.org/wiki/Lab_color_spacehttps://en.wikipedia.org/wiki/CIE_Luv_color_spacehttps://en.wikipedia.org/wiki/CIE_Luv_color_spacehttps://en.wikipedia.org/wiki/CIE_Luv_color_spacehttps://en.wikipedia.org/wiki/YCbCrhttps://en.wikipedia.org/wiki/YCbCrhttps://en.wikipedia.org/wiki/YCbCrhttps://en.wikipedia.org/wiki/YCbCrhttps://en.wikipedia.org/wiki/CIE_Luv_color_spacehttps://en.wikipedia.org/wiki/Lab_color_space
  • 8/12/2019 Histogram Adjustments in MATLAB

    7/9

    used in JPEG encoding.) The stretching is performed on the L channel after converting the colors.

    Using the functions in the Image Processing Toolbox,

    img = imread('lion.png');

    c_rgb2lab = makecform('srgb2lab');labimg = applycform(img, c_rgb2lab);

    labimg(:,:,1)= imadjust(labimg(:,:,1), ...

    stretchlim(labimg(:,:,1), 0.01));

    c_lab2rgb = makecform('lab2srgb');

    img_adjusted = applycform(labimg, c_lab2rgb);

    Figure: Histogram stretching within a L*a*b* color space and in an RGB color space. Note that

    the RGB stretching changes the colors slightly, emphasizing reds and yellows in this particular

    case.

    Since only the L channel was adjusted, the colors remain the same, but the brightness is re-

    mapped. This adjusts the contrast in a way that sometimes can be more visually pleasing without

    significantly affecting the color balance.

    If you dont have the Image Processing Toolbox, Mark Ruzon wrote simple functions to convert

    between RGB and L*a*b*:Lab2RGB andRGB2Lab.

    http://www.mathworks.com/matlabcentral/fileexchange/24010http://www.mathworks.com/matlabcentral/fileexchange/24010http://www.mathworks.com/matlabcentral/fileexchange/24010http://www.mathworks.com/matlabcentral/fileexchange/24009http://www.mathworks.com/matlabcentral/fileexchange/24009http://www.mathworks.com/matlabcentral/fileexchange/24009http://imageprocessingblog.com/wp-content/uploads/2013/07/hist-stretching-w-lab-lion.jpghttp://www.mathworks.com/matlabcentral/fileexchange/24009http://www.mathworks.com/matlabcentral/fileexchange/24010
  • 8/12/2019 Histogram Adjustments in MATLAB

    8/9

    Another variation is to apply histogram stretching in the hue-saturation-value (HSV)color space.

    The histogram stretching is only applied to the S and V channels, but not the hue channel. This

    tends to result in reasonable contrast and fuller colors without significantly affecting the color

    balance.

    img = imread('bopFlower.png');

    hsvimg = rgb2hsv(img);

    forch=2:3

    hsvimg(:,:,ch)= imadjust(hsvimg(:,:,ch), ...

    stretchlim(hsvimg(:,:,ch), 0.01));

    end

    img_adjusted = hsv2rgb(hsvimg);

    Stretching on HSV is also sometimes done after stretching in the RGB color space to ensure full

    color saturation. One example is this underwater image, which required significant stretching in

    RGB, but then benefits from an additional HSV stretch to gain full contrast:

    https://en.wikipedia.org/wiki/HSL_and_HSVhttps://en.wikipedia.org/wiki/HSL_and_HSVhttps://en.wikipedia.org/wiki/HSL_and_HSVhttp://imageprocessingblog.com/wp-content/uploads/2013/07/bopFlower-hsv-stretched.jpghttps://en.wikipedia.org/wiki/HSL_and_HSV
  • 8/12/2019 Histogram Adjustments in MATLAB

    9/9

    This is a good point to try out histogram stretching on your own images and get familiar with thedifferent color transforms. In thenext post,well explore histogram equalization, primarily used

    for modifying the contrast in an image, and some of its extensions.

    Nick Loomis writes forloomsci.wordpress.comand uses the methods from this series ofposts for various projects: histogram stretching tocorrect the colors of underwater

    photosand histogram matching toreplicate the effect of color filters.

    http://imageprocessingblog.com/histogram-adjustments-in-matlab-part-ii-equalization/http://imageprocessingblog.com/histogram-adjustments-in-matlab-part-ii-equalization/http://imageprocessingblog.com/histogram-adjustments-in-matlab-part-ii-equalization/http://loomsci.wordpress.com/http://loomsci.wordpress.com/http://loomsci.wordpress.com/http://loomsci.wordpress.com/2013/06/20/automatic-color-correction-underwater-photos/http://loomsci.wordpress.com/2013/06/20/automatic-color-correction-underwater-photos/http://loomsci.wordpress.com/2013/06/20/automatic-color-correction-underwater-photos/http://loomsci.wordpress.com/2013/06/20/automatic-color-correction-underwater-photos/http://loomsci.wordpress.com/2013/07/11/learning-instagrams-filters/http://loomsci.wordpress.com/2013/07/11/learning-instagrams-filters/http://loomsci.wordpress.com/2013/07/11/learning-instagrams-filters/http://imageprocessingblog.com/wp-content/uploads/2013/07/ocean-RGB-and-HSV.jpghttp://loomsci.wordpress.com/2013/07/11/learning-instagrams-filters/http://loomsci.wordpress.com/2013/06/20/automatic-color-correction-underwater-photos/http://loomsci.wordpress.com/2013/06/20/automatic-color-correction-underwater-photos/http://loomsci.wordpress.com/http://imageprocessingblog.com/histogram-adjustments-in-matlab-part-ii-equalization/