Still Learning...
Still Learning...

Reputation: 33

can't access pixel intensities in image using cimg (returns 0)

I'm tries to access Cimg pixel values to print out the pixel intensity that my mouse is at, as well as calculating a histogram. However, I got all zeros from Cimg object.

The cimg image is initiated from memory buffer and it is 12 bit gray scale image, but padded to 16 bit to save in memory. The code below is defined in a function that is called multiple times. I want to refresh the images in the current display and not to produce a new one every time the function is called. So the Cimgdisp is defined outside the function.

#include "include\CImg.h"
int main(){
    CImg <unsigned short> image(width,height,1,1);
    CImgDisplay           disp(image);
//showImg() get called multiple times here

}

void showImg(){
    unsigned short* imgPtr = (unsigned short*) (getImagePtr());
    CImg <unsigned short> img(imgPtr,width,height);

    img*=(65535/4095);//Renormalise from 12 bit input to 16bit for better display

    //Display 
    disp->render(img);
    disp->paint();
    img*=(4095/65535);//Normalise back to get corect intensities

    CImg <float> hist(img.histogram(100));
    hist.display_graph(0,3);

    //find mouse position and disp intensity
    mouseX = disp->mouse_x()*width/disp->width();//Rescale the position of the mouse to true position of the image
    mouseY = disp->mouse_y()*height/disp->height();
    if (mouseX>0&mouseY>0){
        PxIntensity = img(mouseX,mouseY,0,0);}
    else {
        PxIntensity = -1;}
}

All the intensities I retrieve are zero and the histogram is also zero.

Upvotes: 0

Views: 379

Answers (2)

PeterT
PeterT

Reputation: 8284

If you just want to scale between 12-bit and 16-bit and back then just using bit-shifts might be better.

img<<=4;//Renormalise from 12 bit input to 16bit for better display

//Display 
disp->render(img);
disp->paint();
img>>=4;//Normalise back to get corect intensities

Upvotes: 1

bvalabas
bvalabas

Reputation: 66

img*=(4095/65535);//Normalise back to get corect intensities is incorrect, as (4095/65535)=0 in C/C++ (division of an integer by a larger one).

Maybe img*=(4095/65535.); ?

Upvotes: 2

Related Questions