Conversion of mat image in rrggbb format

Hello I am trying to convert a Mat image in RRGGBB format. According to my knowledge , RRGGBB information should have 4 channels and type should be CV_8UC4. However, I am not getting the correct number of channels after conversion in rrggbb format.

cv::Mat fin_img;
  fin_img = cv::imread("/home/laddu/pattern_dataset/221.png");
  Mat rgb_img;
  cvtColor(fin_img,rgb_img,COLOR_BGR2RGB);
  Mat rrggbb_img(rgb_img.rows, rgb_img.cols, CV_8UC4);

    // Define color channel mapping
  int from_to[] = {0, 1, 1, 2, 2, 3, -1, 0}; // RRGGBB format: R -> B0, G -> B1, B -> B2

   // Rearrange color channel values using mixChannels()
  mixChannels(&rgb_img, 1, &rrggbb_img, 1, from_to, 4);
  // Save image in RRGGBB format
  imwrite("/home/laddu/rrggbb_image.png", rrggbb_img);

  Mat ex_img = cv::imread("/home/laddu/rrggbb_image.png");

 if(ex_img.channels()==4 && ex_img.type()==CV_8UC4)
  {
    cout<<"IMAGE IS IN RRGGBB FORMAT";
  }
  else{
    cout<<"IMgae is not in RRGGBB format";
  }
  

never heard of that.

do you come from a web development background?

why should three colors be four or even six colors?

your from_to array has more than four elements defined but you tell mixChannels to use only the first four.

how would that be ? please cite sources.

can it be, you’re simply mislead, and looking for a way to print out 2 digit hex values ?

I might be wrong too. I remember reading it somewhere but I may be completely wrong too. I am just looking for a way to convert a grayscale image (mat format) in hex 0xRGB888 format. This is my current code for trying to convert in hex rgb888 format. However, the conversion i am getting is wrong. To give more information, so I am trying to use this recent Deep Learning model named “FOMO” for object detection. However, to make my input data compatible with their code, I need to get the input in hex format 0xRRGGBB This is my updated code :slight_smile: `cv::Mat fin_img;
fin_img = cv::imread(“/home/laddu/pattern_dataset/360.png”,0);
resize(fin_img, fin_img, Size(350,350));
// imshow(“hii”,fin_img);
// waitKey(0);

cv::cvtColor(fin_img, fin_img, cv::COLOR_BGR2RGB);
Mat img_flat = fin_img.reshape(1,fin_img.total()) ;

cv::Mat img_hex = cv::Mat::zeros(img_flat.rows, 1, CV_32SC1);
for(int i = 0; i<img_flat.rows;i++)
{
uchar* pixel = img_flat.ptr(i);
int hex_value = (pixel[0]<<16)|(pixel[0]<<8)|pixel[0];
img_hex.at(i,0) = hex_value;
}

ofstream outfile(“hex_prar_values.txt”);

for (int i = 0; i<img_hex.rows;i++)
{
outfile << “0x” << hex << setw(1) << setfill(‘0’) << img_hex.at(i, 0) <<“,”;
}
outfile.close();`

NOW, we’re talking !

link, please !

https://docs.edgeimpulse.com/docs/deployment/running-your-impulse-locally/deploy-your-model-as-a-c-library When you read in the explaination of c++ library, In signal data you can find about the format. I want to convert my live stream of cv::mat images in their format. I am not understanding how to proceed so?

1 Like

you misunderstand quite a bit about all of this.

your heuristic should be to ASK about every thing you read, and get someone to confirm or correct your understanding, before you base anything on that understanding and carry on.

you found a library that is intended for embedded programming. it is C/C++. that is not for beginners. you should learn C first, and gain experience, before you have the necessary background to deal with that library competently.

I peeked into this “edgeimpulse” thing. all they say is that a 6-nibble hex string contains the 24 bits that are customarily used to hold the RGB data of a pixel.

“0xRRGGBB”, first, is not a valid numeral in any programming language, because R and G aren’t hex digits.

it’s a pattern for humans to understand, which says that the lowest two nibbles (one byte) hold the blue value, the next higher bytes holds the green value, and then comes the red value. one could say:

pixel = (red << 16) | (green << 8) | (blue)

such a compound value is useless to neural networks because it’s code, not a scalar value.

neural networks need the values separated, i.e. red, green, blue values individually.

well, you should have read that page more carefully !!!
(before writing bs code like above …)

in short:

  • you’re supposed to give a callback function for a signal_t struct, that reads ‘chunks’ of your data (not the whole image at once):

    signal.total_length = EI_CLASSIFIER_DSP_INPUT_FRAME_SIZE;
    signal.get_data = &get_signal_data;

  • where you’d pack some piece of the image into a float*(awww), like this:


(converting bgr to rgb input is as easy as:
float pixel_f = (b << 16) + (g << 8) +r;

again, please have a close look at the example here,

and dont write any code, until you 100% understood it, please !