Miyerkules, Nobyembre 29, 2017

Darkness to Light: Enhancement by Histogram Manipulation

This activity is pretty simple, you have an image with unequal bright and dark tones, and then you try to equalize it to make it look better. I generally enjoyed doing this activity especially since it brings satisfaction from the revelation of what a poorly contrasted image was hiding. 

In the intro part of the manual, Ma'am Jing said that we should not trust our eyes. I say, yes it is true. Colors of images depend not just on its hue but mostly on the camera settings (trinity of colors, remember? 187?) Good for us, humans, our eyes have rods that adjusts contrasts on the environment that we are looking at so we can have a good perception of the reality. Due to the vast improvement of science, most the technology of today can mimic or surpass the way our eyes work. Even some of our hand held phone cameras can already capture information that our eyes cannot see. 

There will always be cases however, that some important data, such as crime scene evidence, extinct animal sightings, underwater explorations, and many others, may sometimes be poorly contrasted due to the intense environment settings (dark room, too bright background, camouflaging objects, moving objects, etc) . With the proper tools, the stored information in those images can be extracted to maximize the value of those seemingly lost data, and reveal things our eyes have never seen before. 

For the activity, the PDF of the image was obtained. Naturally, for a good image, the PDF should somehow look like a horizontal line, indicating equal distribution of all the possible intensities. A too dark image would have peaks at the left part of the PDF, which are the 0 or the dark pixels and the PDF may cluster at the left part. A too light image will have peaks or clusters at the right part, indicating 255 or the white pixels. Some image may have intense contrast such that it has peaks far apart from each other, which are too light and too dark. A poorly contrasted image however, has a PDF clustered at one side. A highly contrasted image has peaks at the opposite end of the graph showing too dark and too light pixels. To enhance the image, the desired PDF should be a horizontal line, spread out along all intensities. This is the expected PDF of a well-contrasted image. The image enhancement by histogram manipulation basically is using a desired CDF (or the cumulative distribution function of the PDF) and back projecting the new grayscale values of the image referencing on the original CDF. So, to equalize the intensities, the desired CDF will be a line with a positive slope. 

I have to apply the histogram manipulation to a poorly contrasted image. It took me a hard time to search for an image in my phone since the images nowadays are of good quality and I've already deleted unnecessary photos. I stumbled upon this picture of Rey I've taken when IPL was going for lunch after a Gen Clean in the lab. The image has a bad contrast since the focus was set on the sky at the back ground, which made the foreground look very dark. You wouldn't know it was Rey in the picture at first, right? I even questioned the reason myself on why I still haven't deleted it since I can't comprehend anything from the original image. After swiping some other pictures taken a few moments after the photo was taken (one will be shown near the end of this blog post), then I realized what this photo was supposed to show. I still can't remember though, why I let this bad photo consume space on my phone. 

Anyway, going straight to the point, I used the original image as shown in figure 1(a) and tried to equalize the histogram of the intensities. The result as shown in figure 1(b) is pretty okay. However it became lighter and looks faded as confirmed by the low contrast shown by the histogram. Also, a big spike at some pixel lighter than the original peak in the histogram was created. Due to the limitation of the back projection done in the methodology, most of the domineering pixels, which are dark pixels, in the original image got transformed to a lighter pixel as shown by the new CDF in figure 1(d) where the values at the start of the line has 0 y-values. This result seems like the image did not get enhanced but was rendered far from the original image. However, a lot of details was revealed which are not observable in the original image.  

Figure 1. An enhanced image originally taken with a focus on the sky. (a) The original colored image. (b) The image enhanced by linearizing the intensities. (c) The distributions of the intensities for the original and enhanced image. (d) The CDF


Since the image in figure 1(a) was captured against the light, where the background is a very bright light source, linearizing the contrast of the image makes the image faded due to the limitation or the discreteness of the CDF felt during the backprojection. The rendering of the image was improved by using the fraction of the original intensities after linearizing the intensities of the image. The result shown in figure 2 (a) shows an improved image. The PDF of the histogram also shows that the peak shifted to the left which is nearer to the original peak in the histogram. Not much change in the number of revealed details was observed compared to the result using the linear CDF.  


Figure 2. (a) The image enhanced by linearizing the intensities and using a fraction of the original intensities to render a better image (b) The histogram of the intensities showing the peak nearer to the peak of the original image. (c) The CDF showing a slight distortion due to the original CDF of the image. 

Discovering the effect of other desired CDFs, I used the sigmoid, exponential and log functions. The sigmoid CDF resulted to an image showing more details than the original. The image however is more faded that the image obtained from the linear CDF. The small tree branches are not well defined with the sigmoid CDF compared to the linear CDF. The exponential CDF in figure 4 rendered a bad image as a lot of the fine details at the back ground were removed. Some details in the foreground however was revealed but the image is so faded. The result using the log CDF is okay since the rendered image is not faded and a lot of details were revealed.

