You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用.NET 7/6调用树莓派摄像头V2.1拍摄图像偏绿问题求助

解决.NET调用树莓派Camera V2.1图像偏绿问题

可行解决方案

1. 手动处理像素格式转换

偏绿问题大多是YUV到RGB颜色空间转换错误导致的,Iot.Device.Media的JPEG编码逻辑可能未适配树莓派摄像头。可以先捕获YUYV原始数据,再手动转成RGB/JPEG:

using Iot.Device.Media;
using System.Drawing;
using System.Drawing.Imaging;

var settings = new VideoConnectionSettings(busId: 0, captureSize: (640, 480), pixelFormat: PixelFormat.YUYV);
using var device = VideoDevice.Create(settings);

// 捕获YUYV原始数据
var buffer = device.CaptureRaw();

// 转换为RGB位图
var bitmap = new Bitmap(640, 480, PixelFormat.Format24bppRgb);
var bitmapData = bitmap.LockBits(new Rectangle(0, 0, 640, 480), ImageLockMode.WriteOnly, PixelFormat.Format24bppRgb);
ConvertYuyvToRgb(buffer, bitmapData.Scan0, 640, 480);
bitmap.UnlockBits(bitmapData);

// 保存为JPEG
bitmap.Save("image.jpg", ImageFormat.Jpeg);

// YUYV转RGB的核心实现
void ConvertYuyvToRgb(byte[] yuyvData, IntPtr rgbPtr, int width, int height)
{
    int yuyvIndex = 0;
    unsafe
    {
        byte* rgbBytes = (byte*)rgbPtr;
        for (int y = 0; y < height; y++)
        {
            for (int x = 0; x < width; x += 2)
            {
                byte y0 = yuyvData[yuyvIndex++];
                byte u = yuyvData[yuyvIndex++];
                byte y1 = yuyvData[yuyvIndex++];
                byte v = yuyvData[yuyvIndex++];

                // 标准YUV转RGB公式
                int r0 = (int)(y0 + 1.370705 * (v - 128));
                int g0 = (int)(y0 - 0.698001 * (v - 128) - 0.337633 * (u - 128));
                int b0 = (int)(y0 + 1.732446 * (u - 128));

                int r1 = (int)(y1 + 1.370705 * (v - 128));
                int g1 = (int)(y1 - 0.698001 * (v - 128) - 0.337633 * (u - 128));
                int b1 = (int)(y1 + 1.732446 * (u - 128));

                // 限制数值在0-255区间
                rgbBytes[0] = (byte)Math.Clamp(r0, 0, 255);
                rgbBytes[1] = (byte)Math.Clamp(g0, 0, 255);
                rgbBytes[2] = (byte)Math.Clamp(b0, 0, 255);
                rgbBytes += 3;

                rgbBytes[0] = (byte)Math.Clamp(r1, 0, 255);
                rgbBytes[1] = (byte)Math.Clamp(g1, 0, 255);
                rgbBytes[2] = (byte)Math.Clamp(b1, 0, 255);
                rgbBytes += 3;
            }
        }
    }
}

2. 替换为第三方.NET库

试试LibCameraSharp,它是树莓派官方LibCamera库的.NET绑定,对原生摄像头兼容性更好:

  1. 安装NuGet包:
dotnet add package LibCameraSharp
  1. 示例代码:
using LibCameraSharp;

using var camera = new Camera();
var config = camera.CreateStillConfiguration();
config.Resolution = new Size(640, 480);
config.Format = PixelFormat.Jpeg;

using var capture = camera.CaptureStill(config);
capture.Save("image.jpg");

3. 检查树莓派底层配置

重置摄像头驱动配置,确保底层参数正确:

sudo raspi-config nonint do_camera 0
sudo reboot

同时检查/boot/config.txt,确保以下配置未被注释:

start_x=1
gpu_mem=128

补充说明

偏绿问题本质是颜色转换逻辑的适配差异——Python的PiCamera用了树莓派官方优化的转换逻辑,而Iot.Device.Media的通用实现未针对该摄像头做适配。如果用LibCameraSharp,需确保树莓派系统已预装LibCamera(最新Raspberry Pi OS默认自带)。

内容的提问来源于stack exchange,提问作者Seszele

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.27 16:07:34