Reputation: 75
I get an image type error when I run my code. I know HoughLinesP requires a grayscale image but when I try to convert the source image to grayscale I get the following error(1):
error: (-215) depth == 0 || depth == 2 || depth == 5 in function cv::cvtColor
If I run HoughLinesP without converting to grayscale I get the following error(2):
error: (-215) image.type() == (((0) & ((1 << 3) - 1)) + (((1)-1) << 3)) in function cv::HoughLinesProbabilistic
I don't know what conversion I need to get rid of the error
This is the code for where the errors occur:
#extract largest component from image.
components, output, stats, centroids = cv2.connectedComponentsWithStats(threshold_img, connectivity=4)
sizes = stats[:, -1]
max_label = 1
max_size = sizes[1]
for i in range(2, components):
if sizes[i] > max_size:
max_label = i
max_size = sizes[i]
biggestComponent = np.zeros(output.shape)
biggestComponent[output == max_label] = 255
biggestComponent = biggestComponent - cv2.erode(biggestComponent, np.ones((5,5), np.uint8))
dilated = cv2.dilate(biggestComponent, np.ones((3,3), dtype=np.uint8))
#-------------------------ERROR(1)----------------------------#
dilated = cv2.cvtColor(dilated, cv2.COLOR_BGR2GRAY)
#obtaining corners using houghlines
def find_intersection(line1, line2):
# extract points
x1, y1, x2, y2 = line1[0]
x3, y3, x4, y4 = line2[0]
# compute determinant
Px = ((x1*y2 - y1*x2)*(x3-x4) - (x1-x2)*(x3*y4 - y3*x4))/ \
((x1-x2)*(y3-y4) - (y1-y2)*(x3-x4))
Py = ((x1*y2 - y1*x2)*(y3-y4) - (y1-y2)*(x3*y4 - y3*x4))/ \
((x1-x2)*(y3-y4) - (y1-y2)*(x3-x4))
return Px, Py
def segment_lines(lines, delta):
h_lines = []
v_lines = []
for line in lines:
for x1, y1, x2, y2 in line:
if abs(x2-x1) < delta: # x-values are near; line is vertical
v_lines.append(line)
elif abs(y2-y1) < delta: # y-values are near; line is horizontal
h_lines.append(line)
return h_lines, v_lines
def cluster_points(points, nclusters):
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 10, 1.0)
_, _, centers = cv2.kmeans(points, nclusters, None, criteria, 10, cv2.KMEANS_PP_CENTERS)
return centers
#-------------------------ERROR(2)----------------------------#
# run the Hough transform
lines = cv2.HoughLinesP(dilated, rho=1, theta=np.pi/180, threshold=100, maxLineGap=20, minLineLength=50)
Upvotes: 3
Views: 3269
Reputation: 21203
Some basic understanding of images and their properties is required.
In OpenCV images are basically arrays. Before proceeding with any sort of conversion ensure if it is possible in the first place.
How does one do that?
shape
attribute.int
or float
)astype()
and pass in the datatype you want your array to be in.Coming back to your question! (I actually ran your entire code to arrive at this conclusion)
error: (-215) depth == 0 || depth == 2 || depth == 5 in function cv::cvtColor
This error occurs whenever the image you pass in has the wrong shape. In the line that causes this error cv2.COLOR_BGR2GRAY
expects the image to be a 3D array but when you check it using dilated.shape
, it returns a tuple of two values something like this (558L, 796L)
, which is not a 3D array. You are passing in a 2D array where the function expects a 3D array. The result of cv2.COLOR_BGR2GRAY
is a 2D array.
error: (-215) image.type() == (((0) & ((1 << 3) - 1)) + (((1)-1) << 3)) in function cv::HoughLinesProbabilistic
This error occurs due to the datatype of the array. The shape is correct but it expects an array of type int
. dilated
is a 2D array of type float
.
So how do you change it? Change the datatype of the array using astype
:
lines = cv2.HoughLinesP(dilated.astype(np.uint8), rho=1, theta=np.pi/180, threshold=100, maxLineGap=20, minLineLength=50)
Upvotes: 4