[Having written a function](/node/66/) to read a PGM image, how do we use it?
Well, the testbench at the bottom of the pgm vhdl file has an example:
variable i : pixel_array_ptr;
— Without the transpose function, we would have to present the initialisation data in a non-intuitive way.
constant testdata : pixel_array(0 to 7, 0 to 3) := transpose(pixel_array'(
(000, 027, 062, 095, 130, 163, 198, 232),
(000, 000, 000, 000, 000, 000, 000, 000),
(255, 255, 255, 255, 255, 255, 255, 255),
Having [set up a library](/node/65/) for reading images, let’s now go on to read an image in!
Recall, we have a function:
impure function pgm_read (filename : string)
So, some declarations – fairly self-explanatory:
file pgmfile : text;
variable width, height : coordinate; — storage for image dimensions
variable l : line; — buffer for a line of text
variable s : string(1 to 2); — to check the P2 header
One things VHDL can do ([contrary to popular belief :)](/node/60/)) is 2-D arrays. So reading images into a 2-D array is a very natural way to store the data.
We’ll create a package called `pgm` to keep all our image reading and writing code together:
package pgm is
subtype coordinate : natural;
subtype pixel is integer range 0 to 255;
type pixel_array is array (coordinate range <>, coordinate range <>) of pixel;
type pixel_array_ptr is access pixel_array;
— Function: transpose
The `assert_equal` functions are part of my library of useful things (maybe this could be a start towards [VHDL’s jQuery](http://www.tekphile.com/2010/12/where-is-vhdls-jquery/) :) `libv`. Why not just use `assert` as is? Well, it’s useful to report all assert results in the same fashion, particularly to include the values that you expected and what you tested, along with the difference between them – often this is useful debugging information.
Here’s an example….
It’s been very kindly pointed out to me via email (by Tricky) that I was talking complete rubbish saying that [VHDL is rubbish at 2d arrays](/node/60/).
What I should’ve said was “VHDL is rubbish at arrays of other arrays”, which is a subtle but critical difference! Arrays of other arrays are very useful (for example in register files), and the fact that the dimensions of one of the arrays must be known at compile time can make some things tricky to impossible. For example, a generic mux with a variable number of inputs can’t easily also have a generic number of bits on each input.
Next up in the image processing series – [reading image files in VHDL](/node/60). This allows our testbenches to operate on real image data. After this we can create a camera model which will produce the pixels and synchronisation signals like a real camera so that we can do complete image tests of a processing pipeline.
Please note that this page talks rubbish – [here’s why](/node/62), and [here’s a better version](/node/65).
If we’re going to do some image processing in VHDL, we’re going to need to be able to read images in to our simulations. VHDL’s file handling is pretty poor to say the least, so we’ll keep things simple by only handling a very simple file format – namely [Portable Greymap (PGM)](http://netpbm.sourceforge.net/doc/pgm.html). And for even more simplicity we’ll handle on one variant with 8-bit pixels (P2 format with Maxval<256).
Inadvertently made the front page of [Hackaday](http://hackaday.com/) with the [solderless drawdio](http://hackaday.com/2010/11/09/solderless-drawdio-terminally-implemented/).
Which resulted in an order of magnitude more hits than normal before my server fell over with mysqld leaking its guts all over the console. The youtube [video of it in operation](http://www.youtube.com/watch?v=ooOPQ4h2pq0) that is also linked to from Hackaday has now had 1600+ hits!
A new section of the image processing series is now up – [corner detection](http://www.parallelpoints.com/node/56/)
# Detecting corners
Let’s use a corner detector as an example. [Corner detection](http://en.wikipedia.org/wiki/Corner_detection) is an important image processing task: corners are good things to track over several frames of an image sequence. Once you’ve tracked them you can infer 3-d positions for them.