Hi guys,

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.

Qt Code:
  1. #include "mainwindow.h"
  2. #include "ui_mainwindow.h"
  3. #include <QColor>
  4. #include <iostream>
  5. #include <QByteArray>
  6. #include <QDebug>
  7. #include <vector>
  8. #include <stdio.h>
  9. #include <algorithm>
  10. #include <string>
  11.  
  12. MainWindow::MainWindow(QWidget *parent) :
  13. QMainWindow(parent),
  14. ui(new Ui::MainWindow)
  15. {
  16. ui->setupUi(this);
  17.  
  18. }
  19.  
  20. MainWindow::~MainWindow()
  21. {
  22. delete ui;
  23. }
  24.  
  25.  
  26.  
  27. void MainWindow::on_cleanPlate_clicked()
  28. {
  29.  
  30. // load multiple images - must be same dimensions
  31. QStringList filename = QFileDialog::getOpenFileNames(this, tr("Choose Image"), "", tr("Images (*.png *.jpg *.jpeg *.bmp)"));
  32.  
  33. uint numberOfImages = static_cast<uint>(filename.count());
  34.  
  35. QString sizeQuery = filename.at(0);
  36. QImage getImageSize(sizeQuery);
  37. getImageSize = getImageSize.convertToFormat(QImage::Format_RGBA8888);
  38. uint numberOfBytes = static_cast<uint>(getImageSize.sizeInBytes());
  39.  
  40. std::vector<std::vector<std::vector<uint>>> rgb(numberOfImages, std::vector<std::vector<uint>>(numberOfBytes / 4, std::vector<uint>(4,0)));
  41. std::vector<std::vector<std::vector<uint>>> rgbTranspose(numberOfBytes/4, std::vector<std::vector<uint>>(numberOfImages, std::vector<uint>(4,0)));
  42.  
  43. // start of nested loop
  44. for (uint i{0};i < numberOfImages;++i) {
  45.  
  46. QString pictures = filename.at(i);
  47. QImage image(pictures);
  48. image.load(pictures);
  49. image = image.convertToFormat(QImage::Format_RGBA8888);
  50.  
  51. auto const pictureRGBdata = image.bits();
  52. for (uint j{0};j < numberOfBytes/4; ++j) {
  53.  
  54. std::cout << "Image " << i << ": Pixel: " << j;
  55.  
  56. for (uint k{0};k < 4;++k) {
  57.  
  58. rgb[i][j][k] = pictureRGBdata[4*j +k];
  59. std::cout << " " << rgb[i][j][k];
  60.  
  61. }
  62. std::cout << std::endl;
  63. }
  64. std::cout << std::endl;
  65. }
  66. //end of nested loop - but loop gets stuck here - does not exit loop to next line of code
  67. std::cout << "This won't print ";
  68.  
  69.  
  70. }
To copy to clipboard, switch view to plain text mode