OpenCV图像数据字节对齐
阅读原文时间:2023年07月10日阅读:2

目录

图像数据是否做字节对齐,可能影响到逐像素的遍历操作。OpenCV中的图像是HWC维度顺序存储,我们需要注意WC维度是否做4字节对齐。

这里使用的测试图如下:

注意它的宽度是471像素,是不能被4整除的。

const char* im_pth = "image.png";
IplImage* im = cvLoadImage(im_pth);

img->widthStep等于align_up(im->width*im->channels) //向上取整到最近的4的倍数

e.g.
width=471, channels=3, widthStep=1416

e.g.

    FcImage fc_im = fc_load_image(im_pth);
    int im_h = fc_im.h;
    int im_w = fc_im.w;
    int im_c = fc_im.c;

    cv::Mat image = cv::Mat();
    image.create(im_h, im_w, CV_8UC3);
    cout << image.step << endl; //1413

来看看不对齐是什么效果,这里使用了fc image,提供的是对齐的图像数据,然后拷贝给手动创建的Mat,也就是不做对齐的:

    FcImage fc_im = fc_load_image(im_pth);
    int im_h = fc_im.h;
    int im_w = fc_im.w;
    int im_c = fc_im.c;

    cv::Mat image = cv::Mat();
    image.create(im_h, im_w, CV_8UC3);
    for (int h = 0; h < im_h; h++) {
        for (int w = 0; w < im_w; w++) {
            for (int c = 0; c < im_c; c++) {
                int src_idx = h * im_w*im_c + w * im_c + c;
                int dst_idx = h * im_w*im_c + w * im_c + c;
                image.data[dst_idx] = fc_im.data[src_idx];
            }
        }
    }
    std::cout << image.step << std::endl;

    cv::imshow("image", image);
    cv::waitKey(0);

早期的opencv2可以这样写:

const char* im_pth = "image.png";
IplImage* ipl = cvLoadImage(im_pth);
Mat mat(ipl); //或Mat mat(ipl, true)表示拷贝数据

后来的opencv不让这么写了,需要用cvarrayToMat函数:

    IplImage* ipl = cvLoadImage(im_pth);
    int im_h = ipl->height;
    int im_w = ipl->width;
    cv::Mat mat = cv::cvarrToMat(ipl);

    cv::imshow("image", mat);
    cv::waitKey(0);

如果需要逐像素操作,那么最好是定义并使用linebytes属性来替代每一处使用到的im_width*im_channel,因为linebytes=align_up(im_width*im_channel, 4)

手机扫一扫

移动阅读更方便

阿里云服务器
腾讯云服务器
七牛云服务器

你可能感兴趣的文章