Basic operations on images using OpenCV python
In this article, we will learn about essential Python OpenCV Basic Operations on images. We have used Dilation and Erosion on the image given below. Here we will also learn how to perform Face Detection on an image using Cascading Classifier.
import cv2 import numpy as np import matplotlib.pyplot as plt img = cv2.imread('images.jpeg') img= cv2.cvtColor(img, cv2.COLOR_BGR2RGB) plt.figure(figsize=(20, 20)) plt.subplot(3, 2, 1) plt.title("Original") plt.imshow(img) kernel = np.ones((5,5), np.uint8) erosion = cv2.erode(img, kernel, iterations = 1) plt.subplot(3, 2, 2) plt.title("Erosion") plt.imshow(erosion) dilation = cv2.dilate(img, kernel, iterations = 1) plt.subplot(3, 2, 3) plt.title("Dilation") plt.imshow(dilation)
<matplotlib.image.AxesImage at 0x7f12a880ff28>
img = cv.imread('WaldoBeach.jpg') img= cv.cvtColor(img, cv.COLOR_BGR2RGB) mask = np.zeros(img.shape[:2], np.uint8) mask[100:300, 100:400] = 255 masked_img = cv.bitwise_and(img,img,mask = mask) hist_full = cv.calcHist([img],[0],None,[256],[0,256]) hist_mask = cv.calcHist([img],[0],mask,[256],[0,256]) plt.figure(figsize=(20, 20)) plt.subplot(221), plt.imshow(img, 'gray') plt.subplot(222), plt.imshow(mask,'gray') plt.subplot(223), plt.imshow(masked_img, 'gray') plt.subplot(224), plt.plot(hist_full) plt.plot(hist_mask) plt.xlim([0,256]) plt.show()
img = cv.imread('images.jpeg') img= cv.cvtColor(img, cv.COLOR_BGR2RGB) pts1 = np.float32([[50,50],[200,50],[50,200]]) pts2 = np.float32([[10,100],[200,50],[100,250]]) plt.figure(figsize=(20, 20)) M = cv.getAffineTransform(pts1,pts2) dst = cv.warpAffine(img,M,(cols,rows)) plt.subplot(121),plt.imshow(img),plt.title('Input') plt.subplot(122),plt.imshow(dst),plt.title('Output') plt.show()
import cv2 from matplotlib.patches import Rectangle face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml') img = cv2.imread('images.jpeg') gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) img= cv.cvtColor(img, cv.COLOR_BGR2RGB) faces= face_cascade.detectMultiScale(gray, 1.1, 5) for (x,y,w,h) in faces: ax = plt.gca() ax.add_patch( Rectangle((x,y),w,h,fc ='none',ec ='b',lw = 4) ) plt.imshow(img,cmap = 'gray') plt.title('Sachin tendulkar'), plt.xticks([]), plt.yticks([]) plt.show()
import numpy as np import cv2 face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml') eye_cascade = cv2.CascadeClassifier('haarcascade_eye_tree_eyeglasses.xml') img = cv2.imread('images.jpeg') gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) img= cv.cvtColor(img, cv.COLOR_BGR2RGB) faces = face_cascade.detectMultiScale(gray, 1.3, 5) for (x,y,w,h) in faces: cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2) roi_gray = gray[y:y+h, x:x+w] roi_color = img[y:y+h, x:x+w] eyes = eye_cascade.detectMultiScale(roi_gray) for (ex,ey,ew,eh) in eyes: cv2.rectangle(roi_color,(ex,ey),(ex+ew,ey+eh),(0,255,0),2) plt.imshow(img,cmap = 'gray') plt.title('Sachin tendulkar'), plt.xticks([]), plt.yticks([]) plt.show()
From the above content we came to know about how many more operations can be used on an image using open cv and how these operations make a huge difference in their appearance. I also came to know how cascade classifier can detect any part of the body through the classifier that is used with the cascading and how we can plot a histogram through an image.