Hi everyone,
I am trying to read input from a camera and run Bayer conversion on windows. The camera is supposed to give 10 bit data. I found a working script in python, which I am trying to replicate in C++. But VideoCapture’s read function itself is giving different pixel values in python and C++. What would be the reason behind this?
Python Script
import cv2
import numpy as np
cam2 = cv2.VideoCapture(1)
cam2.set(cv2.CAP_PROP_CONVERT_RGB, 0); # Request raw camera data
while True:
b, frame = cam2.read()
if b:
print(frame[0,0])
frame_16 = frame.view(dtype=np.uint16) # reinterpret data as 16-bit pixels
frame_sh = np.right_shift(frame_16, 2) # Shift away the bottom 2 bits
frame_8 = (frame_sh).astype(np.uint8) # Keep the top 8 bits
img = frame_8.reshape(400, 400) # Arrange them into a rectangle
img = cv2.cvtColor(img,cv2.COLOR_BAYER_BG2RGB)
cv2.imshow("Video", img)
k = cv2.waitKey(5)
if k & 0xFF == 27:
cam2.release()
cv2.destroyAllWindows()
break
Output: frame.shape = (1,320000) and Pixel value at [0,0] is in the range of 55-68
C++
#include <stdio.h>
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
int main(int argc, char** argv )
{
Mat frame;
namedWindow("Display window");
VideoCapture cam(1);
if (!cam.isOpened()) {
cout << "cannot open camera";
}
cam.set(CAP_PROP_CONVERT_RGB, 0.0); //Request raw camera data
while (true) {
cam.read(frame);
// get the pointer (cast to data type of Mat)
uchar *pImgData = (uchar *)frame.data;
uchar value = pImgData[channels*(cols*0+0)+0];
cout<<unsigned(value)<<endl;
imshow("Display window", frame);
cnt++;
if(waitKey(1) == 'q')
{
destroyAllWindows();
break;
}
}
return 0;
}
Output: CV_8UC3, (400,400,3) and pixel values at 0th index is 255.