OpenCV Python – Template Matching

OpenCV Python – Template Matching ”; Previous Next The technique of template matching is used to detect one or more areas in an image that matches with a sample or template image. Cv.matchTemplate() function in OpenCV is defined for the purpose and the command for the same is as follows: cv.matchTemplate(image, templ, method) Where image is the input image in which the templ (template) pattern is to be located. The method parameter takes one of the following values − cv.TM_CCOEFF, cv.TM_CCOEFF_NORMED, cv.TM_CCORR, cv.TM_CCORR_NORMED, cv.TM_SQDIFF, cv.TM_SQDIFF_NORMED This method slides the template image over the input image. This is a similar process to convolution and compares the template and patch of input image under the template image. It returns a grayscale image, whose each pixel denotes how much it matches with the template. If the input image is of size (WxH) and template image is of size (wxh), the output image will have a size of (W-w+1, H-h+1). Hence, that rectangle is your region of template. Example In an example below, an image having Indian cricketer Virat Kohli’s face is used as a template to be matched with another image which depicts his photograph with another Indian cricketer M.S.Dhoni. Following program uses a threshold value of 80% and draws a rectangle around the matching face − import cv2 import numpy as np img = cv2.imread(”Dhoni-and-Virat.jpg”,1) cv2.imshow(”Original”,img) gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) template = cv2.imread(”virat.jpg”,0) cv2.imshow(”Template”,template) w,h = template.shape[0], template.shape[1] matched = cv2.matchTemplate(gray,template,cv2.TM_CCOEFF_NORMED) threshold = 0.8 loc = np.where( matched >= threshold) for pt in zip(*loc[::-1]): cv2.rectangle(img, pt, (pt[0] + w, pt[1] + h), (0,255,255), 2) cv2.imshow(”Matched with Template”,img) Output The original image, the template and matched image of the result as follows − Original image The template is as follows − The image when matched with template is as follows − Print Page Previous Next Advertisements ”;

OpenCV Python – Feature Matching

OpenCV Python – Feature Matching ”; Previous Next OpenCV provides two techniques for feature matching. Brute force matching and FLANN matcher technique. Example Following example uses brute-force method import numpy as np import cv2 img1 = cv2.imread(”lena.jpg”) img2 = cv2.imread(”lena-test.jpg”) # Convert it to grayscale img1_bw = cv2.cvtColor(img1,cv2.COLOR_BGR2GRAY) img2_bw = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY) orb = cv2.ORB_create() queryKeypoints, queryDescriptors = orb.detectAndCompute(img1_bw,None) trainKeypoints, trainDescriptors = orb.detectAndCompute(img2_bw,None) matcher = cv2.BFMatcher() matches = matcher.match(queryDescriptors,trainDescriptors) img = cv2.drawMatches(img1, queryKeypoints, img2, trainKeypoints, matches[:20],None) img = cv2.resize(img, (1000,650)) cv2.imshow(“Feature Match”, img) Output Print Page Previous Next Advertisements ”;

OpenCV Python – Image Blending

OpenCV Python – Image Blending with Pyramids ”; Previous Next The discontinuity of images can be minimised by the use of image pyramids. This results in a seamless blended image. Following steps are taken to achieve the final result − First load the images and find Gaussian pyramids for both. The program for the same is as follows − import cv2 import numpy as np,sys kalam = cv2.imread(”kalam.jpg”) einst = cv2.imread(”einstein.jpg”) ### generate Gaussian pyramid for first G = kalam.copy() gpk = [G] for i in range(6): G = cv2.pyrDown(G) gpk.append(G) # generate Gaussian pyramid for second G = einst.copy() gpe = [G] for i in range(6): G = cv2.pyrDown(G) gpe.append(G) From the Gaussian pyramids, obtain the respective Laplacian Pyramids. The program for the same is as follows − # generate Laplacian Pyramid for first lpk = [gpk[5]] for i in range(5,0,-1): GE = cv2.pyrUp(gpk[i]) L = cv2.subtract(gpk[i-1],GE) lpk.append(L) # generate Laplacian Pyramid for second lpe = [gpe[5]] for i in range(5,0,-1): GE = cv2.pyrUp(gpe[i]) L = cv2.subtract(gpe[i-1],GE) lpe.append(L) Then, join the left half of the first image with the right half of second in each level of pyramids. The program for the same is as follows − # Now add left and right halves of images in each level LS = [] for la,lb in zip(lpk,lpe): rows,cols,dpt = la.shape ls = np.hstack((la[:,0:int(cols/2)], lb[:,int(cols/2):])) LS.append(ls) Finally, reconstruct the image from this joint pyramid. The program for the same is given below − ls_ = LS[0] for i in range(1,6): ls_ = cv2.pyrUp(ls_) ls_ = cv2.add(ls_, LS[i]) cv2.imshow(”RESULT”,ls_) Output The blended result should be as follows − Print Page Previous Next Advertisements ”;

