Marsh Mellow
Marsh Mellow

Reputation: 17

How to make cv2 colored image (bgr) to blue range or green range image?

  1. A numpy array (x,y) = unsorted data between(0,10 f.eks.) is coverted to a colored cv2 image bgr and saved.

    self.arr = self.arr * 255 #bgr format

    cv2.imwrite("img", self.arr)

How to make this cv2 colored image to blue range color (light to dark blue), and how to make it to green range color(light to dark green)?

My thoughts are to go image2np and then do some stuff to the array. Then go back np2image. But I don't know how change values to get expected colours.

Upvotes: 0

Views: 2115

Answers (1)

furas
furas

Reputation: 142641

I'm not sure if I understand problem but I would convert RGB to grayscale and next create empty RGB (with zeros) and put grayscale as layer B to get "blue range" or as G to get "green range"

import cv2
import numpy as np

img = cv2.imread('test/lenna.png')
cv2.imshow('RGB', img)

h, w = img.shape[:2]  # height, width

gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
cv2.imshow('Gray', gray_img)

blue_img = np.zeros((h,w,3), dtype='uint8') 
blue_img[:,:,0] = gray_img  # cv2 uses `BGR` instead of `RGB`
cv2.imshow('Blue', blue_img)

green_img = np.zeros((h,w,3), dtype='uint8') 
green_img[:,:,1] = gray_img  # cv2 uses `BGR` instead of `RGB`
cv2.imshow('Green', green_img)

red_img = np.zeros((h,w,3), dtype='uint8') 
red_img[:,:,2] = gray_img  # cv2 uses `BGR` instead of `RGB`
cv2.imshow('Red', red_img)

cv2.waitKey(0)
cv2.destroyAllWindows()

Image Lenna from Wikipedia.

enter image description here

enter image description here

enter image description here

enter image description here

enter image description here

Upvotes: 0

Related Questions