简体   繁体   中英

How to convert RGBA array into Mat in OpenCV

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM