Professional Documents
Culture Documents
1. THỰC HÀNH
import cv2
webcam = cv2.VideoCapture(0)
while True:
ret, webcam_frame = webcam.read()
cv2.imshow('my webcam', webcam_frame)
webcam.release()
cv2.destroyAllWindows()
import cv2
img_leaf = cv2.imread('green_leaf.jpg',cv2.IMREAD_GRAYSCALE)
img_leaf = cv2.cvtColor(img_leaf, cv2.COLOR_GRAY2BGR)
img_bug = cv2.imread('ladybug.jpg', cv2.IMREAD_COLOR)
img_bug_resized = cv2.resize(img_bug, (100,100))
cv2.waitKey(0)
cv2.destroyAllWindows()
2. BÀI TẬP
Bài 1: Tìm hiểu các hàm trong thư viện OpenCV. (1đ)
2
Thực hành Nhập Môn Kỹ Thuật (2023)
3
Thực hành Nhập Môn Kỹ Thuật (2023)
4
Thực hành Nhập Môn Kỹ Thuật (2023)
Bài 2: Viết chương trình xử lý ảnh phát hiện nắp chai trong nhà máy sản xuất nước tinh
khiết. (4đ)
import sys
5
Thực hành Nhập Môn Kỹ Thuật (2023)
import cv2
import numpy as np
def main(argv):
default_file = 'bottle.jpg'
filename = argv[0] if len(argv) > 0 else default_file
src = cv2.imread(cv2.samples.findFile(filename), cv2.IMREAD_COLOR)
if src is None:
print ('Error opening image!')
print ('Usage: hough_circle.py [image_name -- default ' + default_file + '] \n')
return -1
rows = gray.shape[0]
circles = cv2.HoughCircles(gray, cv2.HOUGH_GRADIENT, 1, rows / 8,
param1=100, param2=30,
minRadius=70, maxRadius=79)
if circles is not None:
circles = np.uint16(np.around(circles))
for i in circles[0, :]:
center = (i[0], i[1])
cv2.circle(src, center, 1, (0, 100, 100), 3)
radius = i[2]
cv2.circle(src, center, radius, (255, 0, 255), 3)
if __name__ == "__main__":
main(sys.argv[1:])