This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#cv2.getStructuringElement | |
#=============================== | |
#Generate Different Kernel Combinations | |
kernel1 = cv2.getStructuringElement(cv2.MORPH_RECT,(5,5)) | |
kernel2 = cv2.getStructuringElement(cv2.MORPH_ELLIPLSE,(5,5)) | |
kernel3 = cv2.getStructuringElement(cv2.MORPH_CROSS,(5,5)) | |
#cv2.filter2D | |
#============== | |
#OpenCV provides a function cv2.filter2D() to convolve a kernel with an image | |
#Example1 | |
import cv2 | |
import numpy as np | |
original_image = cv2.imread(r'E:\Opencv_Examples\goalkeeper.jpg') | |
kernel1 = cv2.getStructuringElement(cv2.MORPH_RECT,(5,5)) | |
kernel2 = cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(5,5)) | |
kernel3 = cv2.getStructuringElement(cv2.MORPH_CROSS,(5,5)) | |
conv1 = cv2.filter2D(original_image ,-1,kernel1) | |
conv2 = cv2.filter2D(original_image ,-1,kernel2) | |
conv3 = cv2.filter2D(original_image ,-1,kernel3) | |
cv2.imshow('conv1',conv1) | |
cv2.imshow('conv2',conv2) | |
cv2.imshow('conv3',conv3) | |
cv2.waitKey(0) | |
cv2.destroyAllWindows() | |
#cv2.calcHist | |
#============= | |
#cv2.calcHist() function to find the histogram | |
#cv2.calcHist(images, channels, mask, histSize, ranges[, hist[, accumulate]]) | |
#channels - grayscale image [0]. [0], [1] or [2] to calculate histogram of blue, green or red channel | |
#mask : mask image. To find histogram of full image, it is given as "None" | |
#histSize : this represents our BIN count. Need to be given in square brackets. For full scale, we pass [256]. | |
#ranges : this is our RANGE. Normally, it is [0,256]. | |
##Example2 | |
import cv2 | |
original_image = cv2.imread(r'E:\Opencv_Examples\goalkeeper.jpg',cv2.IMREAD_GRAYSCALE) | |
hist = cv2.calcHist(original_image,[0],None,[256],[0,256]) | |
original_image = cv2.imread(r'E:\Opencv_Examples\goalkeeper.jpg',cv2.IMREAD_COLOR) | |
hist_r = cv2.calcHist(original_image,[0],None,[256],[0,256]) | |
hist_g = cv2.calcHist(original_image,[1],None,[256],[0,256]) | |
hist_b = cv2.calcHist(original_image,[2],None,[256],[0,256]) | |
from matplotlib import pyplot as plt | |
plt.hist(hist_b, bins=10) | |
plt.ylabel('Values') | |
plt.show() | |
#cv2.threshold | |
#=============== | |
#If pixel value is greater than a threshold value, it is assigned one value (may be white) | |
#assigned another value (may be black) | |
#Minot changes from https://docs.opencv.org/3.4/d7/d4d/tutorial_py_thresholding.html | |
import cv2 | |
import numpy as np | |
from matplotlib import pyplot as plt | |
img = cv2.imread(r'E:\Opencv_Examples\goalkeeper.jpg',cv2.IMREAD_GRAYSCALE) | |
ret,thresh1 = cv2.threshold(img,127,255,cv2.THRESH_BINARY) | |
ret,thresh2 = cv2.threshold(img,127,255,cv2.THRESH_BINARY_INV) | |
ret,thresh3 = cv2.threshold(img,127,255,cv2.THRESH_TRUNC) | |
ret,thresh4 = cv2.threshold(img,127,255,cv2.THRESH_TOZERO) | |
ret,thresh5 = cv2.threshold(img,127,255,cv2.THRESH_TOZERO_INV) | |
titles = ['Original Image','BINARY','BINARY_INV','TRUNC','TOZERO','TOZERO_INV'] | |
images = [img, thresh1, thresh2, thresh3, thresh4, thresh5] | |
for i in range(6): | |
plt.subplot(2,3,i+1),plt.imshow(images[i],'gray') | |
plt.title(titles[i]) | |
plt.xticks([]),plt.yticks([]) | |
plt.show() | |
#cv2.calcBackProject | |
#=================== | |
#cv2.calcBackProject(). Its parameters are almost same as the cv2.calcHist() | |
#object histogram should be normalized before passing on to the backproject function | |
#One of input is histogram of object we want to find it | |
#cv2.merge | |
#========= | |
import cv2 | |
img = cv2.imread(r'E:\Opencv_Examples\goalkeeper.jpg',cv2.IMREAD_COLOR) | |
b,g,r = cv2.split(img) | |
cv2.imshow('b',b) | |
cv2.imshow('g',g) | |
cv2.imshow('r',r) | |
img2 = cv2.merge((b,g,g)) | |
cv2.imshow('bgg',img2) | |
img3 = cv2.merge((b,g,r)) | |
cv2.imshow('bgr',img3) | |
cv2.waitKey(0) | |
cv2.destroyAllWindows() | |
#cv2.bitwise_and | |
#=============== | |
#This includes bitwise AND, OR, NOT and XOR operations. | |
#They will be highly useful while extracting any part of the image | |
#cv2.bitwise_and | |
#cv2.bitwise_not | |
#https://docs.opencv.org/3.2.0/d0/d86/tutorial_py_image_arithmetics.html |
No comments:
Post a Comment