BÀI TH C HÀNH SỐỐ 2
H và tên: Nguyêễn Đoàn Hoàng H i
Mã sv: 19574802010069
Câu 1:
import cv2
def nothing( ):x
pass
image = cv2.imread( )'D://anhTest//Anh01.jpg'
cv2.namedWindow( )'image'
cv2.createTrackbar( nothing)'Tuong phan', 'image', 10 30, ,
cv2.createTrackbar( nothing)'Do sang', 'image', 0 100, ,
while True:
alpha = cv2.getTrackbarPos( ) / 'Tuong phan', 'image' 10
beta = cv2.getTrackbarPos( )'Do sang', 'image'
adjusted = cv2.convertScaleAbs(image =beta), , alpha=alpha beta
cv2.imshow('image', adjusted)
) & == if cv2.waitKey(1 0xFF ord( ):'s'
cv2.imwrite('D://anhTest//Anh01-new.jpg', adjusted)
break
if cv2.waitKey( ( ):1) & == 0xFF ord 'q'
break
cv2.destroyAllWindows()
Câu 2:
import cv2
def nothing( ):x
pass
# đọc ảnh và chuyển sang ảnh xám
img = cv2.imread( )'D://anhTest//Anh01.jpg'
gray = cv2.cvtColor(img cv2.COLOR_BGR2GRAY),
cv2.namedWindow( )'image'
cv2.createTrackbar( nothing)'Gia tri', 'image', 100 255, ,
while True:
threshold_value = cv2.getTrackbarPos( )'Gia tri', 'image'
max_value = 255 # giá trị cực đại
binary = cv2.threshold(gray, threshold_value, max_value,
cv2.THRESH_BINARY)[ ]1
cv2.imshow( binary)'image',
) & == if cv2.waitKey(1 0xFF ord( ):'s'
cv2.imwrite( binary)'D://anhTest//Anh01-new-Bai2.jpg',
break
if cv2.waitKey( ( ):1) & == 0xFF ord 'a'
inverted = cv2.bitwise_not(binary)
cv2.imwrite( inverted)'D://anhTest//Anh01-new-Bai2-amban.jpg',
break
cv2.destroyAllWindows()
Câu 3:
import cv2
def nothing( ):x
pass
img = cv2.imread( )'D://anhTest//Anh01.jpg'
cv2.namedWindow( )'image'
cv2.createTrackbar( nothing)'Thu phong', 'image', 50 200, ,
h ], w = img.shape[:2
while True:
scale_percent = cv2.getTrackbarPos( )'Thu phong', 'image'
new_h = (h * scale_percent / )int 100
new_w = (w * scale_percent / )int 100
dim = (new_w new_h),
new_img = cv2.resize(img dim),
cv2.imshow( new_img)'New img',
) & == if cv2.waitKey(1 0xFF ord( ):'q'
break
cv2.destroyAllWindows()
Câu 4:
import cv2
def nothing( ):x
pass
img = cv2.imread( )'D://anhTest//Anh01.jpg'
# Tọa độ góc trên trái và kích thước vùng cần cắt
start_x = 0
start_y = 0
width = ))int input( ("Nhap vao chieu rong: "
height = ))int input( ("Nhap vao chieu cao: "
# Cắt ảnh
cropped_img = img[start_y:width start_x:height],
cv2.imshow( cropped_img)'Cat anh theo chieu cao, rong',
cv2.waitKey( )0
end_x = ))int input( ("Nhap vao toa do x: "
end_y = ))int input( ("Nhap vao toa do y: "
cropped_img = img[start_y:end_y start_x:end_x],
cv2.imshow( cropped_img)'Cat anh theo toa do',
cv2.waitKey( )0
Câu 5:
import cv2
import as numpy np
def nothing( ):x
pass
image = cv2.imread( )'D://anhTest//Anh01.jpg'
rows cols, , _ = image.shape
cv2.namedWindow( )'image'
cv2.createTrackbar( nothing)'tx' 'image', , 100 200, ,
cv2.createTrackbar( nothing)'ty' 'image', , 100 200, ,
while True:
tx = cv2.getTrackbarPos( ) - 'tx', 'image' 100
100ty = cv2.getTrackbarPos( ) - 'ty', 'image'
M1 = np.array([[1, 0, , , tx] [0 1, , ty]] dtype=np.float32)
tran1 = cv2.warpAffine(image rows)), M1, (cols,
cv2.imshow( tran1)'Dich chuyen',
) & == if cv2.waitKey(1 0xFF ord( ):'q'
break
cv2.destroyAllWindows()
Câu 6:
import cv2
def nothing( ):x
pass
image = cv2.imread( )'D://anhTest//Anh01.jpg'
w ], h = image.shape[:2
cv2.namedWindow( )'image'
cv2.createTrackbar( nothing)'tx' 'image', , 0 360, ,
while True:
tx = cv2.getTrackbarPos( )'tx', 'image'
M = cv2.getRotationMatrix2D((w / h / )2, 2), tx, 1
rotated = cv2.warpAffine(image h)), M, (w,
cv2.imshow( rotated)'image',
) & == if cv2.waitKey(1 0xFF ord( ):'q'
break
cv2.destroyAllWindows()
Câu 8:
import cv2
def nothing( ):x
pass
image = cv2.imread( )'D://anhTest//Anh01.jpg'
w ], h = image.shape[:2
cv2.namedWindow( )'image'
cv2.createTrackbar( nothing)'Chon gia tri', 'image', 0 6, ,
cv2.putText(image ( ) cv2.FONT_HERSHEY_SIMPLEX, , 'van ban tren anh' 50 50, , , 1,
( ) )0 0 255, , , 2
cv2.line(image ( ) ( ) ( ) ), 0 0, , 100 100, , 0 255 0, , , 2
cv2.arrowedLine(image ( ) ( ) ( ) ), 100 0, , 100 100, , 0 255 0, , , 2
cv2.circle(image ( ) ( ) = ), 100 100, , 50, 0 0 255, , , thickness 2
cv2.ellipse(image ( ) ( ) ( ), 250 250, , 100 50, , 30 0 180, , , 255 0 0, , ,
thickness= )2
cv2.rectangle(image ( ) ( ) ( ) = ), 150 150, , 400 300, , 0 0 255, , , thickness 2
cv2.imshow( image)'image',
cv2.waitKey( )0
cv2.destroyAllWindows()

Preview text:

BÀI TH C HÀNH SỐỐ 2 Ự
Họ và tên: Nguyêễn Đoàn Hoàng H i ả Mã sv: 19574802010069 Câu 1: import cv2 def nothing(x): pass image = cv2.imread( ) 'D://anhTest//Anh01.jpg' cv2.namedWindow('image')
cv2.createTrackbar('Tuong phan', 'image', 10, 30, nothing)
cv2.createTrackbar('Do sang', 'image', 0, 100, nothing) while True:
alpha = cv2.getTrackbarPos('Tuong phan', 'image') / 10
beta = cv2.getTrackbarPos('Do sang', 'image')
adjusted = cv2.convertScaleAbs(image, alpha=alpha, beta=beta) cv2.imshow('image', adjusted)
if cv2.waitKey(1) & 0xFF == ord('s'):
cv2.imwrite('D://anhTest//Anh01-new.jpg', adjusted) break
if cv2.waitKey(1) & 0xFF == ord('q'): break cv2.destroyAllWindows() Câu 2: import cv2 def nothing(x): pass
# đọc ảnh và chuyển sang ảnh xám img = cv2.imread( ) 'D://anhTest//Anh01.jpg'
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) cv2.namedWindow('image')
cv2.createTrackbar('Gia tri', 'image', 100, 255, nothing) while True:
threshold_value = cv2.getTrackbarPos('Gia tri', 'image')
max_value = 255 # giá trị cực đại
binary = cv2.threshold(gray, threshold_value, max_value, cv2.THRESH_BINARY)[1] cv2.imshow('image', binary)
if cv2.waitKey(1) & 0xFF == ord('s'):
cv2.imwrite('D://anhTest//Anh01-new-Bai2.jpg', binary) break
if cv2.waitKey(1) & 0xFF == ord('a'):
inverted = cv2.bitwise_not(binary)
cv2.imwrite('D://anhTest//Anh01-new-Bai2-amban.jpg', inverted) break cv2.destroyAllWindows() Câu 3: import cv2 def nothing(x): pass img = cv2.imread( ) 'D://anhTest//Anh01.jpg' cv2.namedWindow('image')
cv2.createTrackbar('Thu phong', 'image', 50, 200, nothing) h, w = img.shape[:2] while True:
scale_percent = cv2.getTrackbarPos('Thu phong', 'image')
new_h = int(h * scale_percent / 100)
new_w = int(w * scale_percent / 100) dim = (new_w, new_h)
new_img = cv2.resize(img, dim)
cv2.imshow('New img', new_img)
if cv2.waitKey(1) & 0xFF == ord('q'): break cv2.destroyAllWindows() Câu 4: import cv2 def nothing(x): pass img = cv2.imread( ) 'D://anhTest//Anh01.jpg'
# Tọa độ góc trên trái và kích thước vùng cần cắt start_x = 0 start_y = 0
width = int(input("Nhap vao chieu rong: "))
height = int(input("Nhap vao chieu cao: ")) # Cắt ảnh
cropped_img = img[start_y:width, start_x:height]
cv2.imshow('Cat anh theo chieu cao, rong', cropped_img) cv2.waitKey(0)
end_x = int(input("Nhap vao toa do x: "))
end_y = int(input("Nhap vao toa do y: "))
cropped_img = img[start_y:end_y, start_x:end_x]
cv2.imshow('Cat anh theo toa do', cropped_img) cv2.waitKey(0) Câu 5: import cv2 import numpy as np def nothing(x): pass image = cv2.imread( ) 'D://anhTest//Anh01.jpg' rows, cols, _ = image.shape cv2.namedWindow('image')
cv2.createTrackbar('tx', 'image', 100, 200, nothing)
cv2.createTrackbar('ty', 'image', 100, 200, nothing) while True:
tx = cv2.getTrackbarPos('tx', 'image') - 100
ty = cv2.getTrackbarPos('ty', 'image' 100 ) -
M1 = np.array([[1, 0, tx], [0, 1, ty]], dtype=np.float32)
tran1 = cv2.warpAffine(image, M1, (cols, rows))
cv2.imshow('Dich chuyen', tran1)
if cv2.waitKey(1) & 0xFF == ord('q'): break cv2.destroyAllWindows() Câu 6: import cv2 def nothing(x): pass image = cv2.imread( ) 'D://anhTest//Anh01.jpg' w, h = image.shape[:2] cv2.namedWindow('image')
cv2.createTrackbar('tx', 'image', 0, 360, nothing) while True:
tx = cv2.getTrackbarPos('tx', 'image')
M = cv2.getRotationMatrix2D((w / 2, h / 2), tx, 1)
rotated = cv2.warpAffine(image, M, (w, h)) cv2.imshow('image', rotated)
if cv2.waitKey(1) & 0xFF == ord('q'): break cv2.destroyAllWindows() Câu 8: import cv2 def nothing(x): pass image = cv2.imread( ) 'D://anhTest//Anh01.jpg' w, h = image.shape[:2] cv2.namedWindow('image')
cv2.createTrackbar('Chon gia tri', 'image', 0, 6, nothing)
cv2.putText(image, 'van ban tren anh', (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2)
cv2.line(image, (0, 0), (100, 100), (0, 255, 0), 2)
cv2.arrowedLine(image, (100, 0), (100, 100), (0, 255, 0), 2)
cv2.circle(image, (100, 100), 50, (0, 0, 255), thickness=2)
cv2.ellipse(image, (250, 250), (100, 50), 30, 0, 180, (255, 0, 0), thickness=2)
cv2.rectangle(image, (150, 150), (400, 300), (0, 0, 255), thickness=2) cv2.imshow('image', image) cv2.waitKey(0) cv2.destroyAllWindows()