'Loading & Saving Bitmap Images In OpenCV (OpenCV Version = 3.1.0)

My problem is simple at least as I see, or how I feel about it.

I tried to load and write Bitmap images with OpenCV in C++ and got sucessed in it, my problem is about the way the image is loaded and written.

Here is the thing: I working on 256-color palette (8bpp), trying to edit or add somethings on it, and then I write it or you can say I save it. The problem is that the color palette before and after loading the image is not the same. It seems like OpenCV is not just load the image but also change the palette color, in my case it is changed to RGB color (24bpp) which cause of an output image with size 3 x original image size after I saved the loaded image.

My question is there a way to load it or write without changing image color palette? if not possible, is there a way to write it with specific color palette? so I can specify it to 256-color palette (8bpp), if not possible either any possible converter will be good (from RGB color (24bpp) to 256-color palette (8bpp)).

Here is my code to load and write the Bitmap image:

#include <opencv2/opencv.hpp>
#include <iostream>

using namespace cv;

int main()
{
    Mat BitImage = imread("BitImage_Intput.bmp", -1); // -1 To load image as it is (without changing) but not working.
    imwrite("BitImage_Output.bmp", BitImage); // To write or save the image.
    waitKey(0);
    return 0;
} 

I found this function, imread("BitImage_Intput.bmp", CV_8UC1);, It does the job in a side but ruins it on the other side, the color palette not changed but I got a GRAYSCALE image (All colors except white and black are removed or replaced).

The 256-color palette (8bpp) Bitmap Image

The RGB color (24bpp) Bitmap Image

The GRAYSCALE (8bpp) Bitmap Image



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source