@@ -17,7 +17,21 @@ OCVCamera::OCVCamera(int width, int height, int fps, int index) :
1717 }
1818 is_valid = true ;
1919
20- w_scale = (float )width/(float )cam_native_width;
20+
21+ if (width < 0 || height < 0 )
22+ {
23+ this ->width = cam_native_width;
24+ this ->height = cam_native_height;
25+ }
26+
27+ if (fps < 0 )
28+ this ->fps = cam_native_fps;
29+
30+
31+ cap.set (cv::CAP_PROP_FRAME_WIDTH, this ->width );
32+ cap.set (cv::CAP_PROP_FRAME_HEIGHT, this ->height );
33+ cap.set (cv::CAP_PROP_FPS, this ->fps );
34+
2135 exposure, gain = -1 ;
2236}
2337
@@ -39,7 +53,9 @@ bool OCVCamera::is_camera_available()
3953 if (frame.empty ())
4054 return false ;
4155
42- cam_native_width = cap.get (cv::CAP_PROP_FRAME_WIDTH);
56+ cam_native_width = (int )cap.get (cv::CAP_PROP_FRAME_WIDTH);
57+ cam_native_height = (int )cap.get (cv::CAP_PROP_FRAME_HEIGHT);
58+ cam_native_fps = (int )cap.get (cv::CAP_PROP_FPS);
4359 cap.release ();
4460 }
4561 return available;
@@ -63,9 +79,6 @@ void OCVCamera::get_frame(uint8_t* buffer)
6379{
6480 cv::Mat frame;
6581 cap.read (frame);
66- // Scale maintaining aspect ratio. If distorted, the model will get confused.
67- // TODO: Maybe cropping (width,height) section from the center is better.
68- cv::resize (frame, frame, size, w_scale, w_scale);
6982 cv::flip (frame, frame, 1 );
7083 for (int i = 0 ; i < frame.cols * frame.rows * 3 ; i++)
7184 buffer[i] = frame.data [i];
@@ -74,16 +87,15 @@ void OCVCamera::get_frame(uint8_t* buffer)
7487
7588void OCVCamera::set_settings (CameraSettings& settings)
7689{
77- this ->width = settings.width ;
78- this ->fps = settings.fps ;
79- this ->height = settings.height ;
80- w_scale = (float )width / (float )cam_native_width;
81-
82- // Opencv needs [0,1] ranges
83- exposure = settings.exposure < 0 ? -1 .0F : (float )settings.exposure /255 ;
84- gain = settings.gain < 0 ? -1 .0F : (float )settings.gain / 64 ;
85- cap.set (cv::CAP_PROP_EXPOSURE, exposure);
86- cap.set (cv::CAP_PROP_GAIN, gain);
90+ this ->width = settings.width > 0 ? settings.width : this ->cam_native_width ;
91+ this ->height = settings.height > 0 ? settings.height : this ->cam_native_height ;
92+ this ->fps = settings.fps > 0 ? settings.fps : this ->cam_native_fps ;
93+
94+ // Disabled for the moment because of the different ranges in generic cameras.
95+ // exposure = settings.exposure < 0 ? -1.0F : (float)settings.exposure/255;
96+ // gain = settings.gain < 0 ? -1.0F : (float)settings.gain / 64;
97+ // cap.set(cv::CAP_PROP_EXPOSURE, exposure);
98+ // cap.set(cv::CAP_PROP_GAIN, gain);
8799}
88100
89101CameraSettings OCVCamera::get_settings ()
0 commit comments