Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert a 1 channel image into a 3 channel with opencv2?

Tags:

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

like image 360
grenadier Avatar asked Feb 09 '13 08:02

grenadier


People also ask

How do I split an image in OpenCV?

In this article, we will learn how to split a multi-channel image into separate channels and combine those separate channels into a multi-channel image using OpenCV in Python. To do this, we use cv2. split() and cv2. merge() functions respectively.

What is cv2 merge?

The cv2. split() function splits the source multichannel image into several single-channel images. The cv2. merge() function merges several single-channel images into a multichannel image.

What is Channel in image OpenCV?

There are three channels in an RGB image- red, green and blue. The color space where red, green and blue channels represent images is called RGB color space. In OpenCV, BGR sequence is used instead of RGB. This means the first channel is blue, the second channel is green, and the third channel is red.


1 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) 
like image 190
b_m Avatar answered Sep 28 '22 06:09

b_m