0

I am currently programming with a PixeLINK USB3 machine vision camera along with OpenCV in C. I have had some success passing camera images in Mat format with the following code:

PXL_RETURN_CODE rc = PxLInitialize(0, &hCamera);
if (!API_SUCCESS(rc))
{
    printf("Error: Unable to initialize a camera. \n");
    return EXIT_FAILURE;
}

vector<U8> frameBuffer(3000 * 3000 * 2);
FRAME_DESC frameDesc;

    if (API_SUCCESS(PxLSetStreamState(hCamera, START_STREAM)))
{
    while (true)
    {
        frameDesc.uSize = sizeof(frameDesc);
        rc = GetNextFrame(hCamera, (U32)frameBuffer.size(), &frameBuffer[0], 
                          &frameDesc, 5);

        Mat image(2592, 2048, CV_8UC1);
        Mat imageCopy;

        // Where passing of image data occurs
        int k = 0;
        for (int row = 0; row < 2048; row++)
        {
            for (int col = 0; col < 2592; col++)
            {
                image.at<uchar>(row, col) = frameBuffer[k];
                k++;
            }
        }...

As I mentioned this works, but it seems very sloppy. I have looked online but haven't found too much detail.

I have tried:

Mat image(2592, 2048, CV_8UC1, &frameBuffer, size_t step=AUTO_STEP); 

as well as,

Mat image(2592, 2048, CV_8UC1, frameBuffer, size_t step=AUTO_STEP). 

The former is the only one that compile successfully, and displays gibberish - I mean, it doesn't form an image.

Leonardo Alves Machado
  • 2,747
  • 10
  • 38
  • 53
RBRS1982
  • 3
  • 1
  • 2
  • The C tag here is not needed, you are using C++. On point, does the byte order of the file you are processing match that of your dev environment? – ryyker Apr 02 '18 at 15:50
  • Would you please elaborate little bit – udit043 Apr 02 '18 at 17:45
  • Thank you Leonardo for the reformat. Looks much nicer. As for your question, I am running on the same computer as my dev environment. – RBRS1982 Apr 03 '18 at 17:00

1 Answers1

0

Have you tried switching the row and col of your Mat?

You initialized your Mat with row = 2592, col = 2048,
but you're using switched row and col in your for() loop.

I think this code should work properly:

Mat image(2048, 2592, CV_8UC1, &frameBuffer[0]);

Or, if you're using C++11,

Mat image(2048, 2592, CV_8UC1, frameBuffer.data());
Suhyeon Lee
  • 569
  • 4
  • 18
  • Thank you Suhyeon! The swapping of rows and columns worked! Sorry for making such an obvious mistake. – RBRS1982 Apr 03 '18 at 17:01