Files
2024-09-24 10:29:46 +08:00

30 lines
1.0 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# 1.创建一个Python脚本,并导入OpenCV库,import cv2
import cv2
# 2.使用cv2.imread()函数读取图像文件。例如,如果图像文件名为image.jpg,可以使用以下代码读取图像:
# image = cv2.imread('image.jpg')
# 确保图像文件与你的Python脚本在同一目录下,或者可以提供图像文件的完整路径。
image = cv2.imread('image.jpg')
# 3.可以使用cv2.imshow()函数显示图像:
# cv2.imshow('Image',image)
# cv2.waitKey(0)
# cv2.destroyAllWindows()
cv2.imshow('show_image', image)
cv2.waitKey(0)
cv2.destroyAllWindows()
# 4.对图像进行操作并进行颜色空间变换,如将图像由彩色图转换为灰度图、hsv空间等。
## convert to gray
image_gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
cv2.namedWindow('gray_image')
cv2.imshow('gray_image', image_gray)
cv2.waitKey(0)
cv2.destroyWindow('gray_image')
## convert to hsv
image_hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
cv2.namedWindow('hsv_image')
cv2.imshow('hsv_image', image_hsv)
cv2.waitKey(0)
cv2.destroyWindow('hsv_image')