Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/28.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
OpenCV Python中的距离变换自动将CV_8UC3转换为CV_32SC1,从而创建断言错误_Python_Opencv_Computer Vision_Opencv3.0_Watershed - Fatal编程技术网

OpenCV Python中的距离变换自动将CV_8UC3转换为CV_32SC1,从而创建断言错误

OpenCV Python中的距离变换自动将CV_8UC3转换为CV_32SC1,从而创建断言错误,python,opencv,computer-vision,opencv3.0,watershed,Python,Opencv,Computer Vision,Opencv3.0,Watershed,我正在尝试根据教程将分水岭算法应用于图像:。我之前在灰度图像上应用了高斯滤波和形态变换后的大津阈值,以提高图像质量,如代码所示: img = cv2.imread('Results\Feb_16-0.jpg',0) kernel = np.ones((1,1),np.uint8) opening = cv2.morphologyEx(img, cv2.MORPH_OPEN, kernel) blur = cv2.GaussianBlur(opening,(1,1),0) ret3,th4 = c

我正在尝试根据教程将分水岭算法应用于图像:。我之前在灰度图像上应用了高斯滤波和形态变换后的大津阈值,以提高图像质量,如代码所示:

img = cv2.imread('Results\Feb_16-0.jpg',0)
kernel = np.ones((1,1),np.uint8)
opening = cv2.morphologyEx(img, cv2.MORPH_OPEN, kernel)
blur = cv2.GaussianBlur(opening,(1,1),0)
ret3,th4 = cv2.threshold(blur,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
# sure background area
sure_bg = cv2.dilate(opening,kernel,iterations=1)
# Finding sure foreground area
dist_transform = cv2.distanceTransform(opening,cv2.DIST_L2,3)
根据以下代码,将距离变换作为分水岭算法的第一阶段:

img = cv2.imread('Results\Feb_16-0.jpg',0)
kernel = np.ones((1,1),np.uint8)
opening = cv2.morphologyEx(img, cv2.MORPH_OPEN, kernel)
blur = cv2.GaussianBlur(opening,(1,1),0)
ret3,th4 = cv2.threshold(blur,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
# sure background area
sure_bg = cv2.dilate(opening,kernel,iterations=1)
# Finding sure foreground area
dist_transform = cv2.distanceTransform(opening,cv2.DIST_L2,3)
创建错误:

error: (-215) src.type() == CV_8UC3 && dst.type() == CV_32SC1 in function cv::watershed

其中,尝试将8位3通道图像转换为32位单通道图像。如何防止这种情况,同时使用距离变换?

在函数
cv2中。分水岭(img,markers)
您的输入参数
img
必须有3个通道。 完整工作代码:

#Load image in grayscale
img = cv2.imread('Results\Feb_16-0.jpg',0)

kernel = np.ones((1,1),np.uint8)
opening = cv2.morphologyEx(img, cv2.MORPH_OPEN, kernel)
blur = cv2.GaussianBlur(opening,(1,1),0)
ret3,th4 = cv2.threshold(blur,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)

# sure background area
sure_bg = cv2.dilate(opening,kernel,iterations=1)
# Finding sure foreground area
dist_transform = cv2.distanceTransform(opening,cv2.DIST_L2,3)

ret, sure_fg = cv2.threshold(dist_transform,0.7*dist_transform.max(),255,0)
sure_fg = np.uint8(sure_fg)
unknown = cv2.subtract(sure_bg,sure_fg)

ret, markers = cv2.connectedComponents(sure_fg)
markers = markers+1
markers[unknown==255] = 0
markers = markers.astype('int32')

#now load same image as color image
img = cv2.imread('Results\Feb_16-0.jpg',1)

markers = cv2.watershed(img,markers)
img[markers == -1] = [255,0,0]

cv2.distanceTransform
中的输入数组格式不正确时,会出现此错误。它应该是np.uint8(不是int8)类型的2D数组

比如说

import cv2 as cv
import numpy as np

testim = np.zeros((11,11), dtype = uint8)
testim[2:6,1:6] = 255
testim[3,3] = 0
print(testim)
dist = cv.distanceTransform(testim, cv.DIST_L2, 5)
print(testim)
如果正在读取格式不正确的图像,必须首先将其转换为灰色(仅一个通道),并确保其为uint8格式。这可以通过
imagename.astype(np.uint8)


这是在opencv版本3.3.1、python 3.5中测试的。

您能解决它吗?不幸的是,我不能解决它,我似乎根本没有遇到这个错误。你能上传你正在使用的图像吗?此外,使用1的内核大小进行卷积不会改变任何事情。尝试将内核大小更改为3或更多