Working with images (working_with_images)

The following knowledge is required:
  • Structure of digital images

In this task, you will learn the basics of working with images in Python. We will use the opencv library, which is a powerful library for image processing. You can find the official documentation here: https://docs.opencv.org/4.x/d6/d00/tutorial_py_root.html.

Task 1: Image Basics

  1. Download the input files and extract them to your working directory.

  2. Open the image "wiese.jpg" using openCV and display it using matplotlib (imshow function).

  3. The image is in RGB format. OpenCV loads images in BGR format by default. Convert the image to RGB format before displaying it using cv2.cvtColor function with cv2.COLOR_BGR2RGB. Display it again.

  4. Now convert the image to a grayscale image using cv2.cvtColor function with cv2.COLOR_BGR2GRAY. Display the grayscale image.

  5. Make a copy of the R, G, and B channels of the original image in separate numpy arrays and display them separately using imshow. Pass the argument cmap=gray to show the values as intensities.

  6. Alternatively to above, you can also show the color channels with the corresponding colormaps. For this, use the colormap argument cmap='Reds', cmap='Greens', and cmap='Blues' respectively in imshow.

Task 2: Image Manipulation

In this task, we are going to manipulate the image channels. All tasks are based on the original RGB image from Task 1.

  1. Rebuild a new image matrix that contains the image channels in the order B, G, R (i.e., swap R and B channels). You can use np.stack(…​) for this. Display the new image. The result should look like the first image in this task.

  2. Now set the red channel to zeros and rebuild the image. Display the new image.

Task 3: Cropping and masking

  1. crop the image to a sub region that contains the butterfly only. Find out the pixel coordinates by iteratively adjusting the coordinates until you have the desired region. If you display the image without plt.axis('off'), the coordinate axis will be shown. Display the cropped image.

  2. Print out the RGB values of one of the central pixels of the butterfly containing blue color. This will be used in the next task.

  3. Now we try to build a binary mask of the butterfly by thresholding the RGB values. Set upper and lower threshold values for each channel (R, G, B) such that only the butterfly is selected. Create a binary mask where pixels within the threshold range are set to 1 and all other pixels are set to 0. Display the binary mask.

  4. Use the mask to extract the butterfly from the original image. For this, simply multiply the mask with all image channels separately. Display the extracted butterfly image.

Thresholding using RGB values is not very robust, as the color of the butterfly may vary due to lighting conditions, shadows, etc. Therefore, the result of this task may not be perfect. It will be improved in the next task using the HSV color space.

Task 4: HSV color space

In this task, we are going to work with HSV color space and manipulate the channels.

  1. Convert the original RGB image to HSV color space using cv2.cvtColor function with cv2.COLOR_RGB2HSV. Display the HSV image. Note that the displayed image may not look as expected, as imshow assumes RGB format.

  2. Split the HSV image into its three channels (H, S, V) and display them separately using imshow with cmap='gray'.

  3. Now manipulate the hue channel by reducing the hue values by 50%. Make a copy of the HSV image and modify the hue channel accordingly.

  4. Rebuild the HSV image and convert it back to RGB color space using cv2.cvtColor function with cv2.COLOR_HSV2RGB. Display the modified image.

  5. Similarly, manipulate the saturation channel by reducing the saturation values by 50%. Display the resulting image after converting it back to RGB color space.

  6. Finally, we are going to extract the butterfly using HSV color space. Determine suitable threshold values for H, S, and V channels to create a binary mask that selects the butterfly. Create the binary mask and display it.

  7. Use the mask to extract the butterfly from the original image as in Task 3. Display the extracted butterfly image.