利用Python-OpenCV实现图像的缩放,旋转,标注,分割和平滑等操作
时间: 2024-03-24 19:41:28 浏览: 104
好的,下面是利用Python-OpenCV实现图像的缩放、旋转、标注、分割和平滑等操作的示例代码:
```
import cv2
import numpy as np
# 读取图像
img = cv2.imread('test.jpg')
# 缩放图像
resized = cv2.resize(img, None, fx=0.5, fy=0.5, interpolation=cv2.INTER_LINEAR)
# 旋转图像
(h, w) = img.shape[:2]
center = (w // 2, h // 2)
M = cv2.getRotationMatrix2D(center, 45, 1.0)
rotated = cv2.warpAffine(img, M, (w, h))
# 标注图像
cv2.putText(img, 'Test Image', (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2)
# 分割图像
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(gray, 127, 255, 0)
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
cv2.drawContours(img, contours, -1, (0, 255, 0), 3)
# 平滑图像
blurred = cv2.GaussianBlur(img, (11, 11), 0)
# 显示结果
cv2.imshow('Original', img)
cv2.imshow('Resized', resized)
cv2.imshow('Rotated', rotated)
cv2.imshow('Segmented', thresh)
cv2.imshow('Blurred', blurred)
cv2.waitKey(0)
cv2.destroyAllWindows()
```
在上述代码中,我们首先读取了一张名为'test.jpg'的图像,并分别进行了缩放、旋转、标注、分割和平滑等操作。其中,缩放和旋转操作通过cv2.resize()和cv2.warpAffine()函数实现;标注操作通过cv2.putText()函数实现;分割操作通过cv2.findContours()函数实现;平滑操作通过cv2.GaussianBlur()函数实现。
最后,我们通过cv2.imshow()函数将结果显示出来,并通过cv2.waitKey()函数等待用户按下任意键。
阅读全文
相关推荐


















