1

I'm finding a way to convert RGBA bitmap to Mat in OpenCV. I tried this, but the output image had wrong color and was upside-down.

I use a struct array to store the bitmap.

typedef struct {
    unsigned char r,g,b,a;
} BITMAP;
BITMAP* input = (BITMAP*)malloc(width*height*sizeof(BITMAP));
...
Mat image(height, width, CV_8UC4, input);

Does anyone know any convenient method to convert Bitmap to Mat in OpenCV?

Community
  • 1
  • 1
Galaxy
  • 853
  • 2
  • 11
  • 28

1 Answers1

2

OpenCV's cv::Mat uses row major ordering (increment first along channels, then along columns, then along rows). So, the pixel at (0, 0) is the most top-left pixel in the image. If your BITMAP had (0, 0) as the bottom-left, then the image would appear upside down. Typically, OpenCV prefers BGRA ordering of the channels. The reordering of the channels will mess up the coloring.

Robert Prévost
  • 1,667
  • 16
  • 22