-1

I am trying to add 2 different Mat objects in python. Both Mat objects are binary images (CV_8U). But because the Matrices don't have the same size, I get an error when adding them.

I generate one of the Mat objects using numpy with the proper value for the channels, like this:

diagonal = np.zeros((height,width,1)) cv2.line(diagonal,(0,0),(height,width), (255))

The other Mat object comes from cv2.Canny:

canny_edge = cv2.Canny(input_image, min_thr, max_thr)

Addition code: final = cv2.addWeighted(canny_edge,1.0,diagonal,1.0,0)

I get the following error when I try to add the 2 Mat objects: error: (-5) When the input arrays in add/subtract/multiply/divide functions have different types, the output array type must be explicitly specified in function cv::arithm_op

I also tried removing the channels value from the numpy generated matrix, but I got the same error.

So I tried to print the channels, but I got this:

height, weight, channels = canny_edge.shape ValueError: not enough values to unpack (expected 3, got 2)

EDIT: I am sorry but the answer posted by Miki doesn't help me. The Mat object generated by cv2.Canny doesn't have channel information. I know it is a binary image, but opencv gets confused when it tries to add this Mat object's matrix with a Mat object which does have channel information.

SUB
  • 287
  • 3
  • 14
  • @Miki , I have updated the question to show what the issue is in more detail. The question you have tagged doesn't help in this case. – SUB Nov 05 '17 at 13:30
  • Can't you just add a singleton dimension to `canny_edge`? – Miki Nov 05 '17 at 13:38
  • @Miki thanks for the reply. Yes, I tried that. But the addition never terminates. It seems like it gets stuck in a loop. – SUB Nov 05 '17 at 22:03
  • @Miki Sorry. Please ignore my last comment. I thought I tried it but I can't find context. Could you please tell me how do I add a singleton dimension to the Mat object which is output of cv2.Canny? – SUB Nov 05 '17 at 22:09
  • I will post the answer here for others as the question is marked duplicate and I am not getting an answer button. Basically, use the `canny_edge` Mat object and convert it to a numpy array using: `canny_edge_nparr = np.asarray(canny_edge)` And then we can add a dimension to it: `canny_edge_nparr = np.expand_dims(canny_edge_nparr, axis=2)` Now the shape represents (height, width, channels) Now use numpy addition to add the arrays: `final = np.add(canny_edge_nparr, diagonal)` – SUB Nov 05 '17 at 22:35
  • Now you can post the answer. – Miki Nov 05 '17 at 22:37

1 Answers1

0

Basically, use the canny_edge Mat object and convert it to a numpy array using:

canny_edge_nparr = np.asarray(canny_edge)

And then we can add a dimension to it (as the cv2.Canny outputs a Mat object of shape: (height, width) ):

canny_edge_nparr = np.expand_dims(canny_edge_nparr, axis=2)

Now the shape represents (height, width, channels). Now use numpy addition to add the arrays:

final = np.add(canny_edge_nparr, diagonal)

SUB
  • 287
  • 3
  • 14