1

I am using openCV in my c++ image processing project.

I have this two dimensional array I[800][600] filled with values between 0 and 255, and i want to put this array in a graylevel "IplImage" so i can view it and process it using openCV functions.

Any help will be appreciated.

Thanks in advance.

Golden_phoenix
  • 91
  • 3
  • 10
  • possible duplicate of [OpenCV : convert the pointer in memory to image](http://stackoverflow.com/questions/10124717/opencv-convert-the-pointer-in-memory-to-image) – karlphillip Jun 19 '12 at 01:04
  • You can query [this question][1] where three answers are all correct. [1]: http://stackoverflow.com/questions/2468307/how-to-convert-a-mat-variable-type-in-an-iplimage-variable-type-in-opencv-2-0 – Yantao Xie Jun 19 '12 at 01:06
  • 1
    Pay attention to the answer, you should follow the instructions and call `cvCreateImageHeader(cvSize(800, 600), IPL_DEPTH_8U, 1);` followed by `cvSetData()` – karlphillip Jun 19 '12 at 01:06
  • @CookSchelling I believe a more clear/precise answer to this question the one marked as possible duplicate. – karlphillip Jun 19 '12 at 01:09
  • @karlphillip Yes, you are right. – Yantao Xie Jun 19 '12 at 01:12
  • @karlphillip so do i just put I "the name of the array" instead of rawdata in here cvSetData(cv_image, raw_data, cv_image->widthStep); thanks – Golden_phoenix Jun 19 '12 at 01:24
  • @Golden_phoenix answering that will lead you to come back here ans ask other questions about this, so I beg that you [check the docs](http://opencv.itseez.com/modules/core/doc/old_basic_structures.html?highlight=cvsetdata#setdata). – karlphillip Jun 19 '12 at 02:27

2 Answers2

3

It's easy in Opencv C++ interface, all you need to do is to init a matrice, see the line below

cv::Mat img = cv::Mat(800, 600, CV_8UC1, I) // I[800][600]

Now you can do whatever you want, Opencv treats img as an 8-bit grayscale image.

XYZ
  • 51
  • 3
0
CvSize image_size;
image_size.height = 800;
image_size.width = 600;
int channels = 1;
IplImage *image = cvCreateImageHeader(image_size, IPL_DEPTH_8U, channels);
cvSetData(image, I, image->widthStep)

this is untested, but the most important thing likely to require fixing is the second parameter to cvSetData(). This needs to be a pointer to unsigned character data, and if you're just using a 2D array that isn't part of a Mat, then you'll have to do something a bit different, (possibly a loop? although you should avoid loops in openCV as much as possible).

see this post for a highly relevant question

Community
  • 1
  • 1
eqzx
  • 5,323
  • 4
  • 37
  • 54