Trouble Setting Exposure/Gain with Emgu.CV (or OpenCV) with Scientific Camera

85 views Asked by At

OS: Windows 2008 R2

Camera Model: Sony 20 MP IMX Camera

Emgu.CV version: 3.1.0.1

.NET Framework version: 4.7.2

I'm encountering an issue where, after changing the exposure of an nncam camera using their documentation, any frames I grab don't update after using a capture button utilizing Emgu.CV query frame. I need to be able to adjust the Exposure and Gain because the application I am using this for relies on the ability to adjust the exposure manually.

Here's the relevant code:

private void SetExposureFromTextbox()
{
    if (cam_ == null)
    {
        MessageBox.Show("Camera not initialized.");
        return;
    }

    uint newExposure;
    if (uint.TryParse(exposureTextBox.Text, out newExposure))
    {
        cam_.put_ExpoTime(newExposure);
    }
    else
    {
        MessageBox.Show("Invalid exposure value.");
    }
}

private void GrabAndDisplayFrame()
{
    using (Mat frame = capture.QueryFrame())
    {
        if (frame != null)
        {
            pictureBox1.Image = frame.ToImage<Bgr, byte>().Bitmap;
            //Retrieve exposure value from the camera
            uint updatedCameraExposure;
            cam_.get_ExpoTime(out updatedCameraExposure);
            currentExposureTextBox.Text = updatedCameraExposure.ToString();
        }
    }
}

private void capture_push_Click(object sender, EventArgs e)
{
    // Dispose of the current capture object
    if (capture != null)
    {
        capture.Dispose();
        capture = null;
    }
    // Create a new capture object
    capture = new Capture(0); // nncam is the default camera.
    
    SetExposureFromTextbox();
    GrabAndDisplayFrame();
}

Am I missing something? Is it not possible to use Emgu.CV as a "FrameGrabber" and utilize the native camera's Exposure/Gain controls (cam_.get_ExpoTime in this case)?

I adjusted the exposure time via the camera's controls, anticipating the frames to reflect the new exposure settings. However, even though the camera confirmed the exposure alteration, the image displayed in the picturebox remained unaffected. I have also turned off the autoexposure property of the camera as well during initialization.

private void InitializeCamera()
{
    Nncam.DeviceV2[] arr = Nncam.EnumV2();
    if (arr.Length > 0)
    {
        cam_ = Nncam.Open(arr[0].id); // Assign the camera object to the cam_ field

        // Turn off autoexposure
        cam_.put_AutoExpoEnable(false);

        // Set exposure and gain
        cam_.put_ExpoTime(1001);
    }
    else
    {
        MessageBox.Show("No Nncam camera found.");
    }

    // Initialize Emgu.CV capture
    capture = new Capture(0); // Assuming the Nncam camera is the default camera
}
0

There are 0 answers