基于形状轮廓检测的形状匹配

2024-06-26 13:04:25 发布

您现在位置:Python中文网/ 问答频道 /正文

import numpy as np
import cv2

temp = cv2.imread('./image/star.jpg')
ret, temp_thre = cv2.threshold(temp, 100, 255,0)

cv2.imshow("store gray iamge",temp_thre)
cv2.waitKey(0)

_, contours, hierarchy = cv2.findContours(temp_thre, cv2.RETR_LIST, cv2.CHAIN_APPROX_NONE)
temp_cont = sorted(contours, key=cv2.contourArea, reverse= True)

new_temp_cont = temp_cont[1]

tar = cv2.imread('./image/store.jpg')
tar_gray = cv2.cvtColor(tar, cv2.COLOR_BGR2GRAY)
tar_thr = cv2.threshold(tar_gray, 127, 255, 0)
_, contours, hierarchy = cv2.findContours(tar_thr, cv2.RETR_LIST, cv2.CHAIN_APPROX_NONE)

for c in contours:
    match = cv2.matchShapes(new_temp_cont, c, 1, 0.0)
    print(match)
    if match < 0.15:
        closest_match = c
    else:
        closest_match = []
cv2.drawContours(tar, [closest_match], -1, (0,255,0),2)
cv2.imshow("output", tar)
cv2.waitKey(0)
cv2.destroyAllWindows()

这是我的代码,但我仍然无法纠正它。请在这方面帮助我。在


这里的错误是:

^{pr2}$

Tags: imageimportthresholdmatchtarcv2tempjpg
1条回答
网友
1楼 · 发布于 2024-06-26 13:04:25

对于findContours,您的映像中的通道太多,这需要一个8位的单通道图像。以下是如何获取源映像的信息:

temp = cv2.imread('./image/star.jpg')
print('shape={}'.format(temp.shape))

如果形状中有两个元素,则图像为单通道。如果为3,则第3个元素为通道数。在

您需要将图像转换为单通道8位图像,例如:

^{pr2}$

相关问题 更多 >