Reference no: EM132578979
6302ENG Image Processing and Machine Vision - Griffith University
Experiment - Digital Images and Basic Image Processing
Aim
The aim of this experiment is to understand more fully the practicalities of digital images and basic image processing. The students will also be familiar with the Python programming environment and OpenCV library.
Procedure
Follow the instructions in each section below. The report for this experiment is to be submitted by the Friday of the lab week and should explain the processes and results. Include appropriate images along with a full listing of your Python code. You may work in pairs during the experiment, but you must submit your own report. If you worked with somebody else, please state this on your report.
Python and OpenCV Familiarisation (complete before lab)
If you have never used Python before, it is recommended that you run through some basic tutorials before attempting these lab exercises. Python is a simple but powerful language and is very quick to learn. We will be using only the more basic feature of the language, primarily calls to OpenCV functions, so learning what is necessary should be relatively quick.
1. Reading and Writing Images
Using Python and OpenCV, create a script perform the following tasks. You can download any of the images from the Learning@Griffith site or use your own for this, but they should be colour images.
• Read an image from file. Use the imread() function to do this.
• Print out the basic properties of the image - the dimensions, the total number of pixels, and the data type. Use .size, .shape and .dtype for this.
• Display the image to the screen using the imshow() function. Note that you may need to use waitKey() to pause the program and allow the images to be seen. You can destroy the windows by using destroyAllWindows()
• Write the image to another file using imwrite()
• Extract each colour channel from the image using the split() function. Display each channel separately in its own image, and save each of these to files. Note that OpenCV stores images in the BGR format by default, not RGB, so the first layer will be blue.
2. Bit Depth
Bit depth defines the number of bits used to store each pixel. Most images use 8 bits, but more or fewer bits are possible. To simulate different bit depths, you can use only the n most significant bits for each pixel. You can use a bitwise AND operator (&) to mask each pixel value (assuming it is a byte), for example for a depth of 6 bits you would mask each byte with the byte '11111100' or 252. A bit depth of 2 would be masked with '11000000' or 192. More generally, the mask you would use for a bit depth of n is given by (2n-1)*(28-n)
• Write a Python script which prompts the user to enter a bit depth (1-8) then displays an image in that bit depth. Comment on the difference between bit depth of 8 down to 1.
3. Resampling (Resizing)
Images can be resized using various techniques. In OpenCV, the resize() function is used for this. As well as the image to be resized and the new size of the image (in pixels), this function can also use different interpolation functions to determine new pixel values. These range from very simple nearest neighbour interpolation (INTER_NEAREST) to a complicated Lanczos interpolation over an 8x8 neighbourhood (INTER_LANCZOS4).
• Write a Python script which reads an image and then resizes it by both increasing and decreasing the scale.
• Use three different interpolation methods (see OpenCV tutorial for these values) and comment on the resulting images.
• Show examples of cases where the aspect ratio is both maintained and altered.
4. Geometric Transformations
Images can be translated, scaled and rotated in OpenCV using the warpAffine() function. This function takes an image and transformation matrix (2x3) as input, and returns the transformed image. For simple rotations (multiples of 90 degrees) rotate() can also be used. Read the documentation for these functions, then use them to perform the following tasks:
• Read an image from file
• Transform the image by rotating it 90 degrees clockwise, then save the file (to a new filename)
• Transform the original image by asking the user to enter a value in degrees, then rotating the image by this much. Note that you can either calculate the transformation matrix manually or create it with getRotationMatrix2D(). Display these transformed images and save to file.
• Use the same function warpAffine() to translate the image by a constant amount, and display and save this image.
• Some of these operations will cause parts of the image to be lost, as it goes outside the borders. To overcome this you can first 'pad' the image by adding additional pixels to each side. Use the copyMakeBorder() function to do this. Repeat the rotation exercise, but first add a border to the image of an appropriate size to avoid any pixels being lost. Display and save these images.
5. Arithmetic Image Operations
It is possible to perform arithmetic operations on images using either scalar values or other images. Scalar operations can be performed using the normal arithmetic operators (+, -, *, /, etc). In this case the operation is applied independently to every pixel in the image. Any value which overruns the maximum value of the data type (typically 255 for uint8) will be 'reset' to 0 and continue adding (modulo addition). Another way to add images is using the add() function in OpenCV. Unlike numpy addition, this operation saturates at the maximum pixel value, so these pixels will stay white and not reset back to 0 again.
• Try adding a constant value to every pixel in an image, then displaying the result. Note what happens as the value you add gets high.
• Repeat this operation using the OpenCV add() function and comment on the difference.
Images can also be combined using either normal operators (+, -) which will perform modulo operations ('reset' after passing limits), or using the OpenCV add() or addWeighted() functions. addWeighted() has additional arguments that determine how much each image contributes to the final image. This can be used to add a watermark to an image, for example. Note that the images must be of the same size to be added in this way!
• Use addWeighted() to combine two images in various weightings. Save and comment on the results.
Attachment:- Image Processing and Machine Vision.rar