2

I am new to Qt and C++. I have written some code which loads multiple images (of same dimensions). The red, green, blue and alpha data gets copied into a 3-dimensional vector. That all works.

The problem is that it stays stuck in the nested for loop and does not execute the code after the loop until I stop or close. Then it prints to the console as a test. I want to be able to do processing on the data in the vector after the loop executes.

I am very stuck on this.

This is an example of the output using very small test images per image, per pixel, red green blue alpha values.

Image 0: Pixel: 0 181 230 29 255 Image 0: Pixel: 1 255 255 255 255

Image 1: Pixel: 0 233 5 7 255 Image 1: Pixel: 1 0 0 0 255

Here is my code.

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QColor>
#include <iostream>
#include <QByteArray>
#include <QDebug>
#include <vector>
#include <stdio.h>
#include <algorithm>
#include <string>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

void MainWindow::on_cleanPlate_clicked()
{
    // load multiple images - must be same dimensions
    QStringList filename = QFileDialog::getOpenFileNames(this, tr("Choose Image"), "", tr("Images (*.png *.jpg *.jpeg *.bmp)"));

    uint numberOfImages = static_cast<uint>(filename.count());
    QString sizeQuery = filename.at(0);
    QImage getImageSize(sizeQuery);
    getImageSize = getImageSize.convertToFormat(QImage::Format_RGBA8888);
    uint numberOfBytes = static_cast<uint>(getImageSize.sizeInBytes());

    std::vector<std::vector<std::vector<uint>>> rgb(numberOfImages, std::vector<std::vector<uint>>(numberOfBytes / 4, std::vector<uint>(4,0)));
    std::vector<std::vector<std::vector<uint>>> rgbTranspose(numberOfBytes/4, std::vector<std::vector<uint>>(numberOfImages, std::vector<uint>(4,0)));

    // start of nested loop
    for (uint i{0};i < numberOfImages;++i) {

        QString pictures = filename.at(i);
        QImage image(pictures);
        image.load(pictures);
        image = image.convertToFormat(QImage::Format_RGBA8888);

        auto const pictureRGBdata = image.bits();
        for (uint j{0};j < numberOfBytes/4; ++j) {

            std::cout  << "Image " << i << ": Pixel: " << j;

            for (uint k{0};k < 4;++k) {

                rgb[i][j][k] = pictureRGBdata[4*j +k];
                std::cout << " " << rgb[i][j][k];
            }
            std::cout << std::endl;
        }
        std::cout << std::endl;
    }
    //end of nested loop - but loop gets stuck here - does not exit loop to next line of code
    std::cout << "This won't print ";
}

MainWindow::~MainWindow()
{
    delete ui;
}
yacc
  • 2,915
  • 4
  • 19
  • 33
Rod
  • 49
  • 5
  • 1
    Try to add #include and replace all std::cout<< with qDebug()< – Alexey Tsybin Jun 05 '19 at 10:52
  • Just print i,j,k in each iteration of inner loop and see which of them is overflowing. – nayab Jun 05 '19 at 11:38
  • Are you sure that your loops are not taking a really long time? – Dimitry Ernot Jun 05 '19 at 13:27
  • I even try on a single pixel image. It immediately prints out the rgb data for me. But it just stops doing anything then. After I hit the red Stop button in Qt it then executes the next line of code. I have no idea why it does not proceed. – Rod Jun 05 '19 at 13:30
  • Just try qdebug as i said , its working for me with your code – Alexey Tsybin Jun 05 '19 at 14:25
  • Ok. I'll give it a shot Alexey. – Rod Jun 05 '19 at 14:56
  • That's so weird. It works although the formatting is messed up. Mind you I don't need to print the data. I simply want to be able to apply some mathematical processing to it now outside the loop. Do you know any possible reasons why it works using qDebug() but not std::cout? Anyway, thank you for that Alexey Tsybin. – Rod Jun 05 '19 at 15:13
  • 1
    https://stackoverflow.com/questions/14858262/stdcout-wont-print – Alexey Tsybin Jun 05 '19 at 16:42
  • Brilliant Alexey! Thank you so much for your help on this. – Rod Jun 05 '19 at 17:13

0 Answers0