OpenCV is the best image processing library. Most of the works you do on images using it. Therefore it is very important that you should know how to create matrices, numpy arrays, and operate on these. Doing some basic operations on the image type you can easily understand the complex problem of OpenCV. In this entire intuition, you will know the OpenCV Data types and structures and how to operate scalar operation on this type.
In images generally, for any processing, you have to deal with the numpy array. Let’s create different types of images using numpy. If you know how to create these images then you will easily understand the data types and structures used in the images. Therefore read the entire tutorial.
Creation of the Various Images using Numpy
You can create the black image bypassing the pixel information and channel in the zeroes() method. For example lets I want to create the 200×200 pixel image then I will pass the following information.
black = np.zeros([200,200,1],"uint8")
Here I have used a list of [200,200,1]. The first part is the height in pixel, the second is the width of the image and the third is the channel. After that, you have to indicate the type of image. Like, in this case, I have used unsigned int of 8 bit. Therefore the values of the image type will from 0 to 255 (2 power of 8).
The next step is to show the image on the screen. Thus we will use OpenCV module method imgshow() . And to find the location of the image you can pass the values of the pixel and channel. For example, I want to find the color for the first pixel for all the channels then I will use black[0,0,:]. Full code is below.
import numpy as np import cv2 black = np.zeros([200,200,1],"uint8") cv2.imshow("Black",black) print(black[0,0,:]) cv2.waitKey(0) cv2.destroyAllWindows()
Please note that you when you run the code without the waitKey. The image will show and closed with the blink of the eye. Therefore I have defined waitKey(0) to now allow to automatically closed the window until I press the key. and destroyAllWindows() will closed all the opened images window.
Like you have used zeros() for the black image, you will use the ones() for the white image. But one change is that you have to use all the 3 channels. Let’s create and display a white image. Use the following code and run it.
import numpy as np import cv2 white = np.ones([200,200,3],"uint8") white *= (2**8-1) cv2.imshow("White",white) print(white[0,0,:]) cv2.waitKey(0) cv2.destroyAllWindows()
One point to note that for the completely white image you have to use all the pixel values to 255 maximum value. That’s why I have to multiply each pixel with the value (2 power 8 -1 ). When you will run it the output will be like below.
Most of you must know what RGB stands for. It stands for Red Green and Blue and but in OpenCV, it is called BGR (Bluer, Green, and Red) Channel. Let’s create all these channels using Numpy array.
To create it using the following code and run it.
#rgb color color = np.ones([200,200,3],"uint16") #red color red = color.copy() red[:,:] = (0,0,65535) cv2.imshow("Red Color",red) print(red[0,0,:])
Here I am creating an image using Numpy method ones of type unsigned integer of 16 bit of 200×200 with all the three channels. (R, G, B). For the red color image, I am modifying the channel values of the red with the 65535. Please note that We generally call RGB but in OpenCV, it is BGR channel. That’s why for the red you have to modify the last channel.
#green green = color.copy() green[:,:] = (0,65535,0) cv2.imshow("Green Color",green) print(green[0,0,:])
Modify the second channel with the highest value 65535 and the remaining zero.
#blue blue =color.copy() blue[:,:] = (65535,0,0) cv2.imshow("Blue Color",blue) print(blue[0,0,:])
In the same way, you will modify the first channel with the highest value 65535 for getting the blue color. Full code for the program is below.
import numpy as np import cv2 # rgb color color = np.ones([200, 200, 3], "uint16") # red color red = color.copy() red[:, :] = (0, 0, 65535) cv2.imshow("Red Color", red) print(red[0, 0, :]) # green green = color.copy() green[:, :] = (0, 65535, 0) cv2.imshow("Green Color", green) print(green[0, 0, :]) # blue blue = color.copy() blue[:, :] = (65535, 0, 0) cv2.imshow("Blue Color", blue) print(blue[0, 0, :]) cv2.waitKey(0) cv2.destroyAllWindows()
OpenCV is the best image processing packages. Most of the applications whether it is a web app or mobile app most of them use OpenCV packages. The above tutorial is basic on how to create the various colored image using numpy arrays or matrices. If you understood these concepts on Opencv Data Types and Structures ,then you can easily know how OpenCV works on images. There are also other tutorials that will come on OpenCV. So keep visiting our site for more tutorials.
If you want to get directly in your inbox then you can subscribe us. In the meantime, you can like or message on Data Science Learner FB Page.
Join our list
Subscribe to our mailing list and get interesting stuff and updates to your email inbox.