Skip to content

Commit a048125

Browse files
fixing previous code and adding more example
1 parent 8e14679 commit a048125

22 files changed

+47576
-0
lines changed

11. Optical Flow/optical_flow.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import numpy as np
2+
import cv2
3+
4+
cap = cv2.VideoCapture('motion.avi')
5+
6+
ret, frame = cap.read()
7+
gs_im0 = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
8+
points_prev = cv2.goodFeaturesToTrack(gs_im0, 100, 0.03, 9.0, False)
9+
10+
while(cap.isOpened()):
11+
ret, frame = cap.read()
12+
13+
gs_im1 = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
14+
# Call tracker.
15+
points, st, err = cv2.calcOpticalFlowPyrLK(gs_im0, gs_im1, points_prev, None, (3,3))
16+
17+
for i,p in enumerate(points):
18+
a,b = p.ravel()
19+
frame = cv2.circle(frame,(a,b),3,(255,255,255),-1)
20+
21+
cv2.imshow('frame',frame)
22+
points_prev = points
23+
gs_im0 = gs_im1
24+
if cv2.waitKey(1) & 0xFF == ord('q'):
25+
break
26+
27+
cap.release()
28+
cv2.destroyAllWindows()

12. Blob Detection/blobDetection.py

Whitespace-only changes.

13. contouring/README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Shape Contouring
2+
3+
Contouring is like drawing an outline along the boundary of an object. In OpenCV this will be achieved with 2 processes:
4+
**Thresholding** and **Contouring**.
5+
6+
* Thresholding allows us to filter the object
7+
* Contouring lets us outline/mark the boundary of the object
8+
9+
## Result
10+
**Original** ![](https://github.com/Pranjalmishra30/rep.1/blob/master/Contouring/Data/Shapes.png) **Contoured** ![](https://github.com/Pranjalmishra30/openCV-Rep/blob/master/Mini-Projects/ShapeContouring/Shape_Detected.png)
11+
12+
## Refrences
13+
1. [Thresholding](https://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_imgproc/py_thresholding/py_thresholding.html)
14+
2. [Contours](https://docs.opencv.org/trunk/d4/d73/tutorial_py_contours_begin.html)

13. contouring/Shape_detection.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import cv2
2+
3+
img = cv2.imread('./Media/Shapes.png')
4+
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
5+
blur = cv2.GaussianBlur(gray,(11,11),0) #The values need to be >1 and odd
6+
7+
# creating binary thresholding
8+
ret, th = cv2.threshold(blur,220,255,cv2.THRESH_BINARY_INV) # Inverse Binary thresholding technique
9+
10+
# finding and drawing contour
11+
(cnts,_) = cv2.findContours(th.copy(),cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
12+
cv2.drawContours(img,cnts,-1,(0,0,0),2)
13+
14+
cv2.imshow('image',img)
15+
cv2.imwrite("./Media/Shape_Detected.png",img) # Save the contoured image
16+
cv2.waitKey(0)
17+
cv2.destroyAllWindows()

13. contouring/countouring.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import cv2
2+
import matplotlib.pyplot as plt
3+
4+
# read the image
5+
image = cv2.imread('./Media/thumbs_up_down.jpg')
6+
# convert to RGB
7+
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
8+
# convert to grayscale
9+
gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
10+
# create a binary thresholded image
11+
_, binary = cv2.threshold(gray, 225, 255, cv2.THRESH_BINARY_INV)
12+
13+
# find the contours from the thresholded image
14+
contours, hierarchy = cv2.findContours(binary, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
15+
# draw all contours
16+
image = cv2.drawContours(image, contours, -1, (0, 255, 0), 2)
17+
# show the image with the drawn contours
18+
plt.imshow(image)
19+
#plt.imsave('./Media/thumbs_up_down_countour.jpg', image)
20+
plt.show()
21+
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import cv2
2+
3+
cap = cv2.VideoCapture(0)
4+
5+
while True:
6+
cam, frame = cap.read()
7+
8+
if cam is True:
9+
# convert frame into grayscale
10+
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
11+
12+
# create a binary threshold
13+
_, binary = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)
14+
15+
contours, hierarchy = cv2.findContours(binary, cv2.RETR_TREE,
16+
cv2.CHAIN_APPROX_SIMPLE)
17+
18+
image = cv2.drawContours(image=frame, contours= contours,
19+
contourIdx=-1, color=(128, 0, 0), thickness=2)
20+
21+
cv2.imshow("live-contour-detector", image)
22+
23+
if cv2.waitKey(1) == ord('q'):
24+
break
25+
26+
cap.release()
27+
cv2.destroyAllWindows()

0 commit comments

Comments
 (0)