You don't need to convert NumPy array to Mat because OpenCV cv2 module can accept NumPyarray.
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. So you should divide by 255 in your code, as shown below.
img = numpy.zeros([5,5,3])
img[:,:,0] = numpy.ones([5,5])*64/255.0
img[:,:,1] = numpy.ones([5,5])*128/255.0
img[:,:,2] = numpy.ones([5,5])*192/255.0
cv2.imwrite('color_img.jpg', img)
cv2.imshow("image", img)
cv2.waitKey()
Answer from bikz05 on Stack OverflowYou don't need to convert NumPy array to Mat because OpenCV cv2 module can accept NumPyarray.
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. So you should divide by 255 in your code, as shown below.
img = numpy.zeros([5,5,3])
img[:,:,0] = numpy.ones([5,5])*64/255.0
img[:,:,1] = numpy.ones([5,5])*128/255.0
img[:,:,2] = numpy.ones([5,5])*192/255.0
cv2.imwrite('color_img.jpg', img)
cv2.imshow("image", img)
cv2.waitKey()
You are looking for scipy.misc.toimage:
import scipy.misc
rgb = scipy.misc.toimage(np_array)
It seems to be also in scipy 1.0, but has a deprecation warning. Instead, you can use pillow and PIL.Image.fromarray
How can I turn numpy ndarray into an opencv image? Or alternatively, is there a module that can take numpy array screenshots at a high FPS?
python - How can I read a numpy array image with OpenCV? - Stack Overflow
python - Access numpy array in openCV - Raspberry Pi Stack Exchange
How to combine cv2 video frames and numpy audio array using ffmpeg to give hls playlist as output?
Which os? Windows or linux?
More on reddit.comI'm attempting to automate a game using opencv and yolov5. My final problem is that I can't get a script to capture the screen fast enough to follow the objects correctly. I found a module called DXCam that can capture images at astounding speeds, though I can't use them in my algorithm as they are ndarrays and not normal arrays. Is there a way to convert the ndarrays into normal arrays (this process does not need to be fast, I can simply buffer them and alter them after they've been captured)? This is the script that produces the ndarrays:
def record():
size = (pyautogui.size())
box = pyautogui.locateOnScreen('game_screen.png', region=(0, 0, size[0], size[1]), grayscale=True, confidence=0.7)
record_time = 3
target_fps = 120
writer = cv2.VideoWriter(
"output.mp4", cv2.VideoWriter_fourcc(*"mp4v"), target_fps, (box.width, box.height)
)
camera = dxcam.create(output_color="RGB")
camera.start(target_fps=120, video_mode=True, region=(box.left, box.top, box.left + box.width, box.top + box.height))
cur_time = time.time()
while time.time() < cur_time + record_time:
writer.write(camera.get_latest_frame())
camera.stop()
writer.release()Alternatively, are there any faster methods than this? This one for example is not fast enough for my needs (Merely 12-13 frames a second):
def record_bad():
height = GetSystemMetrics(1)
width = GetSystemMetrics(0)
file_name = 'output.mp4v'
fourcc = cv2.VideoWriter_fourcc(*'MP4V')
captured_video = cv2.VideoWriter(file_name, fourcc, 120.0, (width, height))
time_before = time.time()
frames = []
while time.time() > time_before + 3:
img = ImageGrab.grab(bbox=(0, 0, width, height)) # bbox specifies specific region (bbox= x,y,width,height)
img_final = cv2.cvtColor(np.array(img), cv2.COLOR_BGR2GRGB)
frames.append(img_final)
cv2.waitKey(0)
for frame in frames:
captured_video.write(frame)
captured_video.release()
cv2.destroyAllWindows()