Plotting image histograms with Julia

So, I have spent the morning working on some histogram plotting routines in Julia – useful if you want to visualize an image histogram, or save it for later use in file format. Now Julia does not come with any native plotting libraries, but there are the likes of Gadfly, and PyPlot. For todays activity I chose PyPlot, which effectively uses the Julia PyCall package to call Python’s matplotlib directly from Julia (it’s not all a bed of roses though – you do have to make some syntax changes).

Firstly I wanted a function to derive a simple histogram, in the form of a curve. The function plotHist()  takes an image (grayscale) histogram vector as input, and plots the visual histogram to file: histogram.png. This function also has the alternative of plotting to a console window.

function plotHist(hst, show=false)

    PyPlot.xlim([0, 256])
    if (show)

Here is an example of the output produced:

Next, a function plotRGBhist()  to plot all three components of a colour RGB image in one histogram. This function takes a colour image as input, splits off each of the components, and finds the individual histograms (using getIMGhist() ), and then plots them by means of overlaying them, saving the resulting picture of the histogram in histogramRGB.png.

function plotRGBhist(img)

    # Convert image to vector
    dx, dy, dz = size(img)
    imgR = img[:,:,1]
    imgG = img[:,:,2]
    imgB = img[:,:,3]
    hstR = getIMGhist(imgR)
    hstG = getIMGhist(imgG)
    hstB = getIMGhist(imgB)

    PyPlot.plot(hstR, color="red", linewidth=2.0, label="Red")
    PyPlot.plot(hstG, color="green", linewidth=2.0, label="Green")
    PyPlot.plot(hstB, color="blue", linewidth=2.0, label="Blue")
    PyPlot.xlim([0, 256])

Here is a sample output:

Lastly, a function which takes an alternative approach to plotting the histogram of a grayscale image. In the function plotIMGhist(), a grayscale image is input, and the resulting histogram is in the form of a solid histogram, using vertical bars.

function plotIMGhist(img)

    # Convert image to vector
    dx, dy = size(img)
    imgV = vec(reshape(img,1,dx*dy))
    common_params = Dict( :bins => 256, :range => (0, 255), 
                          :normed => false, :color => "black") 
    PyPlot.plt[:hist](imgV; common_params... );

The image is first converted to a vector, and then applied using the matplotlib function hist(). Note that due to conflicts with Julia’s built-in function hist(), in order to use matplotlib.pyplot.hist, one has to use PyPlot.plt[:hist]. Also notice the use of the Dict expression for the parameters to hist. 

Here is a sample:



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s