【发布时间】:2021-01-13 10:10:06
【问题描述】:
所以我正在做一个项目,有人给了我一些他们在 python 2.7 中创建的代码来实现它。然而,该项目在 python 3.7 上运行,当我尝试执行它时,我不断收到与标记函数相关的错误。有人可以查看它并告诉我缺少什么来执行该版本吗?我附上了用于测试功能的图像以及代码。
以下是我得到的错误:
Traceback (most recent call last):
File "/home/pi/Downloads/distance_to_camera_2 (1).py", line 94, in <module>
width_array=process_component(labels_im)
File "/home/pi/Downloads/distance_to_camera_2 (1).py", line 71, in process_component
x,y,w,h = cv2.boundingRect(cnts[0])
TypeError: Expected cv::UMat for argument 'array'
这是代码:
import numpy as np
import cv2
from matplotlib import pyplot as plt
# Find distance from camera to object using Python and OpenCV
def distance_to_camera(knownWidth, focalLength, perWidth):
# compute and return the distance from the maker to the camera
return (knownWidth * focalLength) / perWidth
KNOWN_WIDTH = 8
focalLength = 545
# put your image here
img = cv2.imread("/home/pi/Downloads/many_blob.png")
cv2.imshow("img", img)
cv2.waitKey(1000)
image = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY)[1] # ensure binary 127
cv2.imshow("image", image)
cv2.waitKey(1000)
kernel = np.ones((5,5),np.uint8)
erosion = cv2.erode(image,kernel,iterations = 5)
dilate=cv2.dilate(erosion,kernel,iterations = 5)
edged = cv2.Canny(dilate, 0, 128)
cv2.imshow("edged", edged)
cv2.waitKey(1000)
connectivity=8
num_labels,labels_im = cv2.connectedComponents(edged,connectivity)
# Function only for labels display (debuging only)
def imshow_components(labels):
# Map component labels to hue val
label_hue = np.uint8(179*labels/np.max(labels))
blank_ch = 255*np.ones_like(label_hue)
labeled_img = cv2.merge([label_hue, blank_ch, blank_ch])
# cvt to BGR for display
labeled_img = cv2.cvtColor(labeled_img, cv2.COLOR_HSV2BGR)
# set bg label to black
labeled_img[label_hue==0] = 0
#labeled_img[labels==0] = 0
cv2.imshow('labeled.png', labeled_img)
cv2.waitKey(1000)
cv2.imwrite('labeled_img.png',labeled_img)
#cv2.imwrite('label_hue.png',label_hue)
def process_component(labels):
width = np.zeros(np.max(labels))
for i in range(1,np.max(labels)+1):
tmp_im= labels.copy()
tmp_im[:] = 0
tmp_im[labels==i] = 255
file="imlabel_%d.png"%(i, )
cv2.imwrite(file,tmp_im)
tmp_im = tmp_im.astype(np.uint8)
cnts = cv2.findContours(tmp_im, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
# bounding box of the countour
x,y,w,h = cv2.boundingRect(cnts[0])
width[i-1] = w
tmp_im=cv2.rectangle(tmp_im,(x,y),(x+w,y+h),(255,0,0),2)
# center = center of the bounding box
center=(x+w/2,y+h/2)
cv2.circle(tmp_im, center, 3, (255,0,0), 2, 8, 0)
cv2.imshow(file, tmp_im)
cv2.waitKey(1000)
return width
width_array=process_component(labels_im)
imshow_components(labels_im)
cv2.imwrite('labels_img.png',labels_im)
for i in range(1,np.max(labels_im)+1):
w=width_array[i-1]
#marker = find_marker(image)
dist_cm = distance_to_camera(KNOWN_WIDTH, focalLength, w)
print("distance en cm = %d",dist_cm)
这是我第一次在堆栈溢出上发帖,所以如果我应该发布其他任何内容来帮助我,请告诉我。
这是我一直在尝试使用的图像: https://i.stack.imgur.com/ONhUA.png
【问题讨论】:
-
我没有弄乱 OpenCV,所以我不能帮你。但是,您应该删除任何不相关的行以使其更具可读性,最后尝试将您的代码分解到问题所在。尝试将其重新组织为
how to draw an image in OpenCV或类似的内容以适合您的问题。祝你好运,我相信你会克服这个障碍。欢迎使用 StackOverflow,它是一个很好的资源。 -
快速注意底部的
print声明应该是print("distance en cm = %d" % dist_cm)。使用%(取模)将dist_cm放在字符串中%d字符处。 -
感谢@Crispy,编辑了代码,这样绒毛就不再存在了
-
Python 2.7 和 3.7 之间的 OpenCV 版本是否相同?
cv2.findContours的输出取决于它是版本 3 还是版本 4,将有两个输出或三个输出。我高度怀疑您在两个 Python 环境之间没有匹配的 OpenCV 版本。对于每个环境,请import cv2; print(cv2.__version__)告诉我们它为 Python 2.7 和 3.7 打印的内容。 -
@rayryeng 好提示。您可能必须重构代码以使用 Python3.7 的库版本,某些特性/功能可能会被贬值。我建议先从最小的部分重建您的程序,然后以这种方式获得帮助。在你这样做之前尝试放置打印语句以找出程序出错的地方。我没有太多时间,但我会采用您的代码并尝试使其正常工作。现在,请尽量在新文件中逐步重新创建脚本。让它一块一块地工作。