OpenCV Python – Using Matplotlib

OpenCV Python – Using Matplotlib ”; Previous Next Python’s Matplotlib is a powerful plotting library with a huge collection of plotting functions for the variety of plot types. It also has imshow() function to render an image. It gives additional facilities such as zooming, saving etc. Example Ensure that Matplotlib is installed in the current working environment before running the following program. import numpy as np import cv2 import matplotlib.pyplot as plt # Load an color image in grayscale img = cv2.imread(”OpenCV_Logo.png”,0) plt.imshow(img) plt.show() Output Print Page Previous Next Advertisements ”;

OpenCV Python – Overview

OpenCV Python – Overview ”; Previous Next OpenCV stands for Open Source Computer Vision and is a library of functions which is useful in real time computer vision application programming. The term Computer vision is used for a subject of performing the analysis of digital images and videos using a computer program. Computer vision is an important constituent of modern disciplines such as artificial intelligence and machine learning. Originally developed by Intel, OpenCV is a cross platform library written in C++ but also has a C Interface Wrappers for OpenCV which have been developed for many other programming languages such as Java and Python. In this tutorial, functionality of OpenCV’s Python library will be described. OpenCV-Python OpenCV-Python is a Python wrapper around C++ implementation of OpenCV library. It makes use of NumPy library for numerical operations and is a rapid prototyping tool for computer vision problems. OpenCV-Python is a cross-platform library, available for use on all Operating System (OS) platforms including, Windows, Linux, MacOS and Android. OpenCV also supports the Graphics Processing Unit (GPU) acceleration. This tutorial is designed for the computer science students and professionals who wish to gain expertise in the field of computer vision applications. Prior knowledge of Python and NumPy library is essential to understand the functionality of OpenCV-Python. Print Page Previous Next Advertisements ”;

OpenCV Python – Reading Image

OpenCV Python – Reading an image ”; Previous Next The CV2 package (name of OpenCV-Python library) provides the imread() function to read an image. The command to read an image is as follows − img=cv2.imread(filename, flags) The flags parameters are the enumeration of following constants − cv2.IMREAD_COLOR (1) − Loads a color image. cv2.IMREAD_GRAYSCALE (0) − Loads image in grayscale mode cv2.IMREAD_UNCHANGED (-1) − Loads image as such including alpha channel The function will return an image object, which can be rendered using imshow() function. The command for using imshow() function is given below − cv2.imshow(window-name, image) The image is displayed in a named window. A new window is created with the AUTOSIZE flag set. The WaitKey() is a keyboard binding function. Its argument is the time in milliseconds. The function waits for specified milliseconds and keeps the window on display till a key is pressed. Finally, we can destroy all the windows thus created. The function waits for specified milliseconds and keeps the window on display till a key is pressed. Finally, we can destroy all the windows thus created. The program to display the OpenCV logo is as follows − import numpy as np import cv2 # Load a color image in grayscale img = cv2.imread(”OpenCV_Logo.png”,1) cv2.imshow(”image”,img) cv2.waitKey(0) cv2.destroyAllWindows() The above program displays the OpenCV logo as follows − Print Page Previous Next Advertisements ”;

OpenCV Python – Environment

OpenCV Python – Environment Setup ”; Previous Next In most of the cases, using pip should be sufficient to install OpenCV-Python on your computer. The command which is used to install pip is as follows − pip install opencv-python Performing this installation in a new virtual environment is recommended. The current version of OpenCV-Python is 4.5.1.48 and it can be verified by following command − >>> import cv2 >>> cv2.__version__ ”4.5.1” Since OpenCV-Python relies on NumPy, it is also installed automatically. Optionally, you may install Matplotlib for rendering certain graphical output. On Fedora, you may install OpenCV-Python by the below mentioned command − $ yum install numpy opencv* OpenCV-Python can also be installed by building from its source available at http://sourceforge.net Follow the installation instructions given for the same. Print Page Previous Next Advertisements ”;

OpenCV Python – Write Image

OpenCV Python – Write an image ”; Previous Next CV2 package has imwrite() function that saves an image object to a specified file. The command to save an image with the help of imwrite() function is as follows − cv2.imwrite(filename, img) The image format is automatically decided by OpenCV from the file extension. OpenCV supports *.bmp, *.dib , *.jpeg, *.jpg, *.png,*.webp, *.sr,*.tiff, *.tif etc. image file types. Example Following program loads OpenCV logo image and saves its greyscale version when ‘s’ key is pressed − import numpy as np import cv2 # Load an color image in grayscale img = cv2.imread(”OpenCV_Logo.png”,0) cv2.imshow(”image”,img) key=cv2.waitKey(0) if key==ord(”s”): cv2.imwrite(“opencv_logo_GS.png”, img) cv2.destroyAllWindows() Output Print Page Previous Next Advertisements ”;

OpenCV Python – Mouse Events

