Basic Operations on Images using OpenCV

Tharuja Sandeepanie
5 min readMay 11, 2020

By my previous article, I explained how to setup the environment to work with OpenCV ( link). So, now I will explain some basic operations we can do on images using OpenCV.

First we will see how to load an image.

Here, if the file does not exist, the imread function will not raise any exception and a none value will be returned instead.

Display your image

This will display your image in a separate window. Since my image is too large, I have to resize it into a smaller size to be shown in the window. The cv.waitKey() is to let the system “wait” without closing the window. We can give time duration to wait the window in the (). cv.waitKey(0) will wait continuously until the user press any key.Without this instruction, the system would return immediately without actually displaying the image. The cv2.destroyAllWindows() function is used to close all open windows.

Here I display the image using OpenCV ,but when working with Jupyter notebook, it is better o use matplotlib to display images.

Here, colors are not correct. It is becuase OpenCV reads images in the BGR format, while matplotlib reads images in the standard RGB format.
If you want to change the color type, do

# cv2.cvtcolor function for color conversions.
img2 = cv2.cvtColor(img,cv2.COLOR_BGR2RGB)
OR# numpy indexing can flip the order of the channels.
plt.imshow(img[…,::-1])

Check image properties

Here, you can find that the loaded image is a numpy array.

And img.shape indicates the number of pixels (2469 X 3704) with three channels (since this is a RGB color image). If the image is gray-scale, image shape contains only number of rows and columns.

The data type of the image is uint8, i.e., 8 bit unsigned int (numbers from 0 to 255) as indicated by img.dtype.

Minimum and maximum number in the matrix can be checked using the min and max functions.

Total number of pixels can be obtained by img.size

Access and modify color of the pixels

# Access and print a value in a pixel
print(img[10,10])
# Output shows the color value of that pixel as [38 30 31]
# Change the color of an area (range of pixels)
img[10:20, 10:20] = [255,255,255]
OR# Certain region of image (ROI) is obtained using Numpy indexing
ball = img[500:1000, 1500:2000]
img[273:773, 100:600] = ball

Basic Image processing operations

1- Convert a color image to gray-scale image

2- Gaussian Blur

3. Image Blending

The arrays obtained from each picture must be with the same size otherwise an error will be triggered. So , need to resize the images first to have the same height and weight.

In OpenCv we can blend images (similar to image addition) using the cv2.addWeighted function, It will apply the following equation on an image

cv2.addWeighted(img, 0.7, img2, 0.5, 0) — Here , numbers after the image name are the weights as in the above equation. They can be varied from 0 to 1. According to that weight it determines how much to blend the two images in other words whether to make them more/less transparent. The additional 0 at the end of the line of code represents gamma.

4. Edge Detection

5. Sobel Derivatives

Draw Objects

Using OpenCV we can draw objects(shapes like circles, rectangles..) on images. First we will create a blank image.

So, now we can draw objects as shown below on this blank image.

Parameters in each function indicate following properties of the shapes.

cv2.line(image, startPoint(x,y), endPoint(x,y), rgb, thinkness)

cv2.rectangle(image, topLeft(x,y), bottomRight(x,y), rgb, thinkness)

cv2.circle(image, center(x,y), radius, rgb, thinkness)

cv2.ellipse(image, center, axes, angle, startAngle, endAngle, rgb, thinkness)

cv2.polylines(image, points, isClosed, rgb, thinkness, lineType, shift)

Add a text on an image

cv2.putText(img, ‘Dog’, (1000,400), font, 15, (0,0,255), 30, cv2.LINE_AA)— here parameters represents following properties of the text.

cv2.putText(image, text to be displayed , bottom-left(x,y coordinates ), font type, font scale, rgb color value, thickness, line type)

Acquire video from a camera

We can obtain a video stream from the webcam as below:

# Constructor of this class gets the index of the device we want to use. If we just have a single camera connected to the computer, we can pass the value 0. It indicates the default camera.cap = cv2.VideoCapture(0)#This reads a frame "img" and a return value "r", set to true if there are no errors.
r,img = cap.read()
while r:
#show the obtained frame
cv2.imshow('Frame',img)
#process current frame
img_c = cv2.Canny(img,180,200)
#show processed frame
cv2.imshow('Canny',img_c)
a=cv2.waitKey(20) #wait for 20 ms (i.e., 25 fps)
if a != -1: #if any key was pressed:
r = False
else:
r,img = cap.read() #else, read new frame

# Release the video capture object
cap.release()
# Close all the frames
cv2.destroyAllWindows()

--

--