raspberrypi / picamera2

New libcamera based python library
BSD 2-Clause "Simplified" License
892 stars 188 forks source link

[HOW-TO] get an rgb cv2 image from picamera2.capture_array #843

Closed cyborgdennett closed 1 year ago

cyborgdennett commented 1 year ago

Please only ask one question per issue! Hello, My question is as follows: I am reading the picamera2 image from a camera. And I need to process it in RGB format. However, I get a 4 dimensional array

camera = Picamera2()
camera.start()

while True:
    im = camera.capture_array()
    print(im.shape)

Output

(240,240,4)

The problem I have is that the array that is returned is 4 wide/4 color. And the function I want to use needs an np.array with dimensions (x,y,3)

How can I get the RGB from an capture_array?

Describe what it is that you want to accomplish A clear and concise description of what you want to happen.

Describe alternatives you've considered A clear and concise description of any alternative solutions or features you've considered.

Additional context Add any other context or screenshots about the request here.

davidplowman commented 1 year ago

By default, Picamera2 often asks for 4-channel (RGBA) images because it uses the GPU to accelerate the preview rendering, and that's what the GPU wants. If you're not showing a preview, there's no reason not to ask for plain RGB in the first place. So something like this:

from picamera2 import Picamera2

picam2 = Picamera2()
config = picam2.create_preview_configuration({'format': 'BGR888'})
picam2.configure(config)
picam2.start()

Replace BGR888 by RGB888 if you find that R and B are the wrong way round.

cyborgdennett commented 1 year ago

Thanks