Answer a question

I'm really stumped on this one. I have an image that was [BGR2GRAY]'d earlier in my code, and now I need to add colored circles and such to it. Of course this can't be done in a 1 channel matrix, and I can't seem to turn the damned thing back into 3.

numpy.dstack() crashes everything

GRAY2BGR does not exist in opencv2

cv.merge(src1, src2, src3, dst) has been turned into cv2.merge(mv) where mv = "a vector of matrices", whatever that means.

Any ideas?

Opencv2.4.3 refmanual

Answers

Here's a way of doing that in Python:

img = cv2.imread("D:\\img.jpg")
gray = cv2.cvtColor(img, cv.CV_BGR2GRAY)

img2 = np.zeros_like(img)
img2[:,:,0] = gray
img2[:,:,1] = gray
img2[:,:,2] = gray

cv2.circle(img2, (10,10), 5, (255,255,0))
cv2.imshow("colour again", img2)
cv2.waitKey()

Here's the complete code for OpenCV3:

import cv2
import numpy as np
img = cv2.imread('10524.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
img2 = np.zeros_like(img)
img2[:,:,0] = gray
img2[:,:,1] = gray
img2[:,:,2] = gray
cv2.imwrite('10524.jpg', img2)
Logo

Python社区为您提供最前沿的新闻资讯和知识内容

更多推荐