Aditya Raman
Aditya Raman

Reputation: 319

Loading an Opencv Matrix from raw data of a 3 channel color image

As I am an absolute beginner to opencv, I keep struggling with the piece of code to load a color image from byte array to opencv matrix. The result that is diplayed is a grey image and not the desired color one.

Loading an Opencv Matrix from raw data of a 3 channel color image. Code is as following:

uchar image_data[200*200*3];

for(int i=0;i<200;i++)

for(int j=0;j<200;j++)

{           image_data[i*200*3+j]=255;

        image_data[i*200*3+j+1]=0;

        image_data[i*200*3+j+2]=0;
}


cv::Mat image_as_mat(Size(200,200),CV_8UC3,image_data);

namedWindow("DisplayVector2",CV_WINDOW_AUTOSIZE);

imshow("DisplayVector2",image_as_mat);

waitKey(0);

Upvotes: 0

Views: 1283

Answers (1)

bhawesh
bhawesh

Reputation: 1320

Your for loop putting wrong value in the array. you are overriding array values in your loop. as per i understand you wanted your image to be red. change your for loop like this

for(int i = 0; i <200; i++)
{
    for(int j = 0; j < 200; j++)
    {
        image_data[i*3*200 + j*3 + 0] = 255;
        image_data[i*3*200 + j*3 + 1] = 0;
        image_data[i*3*200 + j*3 + 2] = 0;
    }
}

Upvotes: 2

Related Questions