Created
March 8, 2021 20:06
-
-
Save yokeshrana/4bf60b8b857da42196470b55f70bcd19 to your computer and use it in GitHub Desktop.
Full Open CV Notes
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
enable=False | |
''' | |
Reading Image | |
''' | |
if enable: | |
import cv2 | |
img = cv2.imread("Resources/lena.png") | |
cv2.imshow("Picture",img) | |
cv2.waitKey(3000) # Define the time until it is visible | |
''' | |
Reading Video | |
''' | |
if enable: | |
import cv2 | |
vid = cv2.VideoCapture("Resources/test_video.mp4") | |
#since video is stream of pictures we have to use something else here | |
while True: | |
success,img=vid.read() | |
if success: | |
cv2.imshow("Video", img) | |
if cv2.waitKey(1) & 0xFF == ord('q'): | |
break | |
''' | |
Reading from webcam | |
''' | |
if enable: | |
import cv2 | |
vid = cv2.VideoCapture(0) # instead of file path we will use the id of the device (webcam) | |
#now we define some specific parameter for the webcam | |
vid.set(3,640) | |
vid.set(4,480) | |
#Rest Code is same as basic video read | |
while vid.isOpened(): # Used to check if camera is opened or not | |
success,img=vid.read() | |
if success: | |
cv2.imshow("Video",img) | |
if cv2.waitKey(1) & 0xFF == ord('q'): | |
break | |
if enable: | |
import cv2 | |
import numpy as np | |
img = cv2.imread("Resources/lena.png") | |
imgGray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) # converting to it gray scale image | |
cv2.imshow("GrayScale", imgGray) | |
# To Blur the image we will use Gaussian Blur to blur | |
imgBlur = cv2.GaussianBlur(imgGray,(7,7),0) # as a paramter kernel size is passed | |
cv2.imshow("GrayScaleBlur", imgBlur) | |
# To detect the edges we use the Canny Filter | |
imgCanny = cv2.Canny(imgGray,150,100) | |
cv2.imshow("Canny_Edge",imgCanny) | |
# Dialate the images | |
# inedge detection we didnt get the thick lines so what we can do is to increase thickness is dialate the image | |
kernel = np.ones((5,5), np.uint8) | |
imgDialted = cv2.dilate(imgCanny,kernel ,iterations=1) | |
cv2.imshow("Dialted Image ", imgDialted) | |
# Erode the image | |
imgEroded=cv2.erode(imgCanny,kernel,iterations=1) | |
cv2.imshow("Eroded Image ", imgCanny) | |
cv2.waitKey(0) | |
if enable: | |
import cv2 | |
import numpy as np | |
img = cv2.imread("Resources/lambo.PNG") | |
cv2.imshow("Image ", img) | |
print(img.shape) | |
''' | |
Resizing the image | |
''' | |
imgResizeinc = cv2.resize(img,(600,600)) | |
print(imgResizeinc.shape) | |
cv2.imshow("Resize Image Inc",imgResizeinc) | |
imgResizedec = cv2.resize(img,(300,300)) | |
print(imgResizedec.shape) | |
cv2.imshow("Resize Image Dec",imgResizedec) | |
''' | |
Cropping the image : | |
Since image is a matrix we can use the normal matrix operations to crop the image | |
''' | |
imgCrop = img[0:200,200:500] # start_height: end_height , start_width: end_width | |
cv2.imshow("Cropped Image",imgCrop) | |
cv2.waitKey(0) | |
if enable: | |
import cv2 | |
import numpy as np | |
img = np.zeros((512,512)) # gray scale image (can check by shape also ) | |
# To add the color make it 3 channel | |
img = np.zeros((512,512,3)) | |
#img[:]=255,0,0 | |
''' | |
To Draw the line ,Rectange ,Circle and put text on images | |
''' | |
cv2.line(img,(0,0),(300,300),(0,255,0),3) # start point ,end point and thickness | |
cv2.rectangle(img,(0,0),(250,350),(0,0,255),4) # start point ,end point and thickness(or can fill also fuly) | |
cv2.circle(img,(400,50),30,(255,255,0),5) #center point,radius ,color and thickness | |
cv2.putText(img,"OPENCV",(300,300),cv2.FONT_HERSHEY_DUPLEX,1,(0,255,0),3) | |
cv2.imshow("Image", img) | |
cv2.waitKey(0) | |
if enable: | |
import cv2 | |
import numpy as np | |
img = cv2.imread("Resources/cards.jpg") | |
height,width = img.shape[0],img.shape[1] | |
''' | |
Warp Perspective we try to get a particular card from multiple card in iamge | |
''' | |
cv2.imshow("Original Image",img) | |
pts1 = np.float32([[111, 219], [287, 188], [154, 482], [352, 440]]) | |
pts2 = np.float32([[0, 0], [width, 0], [0, height], [width, height]]) | |
matrix = cv2.getPerspectiveTransform(pts1, pts2) | |
imgOutput = cv2.warpPerspective(img, matrix, (width, height)) | |
cv2.imshow("Output", imgOutput) | |
cv2.waitKey(0) | |
if enable: | |
""" | |
Join Images | |
We will try to put all images in one window | |
""" | |
import cv2 | |
import numpy as np | |
#Function used to stack the images | |
def stackImages(scale, imgArray): | |
rows = len(imgArray) | |
cols = len(imgArray[0]) | |
rowsAvailable = isinstance(imgArray[0], list) | |
width = imgArray[0][0].shape[1] | |
height = imgArray[0][0].shape[0] | |
if rowsAvailable: | |
for x in range(0, rows): | |
for y in range(0, cols): | |
if imgArray[x][y].shape[:2] == imgArray[0][0].shape[:2]: | |
imgArray[x][y] = cv2.resize(imgArray[x][y], (0, 0), None, scale, scale) | |
else: | |
imgArray[x][y] = cv2.resize(imgArray[x][y], (imgArray[0][0].shape[1], imgArray[0][0].shape[0]), | |
None, scale, scale) | |
if len(imgArray[x][y].shape) == 2: imgArray[x][y] = cv2.cvtColor(imgArray[x][y], cv2.COLOR_GRAY2BGR) | |
imageBlank = np.zeros((height, width, 3), np.uint8) | |
hor = [imageBlank] * rows | |
hor_con = [imageBlank] * rows | |
for x in range(0, rows): | |
hor[x] = np.hstack(imgArray[x]) | |
ver = np.vstack(hor) | |
else: | |
for x in range(0, rows): | |
if imgArray[x].shape[:2] == imgArray[0].shape[:2]: | |
imgArray[x] = cv2.resize(imgArray[x], (0, 0), None, scale, scale) | |
else: | |
imgArray[x] = cv2.resize(imgArray[x], (imgArray[0].shape[1], imgArray[0].shape[0]), None, scale, | |
scale) | |
if len(imgArray[x].shape) == 2: imgArray[x] = cv2.cvtColor(imgArray[x], cv2.COLOR_GRAY2BGR) | |
hor = np.hstack(imgArray) | |
ver = hor | |
return ver | |
img = cv2.imread("Resources/lena.png") | |
imgGray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) | |
imgStack = stackImages(0.5, ([img, imgGray, img], [img, img, img])) | |
imgHor = np.hstack((img,img)) # horizontally stacking the images | |
imgVer = np.vstack((img,img)) # vertically stacking the images | |
#cv2.imshow("Horizontal",imgHor) | |
#cv2.imshow("Vertical",imgVer) | |
cv2.imshow("ImageStack", imgStack) | |
cv2.waitKey(0) | |
if enable: | |
import cv2 | |
import numpy as np | |
''' | |
Here we will try to detect colors in the image | |
''' | |
def empty(a): | |
pass | |
# Function used to stack the images | |
def stackImages(scale, imgArray): | |
rows = len(imgArray) | |
cols = len(imgArray[0]) | |
rowsAvailable = isinstance(imgArray[0], list) | |
width = imgArray[0][0].shape[1] | |
height = imgArray[0][0].shape[0] | |
if rowsAvailable: | |
for x in range(0, rows): | |
for y in range(0, cols): | |
if imgArray[x][y].shape[:2] == imgArray[0][0].shape[:2]: | |
imgArray[x][y] = cv2.resize(imgArray[x][y], (0, 0), None, scale, scale) | |
else: | |
imgArray[x][y] = cv2.resize(imgArray[x][y], (imgArray[0][0].shape[1], imgArray[0][0].shape[0]), | |
None, scale, scale) | |
if len(imgArray[x][y].shape) == 2: imgArray[x][y] = cv2.cvtColor(imgArray[x][y], cv2.COLOR_GRAY2BGR) | |
imageBlank = np.zeros((height, width, 3), np.uint8) | |
hor = [imageBlank] * rows | |
hor_con = [imageBlank] * rows | |
for x in range(0, rows): | |
hor[x] = np.hstack(imgArray[x]) | |
ver = np.vstack(hor) | |
else: | |
for x in range(0, rows): | |
if imgArray[x].shape[:2] == imgArray[0].shape[:2]: | |
imgArray[x] = cv2.resize(imgArray[x], (0, 0), None, scale, scale) | |
else: | |
imgArray[x] = cv2.resize(imgArray[x], (imgArray[0].shape[1], imgArray[0].shape[0]), None, scale, | |
scale) | |
if len(imgArray[x].shape) == 2: imgArray[x] = cv2.cvtColor(imgArray[x], cv2.COLOR_GRAY2BGR) | |
hor = np.hstack(imgArray) | |
ver = hor | |
return ver | |
path = 'Resources/lambo.png' | |
cv2.namedWindow("TrackBars") | |
cv2.resizeWindow("TrackBars", 640, 240) | |
cv2.createTrackbar("Hue Min", "TrackBars", 0, 179, empty) | |
cv2.createTrackbar("Hue Max", "TrackBars", 19, 179, empty) | |
cv2.createTrackbar("Sat Min", "TrackBars", 110, 255, empty) | |
cv2.createTrackbar("Sat Max", "TrackBars", 240, 255, empty) | |
cv2.createTrackbar("Val Min", "TrackBars", 153, 255, empty) | |
cv2.createTrackbar("Val Max", "TrackBars", 255, 255, empty) | |
while True: | |
img = cv2.imread(path) | |
#cv2.imshow("Original", img) | |
imgHSV = cv2.cvtColor(img, cv2.COLOR_BGR2HSV) | |
#cv2.imshow("HSV",imgHSV) | |
h_min = cv2.getTrackbarPos("Hue Min", "TrackBars") | |
h_max = cv2.getTrackbarPos("Hue Max", "TrackBars") | |
s_min = cv2.getTrackbarPos("Sat Min", "TrackBars") | |
s_max = cv2.getTrackbarPos("Sat Max", "TrackBars") | |
v_min = cv2.getTrackbarPos("Val Min", "TrackBars") | |
v_max = cv2.getTrackbarPos("Val Max", "TrackBars") | |
print(h_min, h_max, s_min, s_max, v_min, v_max) | |
lower = np.array([h_min, s_min, v_min]) | |
upper = np.array([h_max, s_max, v_max]) | |
mask = cv2.inRange(imgHSV, lower, upper) | |
#cv2.imshow("Mask",mask) | |
imgResult = cv2.bitwise_and(img, img, mask=mask) | |
#cv2.imshow("Image Result",imgResult) | |
# Instead of displaying all image seprate we can stack them up in single window | |
imgStack = stackImages(0.6,([img,imgHSV],[mask,imgResult])) | |
cv2.imshow("Result", imgStack) | |
cv2.waitKey(200) | |
if enable: | |
import cv2 | |
import numpy as np | |
path = 'Resources/shapes.png' | |
img = cv2.imread(path) | |
imgContour = img.copy() | |
def stackImages(scale, imgArray): | |
rows = len(imgArray) | |
cols = len(imgArray[0]) | |
rowsAvailable = isinstance(imgArray[0], list) | |
width = imgArray[0][0].shape[1] | |
height = imgArray[0][0].shape[0] | |
if rowsAvailable: | |
for x in range(0, rows): | |
for y in range(0, cols): | |
if imgArray[x][y].shape[:2] == imgArray[0][0].shape[:2]: | |
imgArray[x][y] = cv2.resize(imgArray[x][y], (0, 0), None, scale, scale) | |
else: | |
imgArray[x][y] = cv2.resize(imgArray[x][y], (imgArray[0][0].shape[1], imgArray[0][0].shape[0]), | |
None, scale, scale) | |
if len(imgArray[x][y].shape) == 2: imgArray[x][y] = cv2.cvtColor(imgArray[x][y], cv2.COLOR_GRAY2BGR) | |
imageBlank = np.zeros((height, width, 3), np.uint8) | |
hor = [imageBlank] * rows | |
hor_con = [imageBlank] * rows | |
for x in range(0, rows): | |
hor[x] = np.hstack(imgArray[x]) | |
ver = np.vstack(hor) | |
else: | |
for x in range(0, rows): | |
if imgArray[x].shape[:2] == imgArray[0].shape[:2]: | |
imgArray[x] = cv2.resize(imgArray[x], (0, 0), None, scale, scale) | |
else: | |
imgArray[x] = cv2.resize(imgArray[x], (imgArray[0].shape[1], imgArray[0].shape[0]), None, scale, | |
scale) | |
if len(imgArray[x].shape) == 2: imgArray[x] = cv2.cvtColor(imgArray[x], cv2.COLOR_GRAY2BGR) | |
hor = np.hstack(imgArray) | |
ver = hor | |
return ver | |
def getContours(img): | |
contours,hierarchy = cv2.findContours(img,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)[-2:] | |
for cnt in contours: | |
area = cv2.contourArea(cnt) | |
print(area) | |
cv2.drawContours(imgContour,cnt,-1,(255,0,0),3) | |
if area > 500: | |
cv2.drawContours(imgContour,cnt,-1,(255,0,0),3) | |
peri = cv2.arcLength(cnt,True) | |
print(peri) | |
approx = cv2.approxPolyDP(cnt,0.02*peri,True) | |
print(len(approx)) # 3 is triangle 4 can be square /rectnage anything >4 is circle | |
objCor = len(approx) | |
# Now we will create a bounding box around our detected object | |
x,y,w,h = cv2.boundingRect(approx) | |
# Now clasisifying the object | |
objectType="" | |
if objCor == 3:objectType="Triangle" | |
elif objCor == 4: | |
aspRatio = w/float(h) | |
if aspRatio > 0.95 and aspRatio <1.05 : objectType = "Sqaure" | |
else:objectType="Rectangle" | |
elif objCor>4:objectType="Circles" | |
cv2.rectangle(imgContour, (x, y), (x + w, y + h), (0, 255, 0), 2) # this will draw a rectannge over the detected shape | |
cv2.putText(imgContour,objectType, | |
(x+(w//2)-10,y+(h//2)-10), | |
cv2.FONT_HERSHEY_PLAIN, | |
0.6, | |
(0,0,0),2) | |
## First we will convert our image to gray scale and then we will determine the edges | |
imgGray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) | |
imgBlur = cv2.GaussianBlur(imgGray,(7,7),1) | |
imgCanny = cv2.Canny(imgBlur,50,50) #Used to detect images | |
#cv2.imshow("Original",img) | |
#cv2.imshow("Gray",imgGray) | |
#cv2.imshow("Blur",imgBlur) | |
getContours(imgCanny) | |
imgBlank = np.zeros_like(img) | |
imgStack = stackImages(0.6,([img,imgGray,imgBlur], | |
[imgCanny,imgContour,imgBlank])) | |
cv2.imshow("Stack",imgStack) | |
cv2.waitKey(0) | |
enable =True | |
if enable: | |
''' | |
Face Detection | |
-> Using method used by Viola and jones (real time object detection) | |
-> We will use lot of images with positive faces and negative faces and will train the cascade file to detect the faces | |
-> In Open CV we dont have to actually do this we can use trained cascade file to detect things | |
''' | |
import cv2 | |
faceCascade = cv2.CascadeClassifier("Resources/haarcascade_frontalface_default.xml") | |
img = cv2.imread("Resources/lena.png") | |
imgGray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) | |
faces = faceCascade.detectMultiScale(imgGray,1.1,4) | |
for (x,y,w,h) in faces: | |
cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2) | |
cv2.imshow("Result",img) | |
cv2.waitKey(0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment