无法使用 cimg (returns 0) 访问图像中的像素强度

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

我试图访问 Cimg 像素值以打印出我的鼠标所在的像素强度,以及计算直方图。但是,我从 Cimg 对象中得到了所有零。

cimg图像是从内存缓冲区启动的,是12位灰度图像,但填充到16位以保存在内存中。 下面的代码定义在一个被多次调用的函数中。我想刷新当前显示中的图像,而不是每次调用该函数时都生成一个新图像。所以Cimgdisp是在函数外定义的。

#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;}
}

我检索的所有强度均为零,直方图也为零。

img*=(4095/65535);//Normalise back to get corect intensities 不正确,如 C/C++ 中的 (4095/65535)=0(整数除以较大的整数)。

也许 img*=(4095/65535.); ?

如果您只想在 12 位和 16 位之间进行缩放,那么使用位移位可能会更好。

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