What is the C++ code to obtain gray scale image pixel intensity values using CImg library ?

Hi, everyone I’m a beginner to C++ and raspberrypi, Here I’m working on a project to capture the real time images using Raspberrypi CSI camera and to perform simple image processing operation in C++ using CImg library.

That is, to detect the bright spot in an image (such as LED light glow) and if detects the bright spot, output should be given as ‘1’ and if not (when the LED is off ) output should be given as ‘0’.

Note: The image processing has to be done only in the specific portion of an image (i.e focus only on the led section of an image and neglecting remaining portion of an image which is out of interest, such that it saves processing time and improves performance)

But firstly I don’t know how to get pixel intensity values of a gray scale image using CImg library such that we can compare the values obtained in the pixel to the threshold value and problem could be solved ( this is my algorithm for the above operation but any other simpler methods for the above processing ,your suggestions are always welcome )

Could you assist me in a C++ code or algorithm to perform the above task.
Thanks in advance.

This code opens an imagefile and reads the pixel value at coordinate (10,10). I leave the rest to you.

1
2
3
4
5
6
7
8
9
10
11
12
13
#include "CImg.h"
#include <iostream>
using namespace cimg_library;
int main()
{
  CImg<float> image("hills.png");
  CImgDisplay main_disp(image);
  float pixvalR = image(10,10,0,0); // read red val at coord 10,10
  float pixvalG = image(10,10,0,1); // read green val at coord 10,10
  float pixvalB = image(10,10,0,2); // read blue val at coord 10,10
  std::cout << "R = " << pixvalR << ", G = " << pixvalG << ", B = " << pixvalB;
  std::cin.ignore();
}
Hi Moschops, that's great ! thank you so much, it is good for obtaining RGB values if it is a color image, but in my case i'm using gray scale images. so to obtain pixel intensity values for the gray scale could you suggest what can i modify from the above code.
As far as i know Depth (z) = 1 and V (color) = ? i'm not quite sure, can you suggest me

And also I would be glad to know the difference between these two syntaxes
CImgDisplay main_disp(image); AND

image.display("Window Title");

*the display function that you mentioned in the code doesn't display the image, it just shows a blank screen and it quits immediately, Can you tell me why this happens ?

thanks in advance
Last edited on
Topic archived. No new replies allowed.