This post will be helpful in learning OpenCV using Python programming. Here I will show how to implement OpenCV functions and apply them in various aspects using some great examples. Then the output will be visualized along with the comparisons.
We will also discuss the basic of image processing and provide the detail explanation related to the OpenCV functions.
Requirements:
- OpenCV 3.4+
- Python 3.6+
- Numpy
- Image, Webcam or Video input
- Documentation Source: OpenCV Official Documentation
First, you need to setup your Python Environment with OpenCV. You can easily do it by following Life2Coding’s tutorial on YouTube: Linking OpenCV with Python 3
Goals:
The goal is to make you understand how to draw logo or image watermark on images using Python OpenCV.
Documentation:
resize()
dst=cv.resize(src, dsize[, dst[, fx[, fy[, interpolation]]]]) |
Resizes an image.
- Parameters
-
src input image. dst output image; it has the size dsize (when it is non-zero) or the size computed from src.size(), fx, and fy; the type of dst is the same as of src. dsize output image size; if it equals zero, it is computed as: dsize = Size(round(fx*src.cols), round(fy*src.rows))
Either dsize or both fx and fy must be non-zero.fx scale factor along the horizontal axis; when it equals 0, it is computed as (double)dsize.width/src.cols fy scale factor along the vertical axis; when it equals 0, it is computed as (double)dsize.height/src.rows interpolation interpolation method, see InterpolationFlags
imread()
retval=cv.imread(filename[, flags]) |
Loads an image from a file.
- Parameters
-
filename Name of file to be loaded. flags Flag that can take values of cv::ImreadModes
addWeighted()
dst=cv.addWeighted(src1, alpha, src2, beta, gamma[, dst[, dtype]]) |
Calculates the weighted sum of two arrays.
- Parameters
-
src1 first input array. alpha weight of the first array elements. src2 second input array of the same size and channel number as src1. beta weight of the second array elements. gamma scalar added to each sum. dst output array that has the same size and number of channels as the input arrays. dtype optional depth of the output array; when both input arrays have the same depth, dtype can be set to -1, which will be equivalent to src1.depth().
imshow()
None=cv.imshow(winname, mat) |
Displays an image in the specified window.
- Parameters
-
winname Name of the window. mat Image to be shown.
waitKey()
retval=cv.waitKey([, delay]) |
Waits for a pressed key.
- Parameters
-
delay Delay in milliseconds. 0 is the special value that means “forever”.
destroyAllWindows()
None=cv.destroyAllWindows() |
Destroys all of the HighGUI windows.
Steps:
- Load a face image using cv2.imread()
- Load the image using cv2.imread()
- Create the overlay copy of the image using copy()
- Draw the watermark image on the overlay image where the alpha channel value is not zero
- Then blend the images using cv2.addWeighted()
- Display the image using cv2.imshow()
- Wait for keyboard button press using cv2.waitKey()
- Exit window and destroy all windows using cv2.destroyAllWindows()
Example Code:
import cv2 def transparentOverlay(src, overlay, pos=(0, 0), scale=1): """ :param src: Input Color Background Image :param overlay: transparent Image (BGRA) :param pos: position where the image to be blit. :param scale : scale factor of transparent image. :return: Resultant Image """ overlay = cv2.resize(overlay, (0, 0), fx=scale, fy=scale) h, w, _ = overlay.shape # Size of foreground rows, cols, _ = src.shape # Size of background Image y, x = pos[0], pos[1] # Position of foreground/overlay image # loop over all pixels and apply the blending equation for i in range(h): for j in range(w): if x + i >= rows or y + j >= cols: continue alpha = float(overlay[i][j][3] / 255.0) # read the alpha channel src[x + i][y + j] = alpha * overlay[i][j][:3] + (1 - alpha) * src[x + i][y + j] return src def addImageWatermark(LogoImage,MainImage,opacity,pos=(10,100),): opacity = opacity / 100 OriImg = cv2.imread(MainImage, -1) waterImg = cv2.imread(LogoImage, -1) tempImg = OriImg.copy() print(tempImg.shape) overlay = transparentOverlay(tempImg, waterImg, pos) output = OriImg.copy() # apply the overlay cv2.addWeighted(overlay, opacity, output, 1 - opacity, 0, output) cv2.imshow('Life2Coding', output) cv2.waitKey(0) cv2.destroyAllWindows() if __name__ == '__main__': addImageWatermark('./logo.png','./hanif.jpg',100,(10,100))
Output:
- How to Create a RGB Color Picker for Images using OpenCV Python - 22 April, 2022
- Combine Several Images Vertically with Padding using OpenCV Python - 21 April, 2022
- Combine Several Images Horizontally with Padding using OpenCV Python - 21 April, 2022