This is the very first tutorial in the OpenCV Python Tutorial series which is aimed at introducing viewers who want to learn about computer vision to get started with OpenCV development with python. If you have any question, leave it in the comments below.

In this tutorial, we need two python libraries to generate colors from matrices and display them – cv2(OpenCV python library) and numpy.

OpenCV is one of the most popular computer vision library in the world, began in 2000, offering functions that are widely used in the development of real-time computer vision, such as image manipulation and motion analysis.

NumPy has an elder age than OpenCV, and it is commonly equipped by most developers who need to perform operations and calculations about multi-dimensional arrays and matrices.

Compatibility

  • python – 3.6.8
  • cv2 – 3.4.2
  • numpy – 1.15.4

You do not have to match the same version number, but be aware that different versions of libraries may have changed something, so go to check out their API document if it breaks your codes.

Helper functions

  • gen_solid_img – generates solid color image from matrics
  • cv2_img_show – displays the image in OpenCV 3 window
  • np.full(Shape of the matrix/image – (height, width, channels), Component value – Single value for grayscale image or (B, G, R) for BGR color image), Type of pixel value)
  • cv2.imshow(header of window, image source)
def gen_solid_img(shape, color):
    image = np.full(shape, color, np.uint8)
    return image
 
def cv2_img_show(title, image):
    cv2.imshow(title, image)
    cv2.waitKey()
    cv2.destroyAllWindows()

Generate colors

A regular digital image file consists of header data and image data. In the image data, the pixel values are stored line by line without breaks and the values ranges from 0 to 255 due to a byte can hold 2 to the power of 8 minus 1 digits, so we can definitely generate our own image pixel by pixel

The only difference between a grayscale image and a color image is the number of channels. A grayscale image contains image data in the form of [Number of rows|Image height in pixel, Number of columns|Image Width in pixel, Grayscale level from 0 to 255]. Similarly, a color image (RGB) contains [Number of rows|Image height in pixel, Number of columns|Image Width in pixel, [Red level, Green level, Blue level]].

Please remeber, OpenCV uses BGR (Blue, Green, Red) channels for color image by default

# Solid Colors Generation
 
img_white = gen_solid_img((768, 1024, 3), 255) # White
img_black = gen_solid_img((768, 1024, 3), 0) # Black
img_red = gen_solid_img((768, 1024, 3), (76, 76, 255)) # Red
 
cv2_img_show("White", img_white)
cv2_img_show("Black", img_black)
cv2_img_show("Red", img_red)
 
'''
Press any key to close the current window and jump to the 
next window. Do not click close button because it does not 
kill the process
'''

Fill color in channels

img_new = img_red.copy() # Create a copy of numpy multi-dimentional array
 
img_new[:, :, 0] = 255 # Fill any pixel in Blue channel to 255
cv2_img_show("New", img_new)
 
img_new[:, :, 1] = 255 # Fill any pixel in Green channel to 255
cv2_img_show("New", img_new)
 
img_new[:, :, 2] = 255 # Fill any pixel in Red channel to 255
cv2_img_show("New", img_new)
 
img_new[:, :, :] = 255 # Fill any pixel in any channel to 255 -> white image
cv2_img_show("White", img_new)
 
img_new[:, :, :] = (255, 0, 0) # Fill any pixel in any channel to BGR value of (255, 0, 0) -> blue image
cv2_img_show("Blue", img_new)

Draw dots

img_dot_1 = img_white.copy()
img_dot_1[100, 150] = img_dot_1[100, 200] = img_dot_1[100, 250] = (76, 76, 255) # Draw red dots at [100, 150], [100, 200] and [100, 250]
 
# Same as
img_dot_1[100, 150, :] = img_dot_1[100, 200, :] = img_dot_1[100, 250, :] = (76, 76, 255)
 
cv2_img_show("Dot 1", img_dot_1)

Draw lines

img_line_1 = img_white.copy()
img_line_1[100, :, :] = 0 # Draw a horizontal line at 100 pixel on y, starting from the origion (top-left). Can be assigned to a bgr value
 
img_line_2 = img_white.copy()
img_line_2[:, 100, :] = 0 # Draw a vertical line at 100 pixel on x, starting from the origion (top-left). Can be assigned to a bgr value
 
cv2_img_show("Line 1", img_line_1)
cv2_img_show("Line 2", img_line_2)

Draw regions

img_region_1 = img_white.copy()
img_region_1[100:200, 500:600, :] = 0
 
img_region_2 = img_white.copy()
img_region_2[:, 500:600, :] = (76, 76, 255)
 
cv2_img_show("Region 1", img_region_1)
cv2_img_show("Region 2", img_region_2)