系列文章目录
前言
提供一张图片,如何截剪其中的某一部分的区域?
我的思路是分两步:
1. 找到要裁剪区域的坐标
2. 根据图片坐标进行剪切,并保存
一、获取感兴趣图片坐标
import cv2
from PIL import Image
def getCoordinate(img):
rectangle = []
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # 灰度图
ret, binary = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY|cv2.THRESH_OTSU) # 二值化
element3 = cv2.getStructuringElement(cv2.MORPH_RECT, (8, 8)) # 设置膨胀和腐蚀操作
dilation = cv2.dilate(binary, element3, iterations=1) # 膨胀一次,让轮廓突出
contours, hierarchy = cv2.findContours(dilation, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_TC89_L1) # 检测轮廓
cv2.drawContours(img, contours, -1, (0, 0, 255), 3) # 参数值为1, 给contours[1]绘制轮廓。 -1: 给所有的contours绘制轮廓
cv2.imshow("img", img)
cv2.waitKe

最低0.47元/天 解锁文章
2039

被折叠的 条评论
为什么被折叠?



