首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将32位rgb图像转换为8位rgb C++

将32位rgb图像转换为8位rgb C++
EN

Stack Overflow用户
提问于 2016-05-13 10:43:58
回答 3查看 4K关注 0票数 0

我想把w的uint32_t图像数据转换成uint8_t图像。我该如何转换图像。我想把轻子子的PIX转换成opencv Mat。

我想知道使用按位运算符的困难方法。像素被打包为AARRGGBB。

另外,我也想皈依

简历:Mat 8UC1至PIX。即8位单通道图像到32位图像。或者你能想出其他办法来解决这个问题。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2016-05-26 05:13:34

以下是将cv::Mat转换为32位PIX格式的答案:

代码语言:javascript
复制
PIX* toPIX(const cv::Mat& img)
{
    if(img.empty())
        throw std::invalid_argument("Image is empty");

    //PIX has got 4 channels hence we need to add one
    //more channel to this image
    cv::Mat alphaImage;
    if(img.type() == CV_8UC3) {
        cv::cvtColor(img,alphaImage,cv::COLOR_BGR2RGBA);
    } else if(img.type() == CV_8UC1) {
        cv::Mat gray = img.clone();
        std::vector<cv::Mat> channelsPlusAlpha;

        //construct 4 channel by reapeating gray across 3 channels and filling alpha with constant value
        channelsPlusAlpha.push_back(gray);//channels[2]);//R
        channelsPlusAlpha.push_back(gray);//channels[1]);//G
        channelsPlusAlpha.push_back(gray);//channels[0]);//B
        channelsPlusAlpha.push_back(cv::Mat(img.size(),CV_8UC1,cv::Scalar::all(255)));//A
        cv::merge(channelsPlusAlpha,alphaImage);
    } else {
        throw std::logic_error("Image type undefined");
    }


    //Prepare PIX
    int w = alphaImage.cols;
    int h = alphaImage.rows;
    int bpl = alphaImage.step1();//bytes per line
    int bpp = alphaImage.channels();//bytes per pixel
    int top = 0; //x (0,0) or if we want to deal with sub image then those coordinates have to be fed
    int left = 0;//y
    uchar* image_data = alphaImage.data;

    //Create PIX
    PIX *pix = pixCreate(w,h,32);
    uint32_t* data = pixGetData(pix);
    int wpl = pixGetWpl(pix);//get the words per line

    const uint8_t* imagedata = image_data + top * bpl + left * bpp;

    for(int y=0; y < h; ++y) {
        const uint8_t* linedata = imagedata; // linescan
        uint32_t* line = data + y *wpl;
        for(int x =0; x < w; ++x) {
            line[x] =   (linedata[0] << 24) |
                        (linedata[1] << 16) |
                        (linedata[2] << 8)  |
                        linedata[3];

            linedata += 4;
        }
        imagedata += bpl;
    }

    return pix;
}
票数 1
EN

Stack Overflow用户

发布于 2016-05-13 12:59:44

如果您想用数据获取一个4通道的RGBA映像,可以尝试将每个uint32_t转换为4个无符号字符号。有关如何进行此操作的讨论,请参见Converting an int into a 4 byte char array (C)。完成此操作后,只需使用所获得的数据创建一个类型为cv::Mat的新CV_8UC4

票数 1
EN

Stack Overflow用户

发布于 2016-05-14 07:32:19

代码语言:javascript
复制
uint32_t * imData = yourImageDataPointer;
uchar * reinterpretedData = (uchar*)imData;

cv::Mat cvImage = cv::Mat(h,w, CV_8UC4);

cv::Mat bgr; // displayable image without alpha channel

cv::cvtColor(cvImage, bgr, CV_RGBA2BGR);

cv::imshow("img", bgr);
cv::waitKey (0);

如果结果不是你所期望的,请尝试这个,并发布结果图像。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37207961

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档