Ragavan
Ragavan

Reputation: 1

how to convert pixels values to display image in opencv

I'm unable to process the pixel values (unsigned int) to convert into an image. I want the syntax which converts the pixel value into image format. cvset (all type of commands) is not working, I'm getting only a particular color image as if only red or only green etc. Please help me with a syntax for reconstructing the pixel values into a image. I'm able to convert image to pixels but I'm unable to convert the image back from these same pixel values.

Upvotes: 0

Views: 2283

Answers (1)

karlphillip
karlphillip

Reputation: 93410

How many channels are there in the image you are working with? This is an important information. Keep in mind that grayscale images have typically 1 channel, while colored images loaded by OpenCV usually have 3 channels (R, G and B).

To work with a 3-channel image in C, you could do:

IplImage* pRGBImg = cvLoadImage("img.png", CV_LOAD_IMAGE_UNCHANGED); 
int width = pRGBImg->width; 
int height = pRGBImg->height;
int bpp = pRGBImg->nChannels; 

for (int i=0; i < width*height*bpp; i+=bpp) 
{

  pRGBImg->imageData[i] = r; // RED pixel
  pRGBImg->imageData[i+1] = g; // GREEN
  pRGBImg->imageData[i+2] = b; // BLUE 
}

For single-channel images, the core of the loop would be much simpler as you would only need to operate on img->imageData[i].

Upvotes: 2

Related Questions