1

I've got a weird problem. Here's my code:

#include <iostream>
#include "opencv2/opencv.hpp"
#include <stdint.h>

using namespace cv;

int main()
{
Mat img = imread("testbild.jpg", CV_LOAD_IMAGE_UNCHANGED);

Mat imgGray = imread("testbild.jpg", CV_LOAD_IMAGE_GRAYSCALE);

if (img.empty())
{
    std::cout << "Kein Bild gefunden!" << std::endl;
}

//This array isn't used for while but I will use it in the future
int intensity[imgGray.rows] [imgGray.cols];

int max;

for (int r = 0; r <= imgGray.rows; r++)
{
    for (int c = 0; c <= imgGray.cols; c++)
    {

        if (max < (int)imgGray.at<uint8_t> (r,c))
        {
            max = (int)imgGray.at<uint8_t> (r,c);
        }

    }
}

std::cout << max << std::endl;

return 0;
}

The thing is, cout is not working. I don't know why. But whenever I comment out the line

int intensity[imgGray.rows] [imgGray.cols];

It works again.

I know that this line isn't used yet, but I'll use it later.

Why does this happen? I'm new to programming so please don't be mad, if it's a really simple answer. I also already used Google to find a solution, but all I got is to flush the output, and that doesn't work.

The console opens by the way, but all it says is "Press to close this window...".

brayen
  • 41
  • 1
  • 6
  • 1
    The reason is you can't declare your array like this when the size is not constant. You need to assign it dynamically. – lamandy Nov 27 '17 at 08:39
  • http://www.cplusplus.com/reference/vector/vector/ a vector of vector should make the job –  Nov 27 '17 at 08:41
  • 4
    Read about `std::vector` in your favourite C++ book. – molbdnilo Nov 27 '17 at 08:41
  • 1
    That's not "std::cout doesn't work". That's "your program crashes". Somebody has told you that things like `int intensity[imgGray.rows] [imgGray.cols];` are OK. You should never listen to them again. – n. m. could be an AI Nov 27 '17 at 08:43
  • Ah I knew it was some stupid thing... thanks for the answers. But it's still "cout doesn't work", as my program is not crashing and I don't get any errors... – brayen Nov 27 '17 at 08:44
  • `cout` is working fine. You are *observing* the misbehaviour of the rest of your program when you try to `cout << max` – Caleth Nov 27 '17 at 09:09

1 Answers1

-2

The reason is you can't declare your array like this when the size is not constant. You need to assign it dynamically.

One way to do it is:

int** intensity; //double pointer used to make 2D array
intensity = new int*[imgGray.rows]; //The double pointer now points to an array of single pointer.
for (int i = 0; i < imgGray.rows; ++i) //for each pointer in the array, create an array of integer, effectively a 2D array.
    intensity[i] = new int[imgGray.cols];

After that, you can access individual value in the 2D array by intensity[i][j] but it is your job to make sure it never goes out of bound and you need to free the memory yourself after finish with it. (Unless you are closing the program immediately after that then it is fine)

lamandy
  • 962
  • 1
  • 5
  • 13