Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

create a multichannel zeros mat in python with cv2

i want to create a multichannel mat object in python with cv2 opencv wrapper.

i've found examples on the net where the c++ Mat::zeros is replaced with numpy.zeros, that seems good. but no multichannel type seems to 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 neither cv2.CV_8UC3 np.uin32 works.

some hint?

like image 949
nkint Avatar asked Apr 26 '13 12:04

nkint


1 Answers

Try this as size:

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

Basically what I did to find what arguments I need for the matrix is:

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

But one could probably find it in OpenCV documentation as well.

like image 92
gatto Avatar answered Oct 26 '22 13:10

gatto