當前位置: 妍妍網 > 碼農

Python OpenCV入門到實戰:影像處理的全方位指南

2024-06-04碼農

OpenCV,全稱Open Source Computer Vision Library,是一個功能強大的開源電腦視覺庫。它支持多種程式語言,包括Python、C++、C#等,廣泛套用於影像處理、影像辨識、影像合成等多個領域。

「一、安裝」

pip install opencv-python

「二、使用」

「1.讀取並顯示圖片」

import cv2
# 開啟影像
image = cv2.imread('example.jpg')
# 顯示影像
cv2.imshow('Image', image)
cv2.waitKey(0)
cv2.destroyAllWindows()
# 保存影像
cv2.imwrite('output.jpg', image)

「2.影像變換」

import cv2
# 開啟影像
image = cv2.imread('example.jpg')
# 縮放影像
scaled_image = cv2.resize(image, (500500))
# 旋轉影像
rotated_image = cv2.rotate(image, cv2.ROTATE_90_CLOCKWISE)
# 裁剪影像
cropped_image = image[100:300100:300]
# 顯示變換後的影像
cv2.imshow('Scaled Image', scaled_image)
cv2.imshow('Rotated Image', rotated_image)
cv2.imshow('Cropped Image', cropped_image)
cv2.waitKey(0)
cv2.destroyAllWindows()

「3.影像增強」

import cv2
# 開啟影像
image = cv2.imread('example.jpg')
# 增強對比度
enhanced_image = cv2.convertScaleAbs(image, alpha=1.5, beta=0)
# 顯示增強後的影像
cv2.imshow('Enhanced Image', enhanced_image)
cv2.waitKey(0)
cv2.destroyAllWindows()

「4.影像尺寸與分辨率」

import cv2
#開啟影像
image = cv2.imread('example.jpg')
#調整影像尺寸
resized_image = cv2.resize(image, (500500))
#調整影像分辨率
downsampled_image = cv2.resize(image, (500500), interpolation=cv2.INTER_AREA)
#顯示調整後的影像
cv2.imshow('Resized Image', resized_image)
cv2.imshow('Downsampled Image', downsampled_image)
cv2.waitKey(0)
cv2.destroyAllWindows()

「三、實戰」

「1.人臉即時檢測」

https://github.com/abhishekpatelmc/face_mask_detection

「2.人像轉卡通」

https://github.com/AswinSampath1401/Pencil-and-Cartoon-Effects

「3.車道線檢測」

https://github.com/misbah4064/lane_detection

「4.手部姿態檢測」

https://github.com/misbah4064/hand_pose_detection