Home > Back-end >  Pygame surface to opencv image object (not file saving)
Pygame surface to opencv image object (not file saving)

Time:06-26

I have a rectangle area on a pygame surface. I want to convert that area into opencv image object (without saving into a file) for further image processing on that rectangle area only. How can I do that?

selected_area =  mysurface.subsurface((x,y,w,h))
img_array=numpy.array(pygame.surfarray.array2d(selected_area))

#image_object = ..... ?

gray = cv2.cvtColor(image_object, cv2.COLOR_BGR2GRAY)
cv2.imshow("test",gray)

CodePudding user response:

Actually, a cv2 image is just a three-dimensional numpy array. the 1st dimension is the height, the 2nd the width and the 3rd the number of channels in the order blue, green, red.
Use

import pygame
import cv2, numpy

pygame.init()
window = pygame.display.set_mode((400, 400))
clock = pygame.time.Clock()

def pygameSurfaceToCv2Image(mysurface, x, y, w, h):
    selected_area =  mysurface.subsurface((x, y, w, h))
    img_array = numpy.array(pygame.surfarray.pixels3d(selected_area))
    image_object = numpy.transpose(img_array, (1, 0, 2))
    #image_object[:, :, [0, 2]] = image_object[:, :, [2, 0]]
    image_object = cv2.cvtColor(image_object, cv2.COLOR_RGB2BGR)
    return image_object

apple_surf = pygame.image.load("Apple1.bmp")
image_object = pygameSurfaceToCv2Image(apple_surf, 20, 20, 200, 120)
gray = cv2.cvtColor(image_object, cv2.COLOR_BGR2GRAY)
cv2.imshow("apple subsurface", image_object)
cv2.imshow("gray apple subsurface", gray)

run = True
while run:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False 

    window.fill(0)
    window.blit(image, image.get_rect(center = window.get_rect().center))
    pygame.display.flip()
    clock.tick(60)

pygame.quit()
exit()

CodePudding user response:

Thanks a lot. just changed pixels2d to pixels3d, it's working as I wanted

  • Related