Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating an RGB picture in Python with OpenCV from a randomized array

I want to create a RGB image made from a random array of pixel values in Python with OpenCV/Numpy setup.

I'm able to create a Gray image - which looks amazingly live; with this code:

import numpy as np
import cv2

pic_array=np.random.randint(255, size=(900,800))
pic_array_8bit=slika_array.astype(np.uint8)
pic_g=cv2.imwrite("pic-from-random-array.png", pic_array_8bit)

But I want to make it in color as well. I've tried converting with cv2.cvtColor() but it couldnt work.

The issue might be in an array definition or a missed step. Couldn't find a similar situation... Any help how to make a random RGB image in color, would be great.

thanks!

like image 707
superasperatus Avatar asked Apr 25 '18 14:04

superasperatus


People also ask

How do I convert an image to RGB in OpenCV?

OpenCV uses BGR image format. So, when we read an image using cv2. imread() it interprets in BGR format by default. We can use cvtColor() method to convert a BGR image to RGB and vice-versa.

How do I get the RGB value of a pixel in Python OpenCV?

I think the most easiest way to get RGB of an image is use cv2. imshow("windowName",image) . The image would display with window, and the little information bar also display coordinate (x,y) and RGB below image.

Can cv2 read NumPy array?

You don't need to convert NumPy array to Mat because OpenCV cv2 module can accept NumPy array. The only thing you need to care for is that {0,1} is mapped to {0,255} and any value bigger than 1 in NumPy array is equal to 255.


1 Answers

RGB image is composed of three grayscale images. You can make three grayscale images like

rgb = np.random.randint(255, size=(900,800,3),dtype=np.uint8)
cv2.imshow('RGB',rgb)
cv2.waitKey(0)
like image 140
user8190410 Avatar answered Oct 17 '22 13:10

user8190410