OpenCV Python – Handling Mouse Events ”; Previous Next OpenCV is capable of registering various mouse related events with a callback function. This is done to initiate a certain user defined action depending on the type of mouse event. Sr.No Mouse event & Description 1 cv.EVENT_MOUSEMOVE When the mouse pointer has moved over the window. 2 cv.EVENT_LBUTTONDOWN Indicates that the left mouse button is pressed. 3 cv.EVENT_RBUTTONDOWN Event of that the right mouse button is pressed. 4 cv.EVENT_MBUTTONDOWN Indicates that the middle mouse button is pressed. 5 cv.EVENT_LBUTTONUP When the left mouse button is released. 6 cv.EVENT_RBUTTONUP When the right mouse button is released. 7 cv.EVENT_MBUTTONUP Indicates that the middle mouse button is released. 8 cv.EVENT_LBUTTONDBLCLK This event occurs when the left mouse button is double clicked. 9 cv.EVENT_RBUTTONDBLCLK Indicates that the right mouse button is double clicked. 10 cv.EVENT_MBUTTONDBLCLK Indicates that the middle mouse button is double clicked. 11 cv.EVENT_MOUSEWHEEL Positive for forward and negative for backward scrolling. To fire a function on a mouse event, it has to be registered with the help of setMouseCallback() function. The command for the same is as follows − cv2.setMouseCallback(window, callbak_function) This function passes the type and location of the event to the callback function for further processing. Example 1 Following code draws a circle whenever left button double click event occurs on the window showing an image as background − import numpy as np import cv2 as cv # mouse callback function def drawfunction(event,x,y,flags,param): if event == cv.EVENT_LBUTTONDBLCLK: cv.circle(img,(x,y),20,(255,255,255),-1) img = cv.imread(”lena.jpg”) cv.namedWindow(”image”) cv.setMouseCallback(”image”,drawfunction) while(1): cv.imshow(”image”,img) key=cv.waitKey(1) if key == 27: break cv.destroyAllWindows() Output Run the above program and double click at random locations. The similar output will appear − Example 2 Following program interactively draws either rectangle, line or circle depending on user input (1,2 or 3) − import numpy as np import cv2 as cv # mouse callback function drawing=True shape=”r” def draw_circle(event,x,y,flags,param): global x1,x2 if event == cv.EVENT_LBUTTONDOWN: drawing = True x1,x2 = x,y elif event == cv.EVENT_LBUTTONUP: drawing = False if shape == ”r”: cv.rectangle(img,(x1,x2),(x,y),(0,255,0),-1) if shape == ”l”: cv.line(img,(x1,x2),(x,y),(255,255,255),3) if shape==”c”: cv.circle(img,(x,y), 10, (255,255,0), -1) img = cv.imread(”lena.jpg”) cv.namedWindow(”image”) cv.setMouseCallback(”image”,draw_circle) while(1): cv.imshow(”image”,img) key=cv.waitKey(1) if key==ord(”1”): shape=”r” if key==ord(”2”): shape=”l” if key==ord(”3”): shape=”c” #print (shape) if key == 27: break cv.destroyAllWindows() On the window surface, a rectangle is drawn between the coordinates of the mouse left button down and up if ‘1’ is pressed. If user choice is 2, a line is drawn using coordinates as endpoints. On choosing 3 for the circle, it is drawn at the coordinates of the mouse up event. Following image will be the output after the successful execution of the above mentioned program − Print Page Previous Next Advertisements ”;

OpenCV Python – Image Properties

OpenCV Python – Image Properties ”; Previous Next OpenCV reads the image data in a NumPy array. The shape() method of this ndarray object reveals image properties such as dimensions and channels. The command to use the shape() method is as follows − >>> img = cv.imread(“OpenCV_Logo.png”, 1) >>> img.shape (222, 180, 3) In the above command − The first two items shape[0] and shape[1] represent width and height of the image. Shape[2] stands for a number of channels. 3 indicates that the image has Red Green Blue (RGB) channels. Similarly, the size property returns the size of the image. The command for the size of an image is as follows − >>> img.size 119880 Each element in the ndarray represents one image pixel. We can access and manipulate any pixel’s value, with the help of the command mentioned below. >>> p=img[50,50] >>> p array([ 1, 1, 255], dtype=uint8) Example Following code changes the color value of the first 100X100 pixels to black. The imshow() function can verify the result. >>> for i in range(100): for j in range(100): img[i,j]=[0,0,0] Output The image channels can be split in individual planes by using the split() function. The channels can be merged by using merge() function. The split() function returns a multi-channel array. We can use the following command to split the image channels − >>> img = cv.imread(“OpenCV_Logo.png”, 1) >>> b,g,r = cv.split(img) You can now perform manipulation on each plane. Suppose we set all pixels in blue channel to 0, the code will be as follows − >>> img[:, :, 0]=0 >>> cv.imshow(“image”, img) The resultant image will be shown as below − Print Page Previous Next Advertisements ”;