Lectures and labs | Notes | Assignments | Further reading | Using Linux | Installing OpenCV
The first laboratory session is intended to get you used to working with OpenCV under Linux by extending the program presented in lectures to work out the histogram of an image as well as calculate its mean. The histogram is a plot with the grey level along the abscissa (horizontal axis) and the number of times that grey level occurs along the ordinate (vertical axis). It is usually presented as a bar-chart, as the following example shows. The image, incidentally, is the mandrill image from the long-standing USC-SIPI image database, dating from the time when getting an image onto a computer was an extraordinarily difficult process.
To help you on your way, here is the program presented in lectures and the equivalent C++ program. Because of limitations of the campus web-server, these will download into files that end in .txt — you need to rename the file after downloading to exclude this extension. The demonstrators will try to speak to everyone during the laboratory session to give you help and feedback on what you are doing.
If you are working in Python, you should work out two lists or numpy arrays: the first should contain the grey levels (the numbers from 0 to 255), and the second the number of occurrences of that grey level. If these are stored in x and y respectively, then the code you will need is:
import pylab pylab.figure () pylab.xlim (0, 255) pylab.grid () pylab.title ("Histogram of " + fn) pylab.xlabel ("grey level") pylab.ylabel ("number of occurrences") pylab.bar (x, y, align="center") pylab.show ()
If you are working in C++, there is no handy graph-drawing library that you can invoke to produce the plot. Instead, save the x and y arrays alluded to above to a file, hist.dat say, with one value of x and y per line. You can then use gnuplot to produce the actual plot:
gnuplot set grid set xrange [0:255] set data style impulses unset key plot "hist.dat"
If you're not familiar with compiling C++ programs under Linux, you need to save the lines near the top of the program between /* and */ to a file called Makefile — note the capitalization, and be careful to leave the indentation of the lines you copy unchanged. You can then enter the command
makeand if you have done it correctly, the program will be compiled for you.
If you manage to get the histogram working during the lab session, you might consider writing a routine that works out the histogram of a single channel of an image, then adapt your program so that it:
|Web pages maintained by Adrian F. Clark [contact]|