Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

After-practise #37

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions Section #2 - Advanced/bitwise.py → Advanced/bitwise.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,27 @@
import cv2 as cv
import numpy as np

blank = np.zeros((400,400), dtype='uint8')
blank = np.zeros((400,400), dtype='uint8')#dtype='uint8' is a grayscale image

rectangle = cv.rectangle(blank.copy(), (30,30), (370,370), 255, -1)
circle = cv.circle(blank.copy(), (200,200), 200, 255, -1)

cv.imshow('Rectangle', rectangle)
cv.imshow('Circle', circle)

# bitwise AND --> intersecting regions
# bitwise AND --> intersecting regions (Appartient au cercle ET au rectangle)
bitwise_and = cv.bitwise_and(rectangle, circle)
cv.imshow('Bitwise AND', bitwise_and)

# bitwise OR --> non-intersecting and intersecting regions
bitwise_or = cv.bitwise_or(rectangle, circle)
bitwise_or = cv.bitwise_or(circle, rectangle)
cv.imshow('Bitwise OR', bitwise_or)

# bitwise XOR --> non-intersecting regions
bitwise_xor = cv.bitwise_xor(rectangle, circle)
cv.imshow('Bitwise XOR', bitwise_xor)

# bitwise NOT
# # bitwise NOT
bitwise_not = cv.bitwise_not(circle)
cv.imshow('Circle NOT', bitwise_not)

Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
img = cv.imread('../Resources/Photos/park.jpg')
cv.imshow('Park', img)

# plt.imshow(img)
# plt.show()
plt.imshow(img)
plt.show()

# BGR to Grayscale
gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
Expand Down
File renamed without changes.
26 changes: 13 additions & 13 deletions Section #2 - Advanced/histogram.py → Advanced/histogram.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@

blank = np.zeros(img.shape[:2], dtype='uint8')

# gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
# cv.imshow('Gray', gray)
gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
cv.imshow('Gray', gray)

