3

I'm trying to capture images with a Raspberry Pi Camera V2.1 on a Raspberry Pi 4B using .NET 7. I've set up my code to capture an image with the following code:

using Iot.Device.Media;

VideoConnectionSettings settings = new VideoConnectionSettings(busId: 0, captureSize: (640, 480), pixelFormat: PixelFormat.JPEG);

using VideoDevice device = VideoDevice.Create(settings);

device.Capture("image.jpg");

However, when I run this code, the images I get are green-tinted. I've confirmed that the camera itself works properly by testing it with Python's PiCamera library.

I was expecting the captured images to look similar to the ones captured using Python's PiCamera library, which had accurate color representation. To troubleshoot the issue, I tried adjusting the PixelFormat parameter to other values, such as YUYV, but I couldn't get it to work. I also checked the connection between the camera and the Raspberry Pi, and it seems to be fine.

Here's an example of one of the green-tinted images I captured. As you can see, the image has a strong green color cast. I'm not sure what could be causing this, as I followed this tutorial exactly and it worked for the author. Is there something I'm missing or doing wrong? Are there any additional settings I need to adjust, or is there a different library or approach I should be using? Any help would be appreciated."

Edit: A commenter pointed out that the tutorial I followed was for .NET 6, not .NET 7. I tried using .NET 6 and published a self-contained app to test the code again, but the problem persists.

Seszele
  • 31
  • 2
  • The tutorial you linked is using .NET 6 - does the code example work properly on .NET 6? – CoolBots Mar 17 '23 at 00:33
  • @pm100 the OP stated that they've "confirmed that the camera itself works properly by testing it with Python's PiCamera library." – CoolBots Mar 17 '23 at 00:52
  • [Here](https://github.com/dotnet/iot/issues/1875) is a lengthy discussion about this binding. It seems that yes, sometimes the color format seems to be wrong. I'm not sure about a possible workaround/fix though. – PMF Mar 17 '23 at 12:55

1 Answers1

2

This fixed it for me, i needed to install "sudo apt install libgdiplus" for the Bitmap and Color classes to work, then only worked for the NV12 pixel format.

VideoConnectionSettings settings = 
new VideoConnectionSettings(busId: 0, captureSize: (640, 480),pixelFormat: PixelFormat.NV12);

using VideoDevice device = VideoDevice.Create(settings);

MemoryStream ms = new MemoryStream(device.Capture());
Color[] colors = VideoDevice.Nv12ToRgb(ms,settings.CaptureSize);
Bitmap bitmap = VideoDevice.RgbToBitmap(settings.CaptureSize, colors);
bitmap.Save(filename, System.Drawing.Imaging.ImageFormat.Jpeg);

Annosz
  • 992
  • 6
  • 23
  • I am getting this error when running the code: Getting information about your camera... Unhandled exception. System.InvalidOperationException: No image factory registered. Call BitmapImage.RegisterImageFactory() with a suitable implementation first. Consult the documentation for further information at Iot.Device.Graphics.BitmapImage.VerifyFactoryAvailable() at Iot.Device.Media.VideoDevice.RgbToBitmap(ValueTuple`2 size, Color[] colors, PixelFormat format) at Program.
    $(String[] args) in /home/droneport/CameraTest/src/Program.cs:line 14
    – tank104 Aug 29 '23 at 20:31
  • although I notice your RgbToBitmap returns Bitmap not BitmapImage. Perhaps different versions of library – tank104 Aug 29 '23 at 20:33
  • Ok I was using version 3 of the Iot.Device.Bindings packages. Going back to version 2 sorted it and it builds. However when I run its all green (although I noticed when doing just a plain JPG output its all NULL repeated, so something is wrong with the setup. – tank104 Aug 29 '23 at 21:11
  • This seems to explain the reason: https://github.com/dotnet/iot/issues/1875 – tank104 Aug 29 '23 at 21:11