You are on page 1of 6

Thực hành Nhập Môn Kỹ Thuật (2023)

Trường Đại học Khoa học Tự nhiên TP.HCM


Khoa Điện tử - Viễn thông

BÁO CÁO THỰC HÀNH PYTHON – BÀI 10

Lớp MSSV Họ và tên


23DVT1 23200080 Nguyễn Hạ Giang

1. THỰC HÀNH

Bài 1: Xử lý video đầu vào. (2đ)

import cv2

webcam = cv2.VideoCapture(0)
while True:
ret, webcam_frame = webcam.read()
cv2.imshow('my webcam', webcam_frame)

gray = cv2.cvtColor(webcam_frame, cv2.COLOR_BGR2GRAY)


cv2.imshow('my gray', gray)

if cv2.waitKey(1) & 0xFF == ord('q'):


break

webcam.release()
cv2.destroyAllWindows()

Bài 2: Vận dụng kiến thức ROI, xử lý 2 bức ảnh. (3đ)


1
Thực hành Nhập Môn Kỹ Thuật (2023)

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))

img_leaf[310:410, 100:200] = img_bug_resized


cv2.imshow('bug on leaf', img_leaf)

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)

Tên Hàm Cú pháp Chức năng


Pixel value img[100,100]
Accessing only blue
img[100,100,0]
pixel
Modifying A Pixel img[100,100] = [255,255,255]
Better pixel accessing img.item(10,10,2)
Better pixel modifying img.itemset((10,10,2),100)
Access image
img.shape
properties
Total number of pixels img.size
Image datatype img.dtype
Truy Xuất Và Hiệu
Getting ROI ball = img[280:340, 330:390]
Chỉnh Pixel
Setting ROI img[273:333, 100:160] = ball
b,g,r = cv2.split(img)
Split Channels
b = img[:,:,0]
Making Borders for cv2.copyMakeBorder(img1,10- ,10,10,
Images 10,cv2.BORDER_REPLICATE)
cv2.BORDER_CONSTANT
cv2.BORDER_REFLECT
Border type cv2.BORDER_REFLECT_101
cv2.BORDER_REPLICATE
cv2.BORDER_WRAP

Tên Hàm Cú pháp Chức năng


Image Addition print cv2.add(x,y) #
(OPENCV) 250+10 = 260 => 255
print x+y # 250+10 = 260
Image Addition (Numpy)
% 256 = 4
dst = cv2.addWeighted
Image Alpha Blending
(img1,- 0.7,img2,0.3,0) Phép toán số học
img1_bg =
Bitwise AND v2.bitwise_and-
(roi,roi,mask = mask_inv)
mask_inv=cv2.bitwise_not-
Bitwise NOT
(mask)

3
Thực hành Nhập Môn Kỹ Thuật (2023)

Tên Hàm Cú pháp Chức năng


erosion = cv2.erode(img, k -
Erosion
ernel,iterations = 1)
dilation = cv2.dilate(img,-
Dilate
kernel,iterations = 1)
opening = cv2.morphologyE-
Opening x(img, cv2.MORPH_OPEN,
kernel)
closing = cv2.morphologyE-
Closing x(img, cv2.MORPH_CLOSE,
kernel)
gradient = cv2.morphologyE-
Morphological
x(img,cv2.MORPH_GRADIENT,
Gradient Phép biến đổi hình
kernel)
thái
tophat = cv2.morphologyE-
Top Hat x(img, cv2.MORPH_TOPHAT,
kernel)
blackhat = cv2.morphologyE-
Black Hat x(img,cv2.MORPH_BLACKHAT,
kernel)
cv2.getStructuringElement(-
cv2.MORPH_RECT,(5,5))
Create Struct-uring cv2.getStructuringElement(-
Elements cv2.MORPH_ELLIPSE,(5,5))
cv2.getStructuringElement(-
cv2.MORPH_CROSS,(5,5))

Tên Hàm Cú pháp Chức năng


circles = cv.HoughCircles
(image, method, dp,
Hough Circle minDist[, Hough Circle
circles[, param1[, param2[,
minRadius[, maxRadius]]]]])

4
Thực hành Nhập Môn Kỹ Thuật (2023)

Tên Hàm Cú pháp Chức năng


List Colorspace Flags flags = [i for i in dir(cv2)
(150+) if i.startswith('COLOR_')
img_gray =
Convert to Gray cv2.cvtColor(img, cv2.
COLOR_BGR2GRAY)
hsv = cv2.cvtColor(img,
Convert to HSV
cv2. COLOR_BGR2HSV)
lower_blue = np.array([11-
0,50,50])
upper_blue = np.array([13-
Thay đổi không gian
0,255,255])
Track Blue (color) màu
mask = cv2.inRange(hsv,
Object
lower_blue, upper_blue)
res =
cv2.bitwise_and(frame-,fram
e, mask= mask)
green = np.uint8([[[0,255,0
]]])
Find HSV Color hsv_green = cv2.cvtColor
(green,cv2.COLOR_BGR2HS
V)

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

gray = cv2.cvtColor(src, cv2.COLOR_BGR2GRAY)


gray = cv2.medianBlur(gray, 5)

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)

cv2.imshow("detected circles", src)


return 0

if __name__ == "__main__":
main(sys.argv[1:])

You might also like