mask = cv.circle(blank, (img.shape[1]//2,img.shape[0]//2), 100, 255, -1)

Expand All @@ -30,16 +30,16 @@

# Colour Histogram

plt.figure()
plt.title('Colour Histogram')
plt.xlabel('Bins')
plt.ylabel('# of pixels')
colors = ('b', 'g', 'r')
for i,col in enumerate(colors):
hist = cv.calcHist([img], [i], mask, [256], [0,256])
plt.plot(hist, color=col)
plt.xlim([0,256])

plt.show()
# plt.figure()
# plt.title('Colour Histogram')
# plt.xlabel('Bins')
# plt.ylabel('# of pixels')
# colors = ('b', 'g', 'r')
# for i,col in enumerate(colors):
# hist = cv.calcHist([img], [i], mask, [256], [0,256])
# plt.plot(hist, color=col)
# plt.xlim([0,256])

# plt.show()

cv.waitKey(0)
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,20 @@

import cv2 as cv
import numpy as np
import matplotlib.pyplot as plt


img = cv.imread('../Resources/Photos/park.jpg')
cv.imshow('Park', img)

blank = np.zeros(img.shape[:2], dtype='uint8')
# plt.imshow(img)
# plt.show()

blank = np.zeros(img.shape[:2], dtype='uint8')
b,g,r = cv.split(img)

# cv.imshow('Blue', b)

blue = cv.merge([b,blank,blank])
green = cv.merge([blank,g,blank])
red = cv.merge([blank,blank,r])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,38 @@

import cv2 as cv

# Read in an image
# # Read in an image
img = cv.imread('../Resources/Photos/park.jpg')
cv.imshow('Park', img)

# Converting to grayscale
gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
cv.imshow('Gray', gray)

# Blur


# Blur an image (Gaussian Blur)
blur = cv.GaussianBlur(img, (7,7), cv.BORDER_DEFAULT)
cv.imshow('Blur', blur)

# Edge Cascade
# Edge Cascade #Just like a filter in old phone cameras XD
canny = cv.Canny(blur, 125, 175)
cv.imshow('Canny Edges', canny)

# Dilating the image
dilated = cv.dilate(canny, (7,7), iterations=3)
# Dilating the image
dilated = cv.dilate(canny, (5,5), iterations=3)
cv.imshow('Dilated', dilated)

# Eroding
eroded = cv.erode(dilated, (7,7), iterations=3)
cv.imshow('Eroded', eroded)

# Resize
# # Resize
resized = cv.resize(img, (500,500), interpolation=cv.INTER_CUBIC)
cv.imshow('Resized', resized)

# Cropping
cropped = img[50:200, 200:400]
cv.imshow('Cropped', cropped)
# # Cropping
cropped = resized[100:250,100:150]
cv.imshow('Cropped', cropped)

cv.waitKey(0)
8 changes: 5 additions & 3 deletions Section #1 - Basics/contours.py → Basics/contours.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@
canny = cv.Canny(blur, 125, 175)
cv.imshow('Canny Edges', canny)

# ret, thresh = cv.threshold(gray, 125, 255, cv.THRESH_BINARY)
# cv.imshow('Thresh', thresh)
# # ret, thresh = cv.threshold(gray, 125, 255, cv.THRESH_BINARY)
# # cv.imshow('Thresh', thresh)

contours, hierarchies = cv.findContours(canny, cv.RETR_LIST, cv.CHAIN_APPROX_SIMPLE)
print(f'{len(contours)} contour(s) found!')

cv.drawContours(blank, contours, -1, (0,0,255), 1)

# (Blue, Green, Red)
cv.drawContours(blank, contours, -1, (100,0,0), 1)
cv.imshow('Contours Drawn', blank)

cv.waitKey(0)
12 changes: 7 additions & 5 deletions Section #1 - Basics/draw.py → Basics/draw.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,21 @@
cv.imshow('Blank', blank)

# 1. Paint the image a certain colour
blank[200:300, 300:400] = 0,0,255
blank[200:300, 300:400] = 0,0,255
cv.imshow('Green', blank)

#Blank.shape = (500,500,3)

# 2. Draw a Rectangle
cv.rectangle(blank, (0,0), (blank.shape[1]//2, blank.shape[0]//2), (0,255,0), thickness=-1)
cv.imshow('Rectangle', blank)
cv.rectangle(blank, (0,0), (blank.shape[1]//2, blank.shape[0]//2), (0,255,0), thickness=-1)#thickness=-1 will fill the rectangle
cv.imshow('Rectangle', blank)#(0,0) is the top left corner, (blank.shape[1]//2, blank.shape[0]//2) is the bottom right corner

# 3. Draw A circle
cv.circle(blank, (blank.shape[1]//2, blank.shape[0]//2), 40, (0,0,255), thickness=-1)
cv.circle(blank, (blank.shape[1]//2, blank.shape[0]//2), 40 , (0,0,255), thickness=-1)
cv.imshow('Circle', blank)

# 4. Draw a line
cv.line(blank, (100,250), (300,400), (255,255,255), thickness=3)
cv.line(blank, (100,250), (300,400), (255,255,255), thickness=1)
cv.imshow('Line', blank)

# 5. Write text
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

# Translation
def translate(img, x, y):
transMat = np.float32([[1,0,x],[0,1,y]])
transMat = np.float32([[1,0,x],[0,1,y]])
dimensions = (img.shape[1], img.shape[0])
return cv.warpAffine(img, transMat, dimensions)

Expand All @@ -17,22 +17,22 @@ def translate(img, x, y):
# x --> Right
# y --> Down

translated = translate(img, -100, 100)
translated = translate(img, 150, 100)
cv.imshow('Translated', translated)

# Rotation
def rotate(img, angle, rotPoint=None):
def rotate(img, angle, rotPoint=None): #rotPoint is the point around which the image will be rotated
(height,width) = img.shape[:2]

if rotPoint is None:
rotPoint = (width//2,height//2)

rotMat = cv.getRotationMatrix2D(rotPoint, angle, 1.0)
rotMat = cv.getRotationMatrix2D(rotPoint, angle, 1.0) #1.0 is the scale
dimensions = (width,height)

return cv.warpAffine(img, rotMat, dimensions)

rotated = rotate(img, -45)
rotated = rotate(img, 180)
cv.imshow('Rotated', rotated)

rotated_rotated = rotate(img, -90)
Expand All @@ -43,7 +43,7 @@ def rotate(img, angle, rotPoint=None):
cv.imshow('Resized', resized)

# Flipping
flip = cv.flip(img, -1)
flip = cv.flip(img, -1)#0 is vertical flip, 1 is horizontal flip, -1 is both
cv.imshow('Flip', flip)

# Cropping
Expand Down
File renamed without changes.
File renamed without changes.
10 changes: 6 additions & 4 deletions Section #3 - Faces/face_detect.py → Faces/face_detect.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,22 @@
import cv2 as cv

img = cv.imread('../Resources/Photos/group 1.jpg')
cv.imshow('Group of 5 people', img)
#cv.imshow('Group of 5 people', img)

gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
cv.imshow('Gray People', gray)
# gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
# cv.imshow('Gray People', gray)

haar_cascade = cv.CascadeClassifier('haar_face.xml')

faces_rect = haar_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=1)
faces_rect = haar_cascade.detectMultiScale(img, scaleFactor=1.1, minNeighbors=1)

print(f'Number of faces found = {len(faces_rect)}')

for (x,y,w,h) in faces_rect:
cv.rectangle(img, (x,y), (x+w,y+h), (0,255,0), thickness=2)

img = cv.resize(img, (int(img.shape[1]/2), int(img.shape[0]/2)))

cv.imshow('Detected Faces', img)


Expand Down
Loading