I am trying to use OpenCV to capture frames from a webcam and display them in a window using SFML.
VideoCapture returns frames in OpenCV Mat format. To display frames, SFML requires a 1D array of pixels in the uint8 format, which (as far as I can tell) is interchangeable with uchar. This array is expected to represent 32 bits per RGBA pixel.
So, I have a uchar array, and I iterate over the Mat data and copy each pixel:
VideoCapture cap(0); Mat frame; cap >> frame; uchar* camData = new uchar[640*480*4]; uchar* pixelPtr = frame.data; for(int i = 0; i < frame.rows; i++) { for(int j = 0; j < frame.cols; j++) { camData[i*frame.cols + j + 2] = pixelPtr[i*frame.cols + j + 0];
Unfortunately this does not work. Something in this loop is incorrect, since the resulting image is scrambled when loading and displaying camData.
As far as I can distinguish, either my math in the loop is wrong, so the pixels are assigned incorrectly, or the Mat data is in some format other than BGR.
Any ideas?
Thew
source share