Figure 3. (a) The image enhanced using a sigmoid CDF. (b) The histogram showing the dark colored pixels are removed and the peak shifted to a lighter pixel. (c) The CDF of a sigmoid. 



Figure 4. (a) The image enhanced using a CDF where x = ya  where a is a positive number. (b) The histogram showing increased height of the peaks near light pixels. (c) The CDF resulting from the discretization of the original CDF. 


Figure 5. (a) The image resulting from a desired CDF where x = ya where a is a fraction. (b) The histogram showing the peaks at the dark colored pixels shifted to the right compared to the original histogram. (c) The CDF of the image. 

So far, with the original image, the best enhancement was made using the linear CDF and the log CDF. Yes, it is true that a lot of details not seen in the original was revealed in the enhanced images. However, the enhanced images are only in its intensity. The RGB details of the images were already lost. I was challenged on thinking of a way to be able to render a colored version of the enhanced image. First, I thought of manipulating the CDF of the R, G, and B channels separately and then restacking the separate channels to show the original image. Figure 6 (a) shows the rendered image after separately linearizing the CDF of the channels, and figure 6 (b) shows the result after using a log CDF of the channels. The images rendered is not that good but is actually still better than the original image. A lot of details on the color of the objects in the image which was not very visible in the original image was now revealed such as the blue shirt worn by RM (the person at the left side of Rey), which looks black in the original image in figure 1 (a). In the resulting images, the R, G, and B ratios were not preserved, which explains the faded result since most of the colors became white due to the linearized intensity of the R, G, and B channels. Figure 6 (c) shows the resulting image while preserving the ratios among the color channels with reference white balancing. As can be seen, red noise is visible and the skin color is reddish. I also tried using the hsv channels of the images however I failed to reconstruct the ideal colors of the objects since the image became yellow-greenish all over. After doing a lot of trial and error with using the RGB or the hsv channels and using difference desired CDFs, I accounted the red noise to the lost data due to the poor contrast in the original image.  To compare the result to the expected real world colors, I showed figure 6 (d) which is the image I captured a few seconds after capturing figure 1 (a) but now with the smart phone camera adjusted to focus on the foreground. See how much details was lost by comparing figure 1 (a) and figure 6 (d). It is indeed vital to know how to focus on the important things to be able to show the essential details. Good thing, we have technology to extract the details we lost due to are mistakes. We are still learning anyway.

Figure 6. (a) Image rendered by linearizing the CDF of the R, G, and B channels. (b) The image rendered be separating the R, G, and B channels and using a log CDF for each. (c) Image rendered using a linear CDF for each R, G, and B channels and preserving the ratios among each color channels with reference white balancing. (d) A colored image taken a few moments after the image in figure 1 (a) was taken, but with the proper camera settings to show the details in the foreground.

For this activity, I give myself 10/10 since I was able to enhance the intensity of a poorly contrasted image using different CDFs. I had a lot of fun doing the activity but it took me a long time to figure out how to properly preserve the color after the histogram manipulation. For the image in figure 1 (a) the best image was shown in figure 6 (c) which is not that good. I tried using other images and the results are better.

For the image is shown in figure 7. The left panels shows the original image (figure 7 (a) and (d)), the middle panels shows the images with the contrast enhanced using a linear CDF (figure 7 (b) and (e)), and the right panels shows the images with the histogram of the RGB channels separately manipulated. Figure 7 (a) is an image of my brother with our dog, the image is a bit highly contrasted since it was taken against the light source, making the skin look dark and the window railings at the lower left part unseen. After the manipulation shown in figure 7 (c), the edges of the window railings were revealed and the skin became lighter. The edge of the roof at the background was also revealed. 
Figure 7 (d) is an image of the BS5 CSG boys during SPP. Due to the bad camera settings, a dark void can be seen near Justin’s hair and at the left side of Rey’s head. The general mood of the image is also cold and a bit gloomy. After the manipulation as shown in figure 7 (f), the edges at the stage (near Justin’s hair) were revealed. Details were also shown near Rey’s hair. Due to the manipulation, the surroundings became lighter giving the image a joyful atmosphere.

Figure 7. (a) and (d) The original colored images. (b) The enhanced image of (a) using a linear CDF. (c) The image in (a)  enhanced with the color information preserved. (e) The enhanced image of (d) by linearizing the CDF. (f) The image in (d) enhanced with the color information preserved. 

Reality is to be found in lightness and darkness - Pablo Picasso