0

I think I've thoroughly searched the forums, unless I left out certain keywords in my search string, so forgive me if I've missed a post. I am currently using OpenCV 2.4.0 and I have what I think is just a simple problem:

I am trying to take in an unsigned character array (8 bit, 3 channel) that I get from another API and put that into an OpenCV matrix to then view it. However, all that displays is an image of the correct size but a completely uniform gray. This is the same color you see when you specify the incorrect Mat name to be displayed.

Have consulted: Convert a string of bytes to cv::mat (uses a string inside of array) and opencv create mat from camera data (what I thought was a BINGO!, but can't seem to get to display the image properly).

I took a step back and just tried making a sample array (to eliminate the other part that supplies this array):

int main() {


bool isCamera = true;

unsigned char image_data[] = {255,0,0,255,0,0,255,0,0,255,0,0,255,0,0,255,0,0,0,255,0,0,255,0,0,255,0,0,255,0,0,255,0,0,255,0,0,0,255,0,0,255,0,0,255,0,0,255,0,0,255,0,0,255};
cv::Mat image_as_mat(Size(6,3),CV_8UC3,image_data);
namedWindow("DisplayVector2",CV_WINDOW_AUTOSIZE);
imshow("DisplayVector2",image_as_mat);
cout << image_as_mat << endl;

getchar();

}

So I am just creating a 6x3 matrix, with the first row being red pixels, the second row being green pixels, and third row being blue. However this still results in the same blank gray image but of correct size.

The output of the matrix is (note the semicolons i.e. it formatted it correctly):

[255, 0, 0, 255, 0, 0, 255, 0, 0, 255, 0, 0, 255, 0, 0, 255, 0, 0; 0, 255, 0, 0, 255, 0, 0, 255, 0, 0, 255, 0, 0, 255, 0, 0, 255, 0; 0, 0, 255, 0, 0, 255, 0, 0, 255, 0, 0, 255, 0, 0, 255, 0, 0, 255]

I might be crazy or missing something obvious here. Do I need to initialize something in the Mat to allow it to display properly? Much appreciated as always for all your help everyone!

Community
  • 1
  • 1
user2891729
  • 137
  • 3
  • 10
  • I hint for debug this. After you make the image_as_at, try to split it two 3 CV_8U Mat (using cv::split) and print each of them. You will see if your initialization was correct or not. – jgmao Oct 17 '13 at 18:52

1 Answers1

3

all the voodoo here boils down to calling getchar() instead of (the required) waitKey()

let me explain, waitKey might be a misnomer here, but you actually need it, as the code inside wraps the window's messageloop, which triggers the actual blitting (besides waiting for keypresses).

if you don't call it, your window will never get updated, and will just stay grey ( that's what you observe here )

indeed, you should have trusted the result from cout , your Mat got properly constructed, it just did not show up in the namedWindow

(btw, getchar() waits for a keypress from the console window, not your img-window)

hope it helps, happy hacking further on ;)

berak
  • 39,159
  • 9
  • 91
  • 89