在Canny边缘检测中出现错误
我正在尝试用OpenCV和Python写一段代码,目的是自动获取Canny边缘检测的阈值,而不是每次都手动设置。
img= cv2.imread('micro.png',0)
output = np.zeros(img.shape, img.dtype)
# Otsu's thresholding
ret2,highthresh = cv2.threshold(img,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
lowthresh=0.1*highthres
edges = cv2.Canny(img,output,lowthresh,highthresh)
cv2.imshow('canny',edges)
我遇到了这个错误: "文件 "test2.py",第14行,出错: edges = cv2.Canny(img, output, lowthresh, highthresh) 类型错误:只有长度为1的数组可以转换为Python标量"
有没有人能帮我解决这个错误?提前谢谢大家!
2 个回答
1
你正在运行:
cv2.Canny(img,output,lowthresh,highthresh)
它在寻找
cv2.Canny(img,lowthresh,highthresh,output)
我觉得在某个版本中,顺序发生了变化,因为我见过对这两者的提及。
2
看起来 cv2.threshold
是用来找出图像中的边缘,而 Canny
则是把这些边缘应用到图像上。下面的代码对我来说很好用,帮我在图像中找到了不错的边缘。
import cv2
cv2.namedWindow('canny demo')
img= cv2.imread('micro.png',0)
ret2,detected_edges = cv2.threshold(img,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
edges = cv2.Canny(detected_edges,0.1,1.0)
dst = cv2.bitwise_and(img,img,mask = edges)
cv2.imshow('canny',dst)
if cv2.waitKey(0) == 27:
cv2.destroyAllWindows()