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

How to create a 2d array from a QImage?



  • I need to get a 2d array from a QImage to use in prediction.
    Is there any way to do this using only Qt classes (or maybe using openCV)?

    Thanks!!



  • Are you trying to convert from QImage to cv::mat?
    If so you may be able to skip the process with a function from this helpful guy over here. He helped me with all my opencv issues with qt.

    QImage frmMain::convertOpenCVMatToQtQImage(cv::Mat mat) {
        if(mat.channels() == 1) {                   // if grayscale image
            return QImage((uchar*)mat.data, mat.cols, mat.rows, mat.step, QImage::Format_Indexed8);     // declare and return a QImage
        } else if(mat.channels() == 3) {            // if 3 channel color image
            cv::cvtColor(mat, mat, CV_BGR2RGB);     // invert BGR to RGB
            return QImage((uchar*)mat.data, mat.cols, mat.rows, mat.step, QImage::Format_RGB888);       // declare and return a QImage
        } else {
            qDebug() << "in convertOpenCVMatToQtQImage, image was not 1 channel or 3 channel, should never get here";
        }
        return QImage();        // return a blank QImage if the above did not work
    }
    


  • @Todd-Morehouse I'm trying to do the opposite of this, I already have the QImage and now I need to create an array from this QImage.
    something like that:

    [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.
      0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
    [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.
      0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
    [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.
      0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
    [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.
      0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
    [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.
      0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
    [ 0.          0.          0.          0.          0.          0.          0.
      0.          0.          0.          0.          0.          0.01176471
      0.07058824  0.07058824  0.07058824  0.49411765  0.53333336  0.68627453
      0.10196079  0.65098041  1.          0.96862745  0.49803922  0.          0.
      0.          0.        ]
    [ 0.          0.          0.          0.          0.          0.          0.
      0.          0.11764706  0.14117648  0.36862746  0.60392159  0.66666669
      0.99215686  0.99215686  0.99215686  0.99215686  0.99215686  0.88235295
      0.67450982  0.99215686  0.94901961  0.7647059   0.25098041  0.          0.
      0.          0.        ]
    [ 0.          0.          0.          0.          0.          0.          0.
      0.19215687  0.93333334  0.99215686  0.99215686  0.99215686  0.99215686
      0.99215686  0.99215686  0.99215686  0.99215686  0.98431373  0.36470589
      0.32156864  0.32156864  0.21960784  0.15294118  0.          0.          0.
      0.          0.        ]
    [ 0.          0.          0.          0.          0.          0.          0.
      0.07058824  0.85882354  0.99215686  0.99215686  0.99215686  0.99215686
      0.99215686  0.7764706   0.71372551  0.96862745  0.94509804  0.          0.
      0.          0.          0.          0.          0.          0.          0.
      0.        ]
    [ 0.          0.          0.          0.          0.          0.          0.
      0.          0.3137255   0.61176473  0.41960785  0.99215686  0.99215686
      0.80392158  0.04313726  0.          0.16862746  0.60392159  0.          0.
      0.          0.          0.          0.          0.          0.          0.
      0.        ]
    [ 0.          0.          0.          0.          0.          0.          0.
      0.          0.          0.05490196  0.00392157  0.60392159  0.99215686
      0.35294119  0.          0.          0.          0.          0.          0.
      0.          0.          0.          0.          0.          0.          0.
      0.        ]
    [ 0.          0.          0.          0.          0.          0.          0.
      0.          0.          0.          0.          0.54509807  0.99215686
      0.74509805  0.00784314  0.          0.          0.          0.          0.
      0.          0.          0.          0.          0.          0.          0.
      0.        ]
    [ 0.          0.          0.          0.          0.          0.          0.
      0.          0.          0.          0.          0.04313726  0.74509805
      0.99215686  0.27450982  0.          0.          0.          0.          0.
      0.          0.          0.          0.          0.          0.          0.
      0.        ]
    [ 0.          0.          0.          0.          0.          0.          0.
      0.          0.          0.          0.          0.          0.13725491
      0.94509804  0.88235295  0.627451    0.42352942  0.00392157  0.          0.
      0.          0.          0.          0.          0.          0.          0.
      0.        ]
    [ 0.          0.          0.          0.          0.          0.          0.
      0.          0.          0.          0.          0.          0.
      0.31764707  0.94117647  0.99215686  0.99215686  0.46666667  0.09803922
      0.          0.          0.          0.          0.          0.          0.
      0.          0.        ]
    [ 0.          0.          0.          0.          0.          0.          0.
      0.          0.          0.          0.          0.          0.          0.
      0.17647059  0.72941178  0.99215686  0.99215686  0.58823532  0.10588235
      0.          0.          0.          0.          0.          0.          0.
      0.        ]
    [ 0.          0.          0.          0.          0.          0.          0.
      0.          0.          0.          0.          0.          0.          0.
      0.          0.0627451   0.36470589  0.98823529  0.99215686  0.73333335
      0.          0.          0.          0.          0.          0.          0.
      0.        ]
    [ 0.          0.          0.          0.          0.          0.          0.
      0.          0.          0.          0.          0.          0.          0.
      0.          0.          0.          0.97647059  0.99215686  0.97647059
      0.25098041  0.          0.          0.          0.          0.          0.
      0.        ]
    [ 0.          0.          0.          0.          0.          0.          0.
      0.          0.          0.          0.          0.          0.          0.
      0.18039216  0.50980395  0.71764708  0.99215686  0.99215686  0.81176472
      0.00784314  0.          0.          0.          0.          0.          0.
      0.        ]
    [ 0.          0.          0.          0.          0.          0.          0.
      0.          0.          0.          0.          0.          0.15294118
      0.58039218  0.89803922  0.99215686  0.99215686  0.99215686  0.98039216
      0.71372551  0.          0.          0.          0.          0.          0.
      0.          0.        ]
    [ 0.          0.          0.          0.          0.          0.          0.
      0.          0.          0.          0.09411765  0.44705883  0.86666667
      0.99215686  0.99215686  0.99215686  0.99215686  0.78823531  0.30588236
      0.          0.          0.          0.          0.          0.          0.
      0.          0.        ]
    [ 0.          0.          0.          0.          0.          0.          0.
      0.          0.09019608  0.25882354  0.83529413  0.99215686  0.99215686
      0.99215686  0.99215686  0.7764706   0.31764707  0.00784314  0.          0.
      0.          0.          0.          0.          0.          0.          0.
      0.        ]
    [ 0.          0.          0.          0.          0.          0.
      0.07058824  0.67058825  0.85882354  0.99215686  0.99215686  0.99215686
      0.99215686  0.7647059   0.3137255   0.03529412  0.          0.          0.
      0.          0.          0.          0.          0.          0.          0.
      0.          0.        ]
    [ 0.          0.          0.          0.          0.21568628  0.67450982
      0.88627452  0.99215686  0.99215686  0.99215686  0.99215686  0.95686275
      0.52156866  0.04313726  0.          0.          0.          0.          0.
      0.          0.          0.          0.          0.          0.          0.
      0.          0.        ]
    [ 0.          0.          0.          0.          0.53333336  0.99215686
      0.99215686  0.99215686  0.83137256  0.52941179  0.51764709  0.0627451   0.
      0.          0.          0.          0.          0.          0.          0.
      0.          0.          0.          0.          0.          0.          0.
      0.        ]
    [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.
      0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
    [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.
      0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
    [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.
      0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
    

    That is a numpy array created from MNIST dataset image but using pyton and I need to recreate this in c++.


  • Qt Champions 2017

    @OliveiraNT
    An image is a compound object, what exactly you want to extract from the image, a specific color channel?

    PS.
    Do you want to get the array(s) in pyton?



  • @kshegunov
    This array was created using this code in python

    (X_train, y_train), (X_test, y_test) = mnist.load_data()
    X_train = X_train.reshape(X_train.shape[0], 1, img_rows, img_cols)
    X_train = X_train.astype('float32')
    X_train /= 255
    

    and I need reproduce this in c++ but now using a QImage as a source.


  • Qt Champions 2017

    Something along those lines should be just enough (haven't tested it though):

    QImage image; //< This has to be 8 bit monochrome image
    
    qint32 width = image.width(), height = image.height();
    quintptr scanLineSize = static_cast<quintptr>(width & 0x3 ? width + 1 : width) << 2;
    
    const quint8 * data = reinterpret_cast<const quint8 *>(image.constBits());
    
    QVector<QVector<float>> matrix;
    
    matrix.resize(height);
    for (qint32 row = 0; row < height; row++)  {
        matrix[row].resize(width);
        for (qint32 column = 0; column < width; column++)
            matrix[row][column] = data[column] / 255.0f;
        data += scanLineSize;
    }
    


  • @kshegunov I'll test this soonest as possible and post the result here.
    Thanks.



  • @kshegunov
    I test your code and its seems just like what I need.
    Really thanks for your help.
    Now I using a QVectorIterator to output matrix on console with qDebug but what I need is create a texte file with this, I tryed to do this using QFile but it fail because QFile is not overloaded to read a vector . Do you know some way to do this?


  • Qt Champions 2017

    That's a good place to use QTextStream. It doesn't have an overloaded operator for vectors, but you can always provide one by yourself, e.g:

    template <class T>
    QTextStream & operator << (QTextStream & out, const QVector<QVector<T>> & data)
    {
        qint32 rows = data.size(), cols = rows > 0 ? data.first().size() : -1;
        if (cols <= 0)  {  //< We passed invalid data
            out.setStatus(QTextStream::WriteFailed); //< Indicate an error
            return out;
        }
    
        // Write the dimensions first (we can do without, but it's better to have them)
        out << rows << ' ' << cols << endl;
        // Write the data row by row, element by element
        for (qint32 i = 0; i < rows; i++)  {
            if (i > 0)
                out << endl;
            for (qint32 j = 0; j < columns; j++)  {
                if (j > 0)
                    out << ' ';
                out << data[i][j];
            }
        }
    
        return out;
    }
    

    You can then use this with a QTextStream object:

    QVector<QVector<float>> matrix;
    
    QFile file("myfile.txt");
    if (!file.open(QFile::WriteOnly | QFile::Text | QFile::Truncate))
        ; //< Errors should be handled appropriately
    
    QTextStream out(&file);
    out << matrix;
    


  • @kshegunov Thank you so much for your help I really learned a lot about Qt and now I'm close to finish my project. Again, thank you for spend your time to help me.


Log in to reply