代码拉取完成,页面将自动刷新
import numpy as np
import cv2 as cv
img = cv.imread('lightning.png')
gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
ret,thresh = cv.threshold(gray,127,255,0)
contours,hierarchy = cv.findContours(thresh,1,2)
cnt = contours[0]
# 反转颜色便于显示(可省略)
gray = cv.bitwise_not(gray)
img = cv.cvtColor(gray, cv.COLOR_GRAY2BGR)
# 直边外接矩形绘制
x,y,w,h = cv.boundingRect(cnt)
cv.rectangle(img,(x,y),(x+w,y+h),(0,255,0),2)
# 旋转矩形绘制
rect = cv.minAreaRect(cnt)
box = cv.boxPoints(rect)
box = np.int0(box)
cv.drawContours(img,[box],0,(0,0,255),2)
cv.imshow('rectangle',img )
cv.imwrite('fig8-6.png',img)
cv.waitKey(0)
cv.destroyAllWindows()
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。