Skip to content

Commit 733787c

Browse files
committed
add new opencv programe files and some sample test images
1 parent 5f650b4 commit 733787c

7 files changed

+159
-0
lines changed
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import cv2
2+
import numpy as np
3+
4+
img = cv2.imread('test_images/opencv-logo.png')
5+
imggray = cv2.cvtColor(img , cv2.COLOR_BGR2GRAY)
6+
ret , threshold = cv2.threshold(imggray , 127 , 255 ,0)
7+
8+
# contours : numpy array of (x,y) coordinates of boundries of object.
9+
# heirarchy : information about image topology
10+
contours , heirarchy = cv2.findContours(threshold , cv2.RETR_TREE , cv2.CHAIN_APPROX_NONE)
11+
12+
print(f"Number of Contours : {str(contours)}")
13+
14+
# contourIdx = -1 i.e draw all contours
15+
cv2.drawContours(img , contours , -1 , (0,255,0) , 5)
16+
17+
cv2.imshow("Original Image",img)
18+
cv2.imshow("Gray image",imggray)
19+
cv2.waitKey(0)
20+
cv2.destroyAllWindows()

‎Readme.md

+3
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222
* image_gradients.py
2323
* canny_edge_detection.py
2424
* image_pyramids_opencv.py
25+
* image_blending_using_pyramids_opencv.py
26+
* Find_and_draw_contours_using_opencv.py
27+
* basic_motion_detection_using_opencv.py
2528

2629
## Helpful Documentations⭐:
2730
* OpenCV : https://docs.opencv.org/master/
+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import cv2
2+
import numpy as np
3+
4+
cap = cv2.VideoCapture('test_images/vtest.avi')
5+
6+
ret , frame1 = cap.read()
7+
ret , frame2 = cap.read()
8+
while cap.isOpened():
9+
10+
# difference between first and second frame
11+
diff = cv2.absdiff(frame1,frame2)
12+
13+
# convert it into gray ( for finding contours )
14+
gray = cv2.cvtColor(diff , cv2.COLOR_BGR2GRAY)
15+
16+
# apply blur
17+
blur = cv2.GaussianBlur(gray , (5,5) , 0)
18+
19+
# find threshold
20+
_ , th = cv2.threshold(blur , 20 , 255 , cv2.THRESH_BINARY)
21+
22+
# dilate the images
23+
dilated = cv2.dilate(th, None , iterations=3)
24+
25+
# find contours
26+
contours , _ = cv2.findContours(dilated , cv2.RETR_TREE , cv2.CHAIN_APPROX_SIMPLE)
27+
28+
# draw contours
29+
# cv2.drawContours(frame1 , contours , -1 , (0,255,0) , 3)
30+
31+
# Make rectangle around contours
32+
for contour in contours:
33+
(x,y,w,h) = cv2.boundingRect(contour)
34+
35+
if cv2.contourArea(contour) < 900:
36+
continue
37+
38+
cv2.rectangle(frame1 ,(x,y) , (x+w,y+h) , (0,255,0) , 2)
39+
cv2.putText(frame1,'Status: Movement' , (10,40) , cv2.FONT_HERSHEY_SIMPLEX ,1 , (255,0,0), 3)
40+
41+
cv2.imshow('feed' , frame1)
42+
frame1 = frame2
43+
ret , frame2 = cap.read()
44+
45+
# again read the frame for only second frame2
46+
# that's how we calculate the difference between two frames
47+
48+
if cv2.waitKey(40) == 27:
49+
break
50+
51+
cap.release()
52+
cv2.destroyAllWindows()
+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
import cv2
2+
import numpy as np
3+
4+
apple = cv2.imread('test_images/apple.jpg')
5+
orange = cv2.imread('test_images/orange.jpg')
6+
7+
# print(apple.shape)
8+
# print(orange.shape)
9+
10+
# apple_orange = np.hstack((apple[: , :256] ,orange[: , 256:]))
11+
12+
13+
14+
# Gaussian pyramid for apple image
15+
apple_copy = apple.copy()
16+
17+
gp_apple = [apple_copy]
18+
19+
for i in range(6):
20+
apple_copy = cv2.pyrDown(apple_copy)
21+
gp_apple.append(apple_copy)
22+
23+
# Gaussian pyramid for Orange image
24+
orange_copy = orange.copy()
25+
26+
gp_orange = [orange_copy]
27+
28+
for i in range(6):
29+
orange_copy = cv2.pyrDown(orange_copy)
30+
gp_orange.append(orange_copy)
31+
32+
33+
34+
35+
36+
# Generate Laplacian pyramid for apple
37+
apple_copy = gp_apple[5]
38+
lp_apple = [apple_copy]
39+
40+
for i in range(5,0,-1):
41+
gaussian_extended = cv2.pyrUp(gp_apple[i])
42+
laplacian = cv2.subtract(gp_apple[i-1],gaussian_extended)
43+
lp_apple.append(laplacian)
44+
45+
46+
# Generate Laplacian pyramid for orange
47+
orange_copy = gp_orange[5]
48+
lp_orange = [orange_copy]
49+
50+
for i in range(5,0,-1):
51+
gaussian_extended = cv2.pyrUp(gp_orange[i])
52+
laplacian = cv2.subtract(gp_orange[i-1],gaussian_extended)
53+
lp_orange.append(laplacian)
54+
55+
56+
57+
58+
59+
# Now add left and right halvas of images in each level.
60+
apple_orange_pyramid = []
61+
n = 0
62+
for apple_lap , orange_lap in zip(lp_apple, lp_orange):
63+
n += 1
64+
rows , cols , ch = apple_lap.shape
65+
laplacian = np.hstack((apple_lap[: , 0:int(cols/2)] ,orange_lap[: , int(cols/2):]))
66+
apple_orange_pyramid.append(laplacian)
67+
68+
69+
70+
71+
72+
# Now Reconsturct the image
73+
apple_orange_reconstruct = apple_orange_pyramid[0]
74+
for i in range(1,6):
75+
apple_orange_reconstruct = cv2.pyrUp(apple_orange_reconstruct)
76+
apple_orange_reconstruct = cv2.add(apple_orange_pyramid[i] , apple_orange_reconstruct)
77+
78+
79+
cv2.imshow('apple',apple)
80+
cv2.imshow('orange',orange)
81+
cv2.imshow('apple_orange_reconstruct',apple_orange_reconstruct)
82+
83+
cv2.waitKey(0)
84+
cv2.destroyAllWindows()

‎test_images/apple.jpg

50.5 KB
Loading

‎test_images/orange.jpg

48.9 KB
Loading

‎test_images/vtest.avi

7.75 MB
Binary file not shown.

0 commit comments

Comments
 (0)