Anubhav Rohatgi
Anubhav Rohatgi

Reputation: 458

Convert 32bit rgb image to 8 bit rgb C++

I want to convert uint32_t image data of w x h to uint8_t image. How should I convert the image. I want to convert leptonica's PIX to opencv Mat.

I would want to know the hard way using the Bitwise operators. The pixels are packed as AARRGGBB.

Also I would like to convert

cv:Mat 8UC1 to PIX. i.e. 8bit single channel image to 32bit image. Or if you can think of any other way to get this sorted.

Upvotes: 0

Views: 4732

Answers (3)

Anubhav Rohatgi
Anubhav Rohatgi

Reputation: 458

Here is the Answer to convert the cv::Mat to leptonicas 32bit PIX format:

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;
}

Upvotes: 1

Micka
Micka

Reputation: 20130

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);

please try this and post the result image if it's not what you expected

Upvotes: 1

Sunreef
Sunreef

Reputation: 4542

If you want to obtain a 4-channel RGBA image with your data, you can try to convert each of your uint32_t to 4 unsigned char numbers. See Converting an int into a 4 byte char array (C) for a discussion on how to do this. Once this is done, you only have to create a new cv::Mat of type CV_8UC4 with the data you obtained.

Upvotes: 1

Related Questions