LAB01
LAB01
VÓI OPENCV
import numpy
as np import
cv2
import
imutils
import
glob
import os
from PIL import
Image from io
import BytesIO
import requests
import matplotlib.pyplot as plt
# folder path =
"/content/gdrive/MyDrive/nhanDienDT/opencv/anhChan
Dung/acd*"
# # lay danh sách các têp anh trong thu muc và sap
xêp chúng theo thú tu can thiêt
# files = sorted(glob.glob(folder_path))
image_folder
='/content/gdrive/MyDrive/nhanDienDT/opencv/anhCh
os.path.join(image_folder,
image_name) img =
cv2.imread(image_path)
# Hien thi
hînh ành
plt.show()
Xoay ành
# output folder =
'/content/gdrive/MyDrive/nhanDienDT/opencv/AnhCh
anDung_rotate/'
os.path.join(output_folder, image_name)
img = cv2.imread(image_path)
# Xoay anh 45 dé
rotated 45 = imutils.rotate(img, 45)
rotated 45 pil
Image.fromarray(cv2.cvtColor(rotated_45,
cv2.COLOR BGR2RGB))
# rotated
45_pil.save(output_path.replace(".jpg", "
rotated_45.jpg"))
Image.fromarray(cv2.cvtColor(rotated_minus_45,
cv2.COLOR BGR2RGB))
# rotated minus
45_pil.save(output_path.replace(".jpg", " rotated -
45.jpg"))
plt.figure()
plt.imshow(rotated minus 45
pil) plt.title("xoay phài
(-45 dõ)") plt.show()
image_folder
'/content/gdrive/MyDrive/nhanDienDT/opencv/im
plt.figure()
plt.imshow(img,
cmap='gray')
plt.axis('off')
plt.show()
# Hien thi
ành canh
plt.figure()
plt.imshow(edges_pil,
cmap='gray')
plt.axis('off')
plt.show()
'/content/gdrive/MyDrive/nhanDienDT/opencv/Bien
img =
cv2.imread(image_pat
h) print(img.shape)
gray = cv2.cvtColor(img,
cv2.COLOR BGR2GRAY) thres =
cv2.adaptiveThreshold(gray, 255,
cv2.ADAPTIVE THRESH GAUSSIAN C, cv2.THRESH
BINARY_INV, 31, 10)
contours = cv2.findContours(thres,
cv2.RETR LIST, cv2.CHAIN APPROX
SIMPLE)
contours = imutils.grab contours(contours)
contours = sorted(contours, key=cv2.contourArea,
reverse=False)
cv2.imwrite("/content/gdrive/MyDrive/nhanDienDT/
opencv/.png".format(number
), crop)
print("Number of Contours found =
" + str(number)) # Hién thi ânh
Threshold
plt.figure()
plt.imshow(thres,
cmap='gray')
plt.axis('off')
plt.show()