The first step to learning OpenCV is understanding the basics. This guide will show you how to open, display, modify and save images. A proper understanding of these building blocks is necessary before moving on to more advanced topics.
The most basic thing you can do with OpenCV would probably be opening and displaying an image. It makes a good 'hello world' project because it is easy and provides a great entry point to discussion on how OpenCV works.
Here is the image we will use. I suggest you take a look at it so you know what the correct output should look like.
import cv2 import matplotlib.pyplot as plt image = cv2.imread('4.2.03.tiff') plt.imshow(image) plt.show()
As you can see this output is wrong. It seems like all the red areas have become blue!
Unlike most modern applications, For historic reasons OpenCV uses the BGR color space as default intead of the more traditional RGB. This means that whenever we mix OpenCV with other libraries (like matplotlib) we need to be careful to convert to the appropriate colorspace as needed.
OpenCV images are 3 dimensional numpy arrays. The dimensions of the array are width, height, color. Since the color space of our image is BGR, the color dimension will contain arrays of size 3. To convert our image to RGB we just need to swap the 0th and 2nd indices of the color dimension. With numpy we can do this in just one line.
Lets try displaying our image again
image = cv2.imread('4.2.03.tiff') # Switches blue and red components to convert image to RGB image[:, :, (2, 0)] = image[:, :, (0, 2)] plt.imshow(image) plt.show()
We can write a function to make future conversion easier:
def switchRB(image): image[:, :, (2, 0)] = image[:, :, (0, 2)] return image
Since OCV images are just numpy arrays, editing images is straightforward. Lets say we want to remove the red channel of an image.
rgb_image = switchRB(cv2.imread('4.2.03.tiff')) rgb_image[:, :, 0] = 0 plt.imshow(rgb_image) plt.show()
We can convert a color image to grayscale by averaging the values of each pixel.
rgb_image = switchRB(cv2.imread('4.2.03.tiff')) gray_image = np.mean(rgb_image, axis=2) # Change cmap to prevent matplotlib from mapping our grayscale values to different colors plt.imshow(gray_image, cmap='gray') plt.show()