Create multi-channel null dial in python with cv2

I want to create a multi-channel mat object in python with an opencv cv2 wrapper.

I found examples on a network where C ++ Mat :: zeros is replaced with numpy.zeros, which seems good. but the multichannel type does not fit.

look at the code:

import cv2
import numpy as np

size = 200, 200
m = np.zeros(size, dtype=np.uint8) # ?
m = cv2.cvtColor(m, cv2.COLOR_GRAY2BGR)
p1 = (0,0)
p2 = (200, 200)
cv2.line(m, p1, p2, (0, 0, 255), 10)

cv2.namedWindow("draw", cv2.CV_WINDOW_AUTOSIZE)
while True:
    cv2.imshow("draw", m)

    ch = 0xFF & cv2.waitKey(1)
    if ch == 27:
        break
cv2.destroyAllWindows()

I want to avoid m = cv2.cvtColor(m, cv2.COLOR_GRAY2BGR), but it doesn’t work cv2.CV_8UC3 np.uin32.

any hint?

+5
source share
1 answer

Try this as size:

size = 200, 200, 3
m = np.zeros(size, dtype=np.uint8)

Basically, what I did to find which arguments I needed for the matrix:

img = cv2.imread('/tmp/1.jpg')
print img.shape, img.dtype
# (398, 454, 3), uint8

But one could also find it in the OpenCV documentation.

+12
source

All Articles