The goal of this session is to make you comfortable with Python and the combination of Python with C++.
The practical assignments must be made in a combination of Python and C++. We assume that you have general programming skills, basic knowledge of C++ and no knowledge of Python. You will be provided with bits of Python/C++ example code to give you a quickstart. In this practical session, you can follow the next instructions to learn how to program in Python and how to use the example code. Following these instructions is facultative but recommended.
Python is easy to learn, easy to use, makes programming take less time and more fun. To start learning, (quickly) read and/or try the Python Short Course (via: Beginners guide or Programmers, which also points to the more extensive Python Tutorial).
Python is great but it has a weakness: 'for-loops' are slow. Therefore, for operations on pixels, we will use C++. These instructions illustrate how to combine Python and C++.
In the following instructions, several directory names are suggested, but you may use alternative directories if you insist.
mkdir ~/hwr
cp -r /home/student/hwr/toolbox ~/hwr/
cp -r /home/student/hwr/introduction ~/hwr/
df -h
to see how much space is available.convert -compress LZW /dev/shm/NL_HaNa_H2_7823_xxxx.tif /dev/shm/NL_HaNa_H2_7823_xxxx.ppm
cd ~/hwr/introduction
make
python example-crop.py /dev/shm/NL_HaNa_H2_7823_xxxx.ppm /dev/shm/cropped.ppm
python example-featextract.py /dev/shm/cropped.ppm
make clean
make
ls -ltr
If you are finished early, you are suggested to start working on Assignment 1.
introduction/
File | Description |
croplib.cpp | Crop p*m images. |
croplib.h | Header for croplib.cpp. |
croplib.i | Interface between C++ and Python for croplib. |
example-crop.py | Shows how to use croplib. Crops an image. |
example-featextract.py | Shows how to use featlib. Computes a simple feature vector containing one value: the fraction of dark pixels. |
featlib.cpp | Example feature extraction code: computes the fraction of dark pixels. |
featlib.h | Header for featlib.cpp |
featlib.i | Interface between C++ and Python for featlib. |
Makefile | Instructions for 'make' to compile the code. |
toolbox/
File | Description |
pamImage.cpp | Read and write .pbm/.pgm/.ppm files (black/white, grayscale, color respectively) (you don't need to look inside this file). |
pamImage.h | Header for pamImage.cpp; look here to see what you can do with a PamImage object. |
pamImage.i | Interface between C++ and Python for pamImage. |
word.py | Object for word zones and transcription (needed for later assignments). |
wordio.py | Reads and writes .words files (needed for later assignments). |
wordsline.py | Object for text line zones and associated word zones (needed for later assignments). |
The .i files define an interface between C++ and Python. Swig uses them to create a Python wrapper around the C++ code.