Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

1d array to grayscale QImage issue



  • Hello everyone.

    i need your help. I try to display an image which is store on a 1d array. The image is 8288 *5644 grayscale of 16bits/pixel.
    for manage this i tried this:

        QImage I((unsigned char*) Image, width , height, width*2, QImage::Format_Grayscale16);
        QPixmap px = QPixmap::fromImage(I);
        ui->label->setPixmap(px);
        ui->label->setScaledContents( true );
    

    i let here two image, one which is the result of the code on the top and the other which is the same image that i exported in 2d csv table and ploted using matplotlib:

    of course i can give you the code if the error is not clear to see with this few lines and the data file for debbuging . But they are nothing more than that.

    thank you in advance

    result
    matplotlib plot



  • 2 things:

    • try removing the width*2 argument (Format_Grayscale16 already knows it's 16 bits per pixel)
    • try removing ui->label->setScaledContents( true ); it might be the scaling algorithm messing up your image. Instead of showing it in a label try saving it to file to see the result


  • thank you for your reply and for your time. I tried what you said: save the image give the same result, dont put the *2 next to the width display nothing. i also tried to convert it in 8bits image and just use:

    QImage I((unsigned char*) Image, width , height, width, QImage::Format_Grayscale8);
    

    but that not function.



  • @superdu said in 1d array to grayscale QImage issue:

    dont put the *2 next to the width display nothing.

    No, I meant: QImage I((unsigned char*) Image, width , height, QImage::Format_Grayscale16);

    If that still doesn't work you need to tell us what Image is



  • here the data: transfernow.net/dl/20210608V8bNa7fC

    the code for read data:

    fit.cpp

    #include "fit.h"
    
    
    void Fit::open(const char* path, img * Img) {
    
        int status = 0;
        int nullval = 0;
        int anynul = 0;
        fitsfile* fptr;
        fits_open_file(&fptr, path, READONLY, &status); // open file
        fits_get_img_dim(fptr, &Img->dim, &status);	// get dim of image (color or gray scale basicly
        Img->size = (long*)malloc(Img->dim * sizeof(long));
        fits_get_img_size(fptr, 2, Img->size, &status); // get size of image (pixels)
        Img->totalPixels = getTotalPixel(Img->dim, Img->size);
        Img->data = (int*)malloc(Img->totalPixels * sizeof(int));
        fits_read_img(fptr, ULONG_IMG, 1, Img->totalPixels, &nullval, Img->data, &anynul, &status);
        fits_close_file(fptr, &status);
    
    }
    
    
    
    int Fit::getTotalPixel(int dim, long* size) {
        int multOfAllPixel = 1;
        for (int i = 0; i < dim; i++)
            multOfAllPixel *= size[i];
        return multOfAllPixel;
    
    }
    

    fit.h

    #ifndef FIT_H
    #define FIT_H
    
    #include "cfitsio/fitsio.h"
    #include "GlobalStruct.h"
    #include <vector>
    #include <utility>
    
    using namespace std;
    class Fit
    {
    
    public:
    
        static void open(const char* path, img* Img);
    
    
    
    private:
        static int getTotalPixel(int dim, long * size);
    };
    
    
    
    #endif // FIT_H
    
    

    GlobalStruct

    #ifndef FIT_H
    #define FIT_H
    
    #include "cfitsio/fitsio.h"
    #include "GlobalStruct.h"
    #include <vector>
    #include <utility>
    
    using namespace std;
    class Fit
    {
    
    public:
    
        static void open(const char* path, img* Img);
    
    
    
    private:
        static int getTotalPixel(int dim, long * size);
    };
    
    
    
    #endif // FIT_H
    
    

    mainwindow

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include "GlobalStruct.h"
    #include "fit.h"
    #include <fstream>
    #include<iostream>
    
    
    using namespace std;
    
    MainWindow::MainWindow(QWidget *parent)
        : QMainWindow(parent)
        , ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
        const char* path = "c:/.../light_m51_60.0s_bin1_l_gain60_20210515-015357_-9.4c_0001.fit";
        img Img;
        Fit::open(path, &Img);
    
    
        float * newImg = (float*)malloc(Img.totalPixels * sizeof(float)); // just for rescale the dynamics of the image
        for(int i = 0; i < Img.totalPixels; i++){
            newImg[i]=(Img.data[i] - 1000.0) / (9000-1000);
            if(newImg[i] < 0)
                newImg[i] = 0;
            if(newImg[i] > 1)
                newImg[i] = 1;
            Img.data[i]=(int)(65535 * newImg[i]);
    
        }
    
    
        QImage I((unsigned char*) Img.data, Img.size[0], Img.size[1], Img.size[0]*2, QImage::Format_Grayscale16);
        QPixmap px = QPixmap::fromImage(I);
        ui->label->setPixmap(px);
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    


  • someone have an idea?


Log in to reply