为什么cv2没有boundingRect这个方法

干饭人之路 CSDN会员 干饭人之路 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
​
from skimage.metrics import structural_similarity
import imutils
import cv2


#加载两张图片并将他们转换为灰度
imageA = cv2.imread('D:\\PycharmProjects\\yolov5-master\\data\\getpicture\\different\\230221_124819.jpeg')
imageB = cv2.imread('D:\\PycharmProjects\\yolov5-master\\data\\getpicture\\different\\230221_125552.jpeg')

grayA = cv2.cvtColor(imageA, cv2.COLOR_BGR2GRAY)
grayB = cv2.cvtColor(imageB, cv2.COLOR_BGR2GRAY)

# 计算两个灰度图像之间的结构相似度指数
(score, diff) = structural_similarity(grayA, grayB, full=True)
diff = (diff * 255).astype("uint8")
print("SSIM:{}".format(score))

# 找到不同点的轮廓以致于我们可以在被标识为“不同”的区域周围放置矩形
thresh = cv2.threshold(diff, 0, 255, cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)[1]
cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
print(cnts[0])
print(imutils.is_cv2())
print(cnts[1])
cnts = cnts[0] if imutils.is_cv2() else cnts[1]

#print(cnts)
# 找到一系列区域,在区域周围放置矩形
for c in cnts:
    x, y, w, h = cv2.boundingRect(c)
    cv2.rectangle(imageA, (x, y), (x + w, y + h), (0, 255, 0), 2)
    cv2.rectangle(imageB, (x, y), (x + w, y + h), (0, 255, 0), 2)

# 用cv2.imshow 展现最终对比之后的图片, cv2.imwrite 保存最终的结果图片
cv2.imshow("Modified", imageB)
cv2.imwrite('D:\\PycharmProjects\\yolov5-master\\data\\getpicture\\different\\result.bmp', imageB)
cv2.waitKey(0)

​

上述python代码在运行时提醒如下:

C:\Python39\python.exe D:/PycharmProjects/yolov5-master/data/getpicture/picturediff_t2.py
SSIM:0.9624110411034766
Traceback (most recent call last):
  File "D:\PycharmProjects\yolov5-master\data\getpicture\picturediff_t2.py", line 30, in <module>
    x, y, w, h = cv2.boundingRect(c)
cv2.error: OpenCV(4.7.0) D:\a\opencv-python\opencv-python\opencv\modules\imgproc\src\shapedescr.cpp:874: error: (-215:Assertion failed) npoints >= 0 && (depth == CV_32F || depth == CV_32S) in function 'cv::pointSetBoundingRect'


进程已结束,退出代码1

上述代码为什么报错,为什么cv2没有boundingRect这个方法?如何修复??

给出正确解答可以得到2元红包。

...全文

相关推荐

评论 1
收藏 已收藏
分享
返回顶部