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.plot(hst)
    PyPlot.xlabel("Intensities")
    PyPlot.xlim([0, 256])
    PyPlot.yticks([])
    if (show)
        PyPlot.show()
    end
    PyPlot.savefig("histogram.png")
end

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.xlabel("Intensities")
    PyPlot.xlim([0, 256])
    PyPlot.yticks([])
    PyPlot.legend(fontsize=10)
    PyPlot.savefig("histogramRGB.png")
 
end

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.yticks([])
    PyPlot.xlabel("Intensities")
    PyPlot.plt[:hist](imgV; common_params... );
    PyPlot.savefig("histogramIMG.png") 
end

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:

 

 

Advertisements

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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