函数错误';距离变换';-Python,使用OpenCV(3.4.9)
我正在尝试使用距离变换对轮廓进行变换,但我遇到了一个错误:函数错误';距离变换';-Python,使用OpenCV(3.4.9),python,opencv,Python,Opencv,我正在尝试使用距离变换对轮廓进行变换,但我遇到了一个错误: out = cv2.distanceTransform(mask, distanceType=cv2.DIST_L2, maskSize=5) cv2.error: OpenCV(3.4.9) /Users/travis/build/skvark/opencv-python/opencv/modules/imgproc/src/distransform.cpp:724: error: (-215:Assertion failed) sr
out = cv2.distanceTransform(mask, distanceType=cv2.DIST_L2, maskSize=5)
cv2.error: OpenCV(3.4.9) /Users/travis/build/skvark/opencv-python/opencv/modules/imgproc/src/distransform.cpp:724: error: (-215:Assertion failed) src.type() == CV_8UC1 in function 'distanceTransform'
这是我的代码:
import cv2
import imutils
pathToThePhoto = 'labrador.jpg'
img = cv2.imread(pathToThePhoto)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
thresh = cv2.threshold(gray, 100 , 255, cv2.THRESH_BINARY)[1]
cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = imutils.grab_contours(cnts)
c = max(cnts, key=cv2.contourArea)
mask = cv2.drawContours(gray, [c], -1, (0, 255, 255), 2) #Edit: Changed from img to gray
out = cv2.distanceTransform(mask, distanceType=cv2.DIST_L2, maskSize=5)
cv2.imshow("distance-transform", out)
cv2.waitKey(0)
cv2.destroyAllWindows()
拉布拉多:
编辑后的结果:
这看起来不是正确的结果,是吗 问题在于
cv2.distanceTransform
的输出类型为np.float32
在显示
out
之前,您需要将out
规格化为范围[0,1]
请参见OpenCV:
规范化范围={0.0,1.0}的距离图像因此,我们可以将其可视化并设置阈值
cv.normalize(dist,dist,0,1.0,cv.NORM\u MINMAX)
cv.imshow('Distance Transform Image',dist)
代码如下:
import cv2
import imutils
pathToThePhoto = 'labrador.jpg'
img = cv2.imread(pathToThePhoto)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
thresh = cv2.threshold(gray, 100 , 255, cv2.THRESH_BINARY)[1]
cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = imutils.grab_contours(cnts)
c = max(cnts, key=cv2.contourArea)
mask = cv2.drawContours(gray, [c], -1, 255, 2) #Edit: Changed from img to gray
out = cv2.distanceTransform(mask, distanceType=cv2.DIST_L2, maskSize=5)
# Normalize the distance image for range = {0.0, 1.0}
# so we can visualize and threshold it
out = cv2.normalize(out, out, 0, 1.0, cv2.NORM_MINMAX)
cv2.imshow("distance-transform", out)
cv2.waitKey(0)
cv2.destroyAllWindows()
out
:我不确定这是否是您预期的结果。
您正在使用小狗对图像应用距离变换
掩码
:问题在于
cv2.distance transform
的输出类型为np.float32
在显示
out
之前,您需要将out
规格化为范围[0,1]
请参见OpenCV:
规范化范围={0.0,1.0}的距离图像因此,我们可以将其可视化并设置阈值
cv.normalize(dist,dist,0,1.0,cv.NORM\u MINMAX)
cv.imshow('Distance Transform Image',dist)
代码如下:
import cv2
import imutils
pathToThePhoto = 'labrador.jpg'
img = cv2.imread(pathToThePhoto)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
thresh = cv2.threshold(gray, 100 , 255, cv2.THRESH_BINARY)[1]
cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = imutils.grab_contours(cnts)
c = max(cnts, key=cv2.contourArea)
mask = cv2.drawContours(gray, [c], -1, 255, 2) #Edit: Changed from img to gray
out = cv2.distanceTransform(mask, distanceType=cv2.DIST_L2, maskSize=5)
# Normalize the distance image for range = {0.0, 1.0}
# so we can visualize and threshold it
out = cv2.normalize(out, out, 0, 1.0, cv2.NORM_MINMAX)
cv2.imshow("distance-transform", out)
cv2.waitKey(0)
cv2.destroyAllWindows()
out
:我不确定这是否是您预期的结果。
您正在使用小狗对图像应用距离变换
掩码
:您需要将灰度图像(或只有一个通道的图片)传递给函数。@FarhoodET请检查我的编辑您需要将灰度图像(或只有一个通道的图片)传递给函数。@FarhoodET请检查我的编辑