-3

I have some C++-Code which for some reason keeps hanging. This is the code:

using namespace std;
using namespace cv; 


int main(int argc, char** argv) {

cout <<"started";
Mat im(256, 256, CV_8UC1, cv::Scalar(255));
for (int j = 0; j < 255; j++) {
    for (int k = 0; k < 255; k++) {

        if (k > j) {
            cv::Mat black(im, cv::Rect(j, k, 1, 1));
            black = cv::Scalar(0, 0, 0);
        }

    }
}

Mat image2;
//cvtColor(image, image2, CV_BGR2GRAY);
cout<<"started";
imshow("", im);
waitKey(0);

return 0;
}

Now, I have been trying this for 3 days now, literally erasing every single line of that code and putting it back in, it all comes down to:

must bei the imshow() which causes the program to hang so that the image does not show.

The strange thing is, I copied verbatim out of an older program of mine, it worked, even worked in the new project for a day and then SUDDENLY started hanging, meaning the program just keeps running and running without any result and it cannot be shut down.

Obviously I must be overlooking sth really basic here, but I simply cannot find it.

Help, please?

My OS is Ubuntu 15.10. Also, I just tried out SSteves answer => same problem. So it must be some kind of memory leak and not my code, right? How on earth do you fix memory leaks?

user1862770
  • 307
  • 1
  • 4
  • 13
  • 2
    I don't see an imshow command in your code? – Emily L. Feb 27 '16 at 19:50
  • 1
    Was thinking the same thing. – Evan Carslake Feb 27 '16 at 19:53
  • Possible duplicate of [cv::imshow sometimes is very slow](http://stackoverflow.com/questions/19837796/cvimshow-sometimes-is-very-slow) – Evan Carslake Feb 27 '16 at 19:54
  • There are **several** problems with your code. What exactly were you trying to do? Can you explain the algorithm? – karlphillip Feb 27 '16 at 19:59
  • The algorithim pretty much only gives me a quadratic mat image, half of which is white the other half is black, with the two halves being separated diagonally. This image would be "im". I have tested that, this part works. Even showing the image workend fine untill two days ago. – user1862770 Feb 27 '16 at 20:17
  • Also, it would be nice if someone who actually downvotes a question would at least have the decency to mention why he did it, otherwise I cannot even fix what was so annoying to the person. – user1862770 Feb 27 '16 at 20:18
  • To explain the algorithm further: I loop through a Mat (initially completely white) with of size 256x256 pixel by pixel and draw a rectangle of size one pixel if and only if the y-coordinate is bigger than the x-coordinate – user1862770 Feb 27 '16 at 20:22
  • 1
    Have you tried to put a non-empty string as window name in imshow? – Miki Feb 27 '16 at 20:33
  • Yes. same result. And it has now been hanging for an hour, so I suppose it is not a duplicate as mentioned above. – user1862770 Feb 27 '16 at 20:36

1 Answers1

1

Try this. It works for me and does what you describe.

#include <iostream>

#include "opencv2/core.hpp"
#include "opencv2/highgui.hpp"

using namespace std;
using namespace cv;

int main(int argc, char *argv[]) {
    Mat image(256, 256, CV_8UC1, Scalar(255));
    for (int j = 0; j < 256; j++) { //index goes to < 256, not < 255
        for (int k = j + 1; k < 256; k++) { //start loop at j + 1 to eliminate if (k > j) test
            Mat black(image, Rect(j, k, 1, 1));
            black = Scalar(0); //only one value in Scalar since this is a single-channel image
        }       
    }
    cv::imshow("", image);
    // wait for key
    cv::waitKey(0);

    return 0;
}

black/white diagonal

I made a couple small changes:

  • The loop index test should be < 256, not < 255 unless you specifically don't want to modify the last row or column.
  • You can start the inner loop with int k = j + 1 to avoid unnecessary loop iterations and the if (k > j) test.
  • You're setting black to a three-channel Scalar value. Mine still works when I changed it to Scalar(0, 0, 0) but you still shouldn't do that.

I tried your code and it also works. You don't mention what OS you're running. I'm running OS X.

SSteve
  • 10,550
  • 5
  • 46
  • 72
  • I copypasted your code verbatim, => still same problem. My OS is Ubuntu 15.10 – user1862770 Feb 28 '16 at 07:50
  • Is it possible that the window is displayed but not visible? I have had situations where the OpenCV window appears behind another window. Or maybe you had a second monitor connected at some point and it being displayed there. – SSteve Feb 28 '16 at 16:54
  • no. no second monitor and no, window is not invisible either. – user1862770 Feb 28 '16 at 17:37
  • Not sure what else to suggest. The code is right so it must be something with your system configuration. – SSteve Feb 29 '16 at 01:40