Ask Your Question
0

How to set manually mat properties?

asked 2014-02-23 02:07:07 -0600

tiho_bg gravatar image

updated 2014-02-23 03:39:08 -0600

berak gravatar image

Hi

I would like to ask is it possible in opencv to set manually all mat properties. I am using a specialised camera were it is not possible to recognise it with opencv funcions. I would like to vizualise the data image of the camera using opencv but combining it with the opening funcitons offered by the manufacturer. I saw that when the image is ready there is a variable structure that has the following parameters:

typedef struct tag_mexImg
{
  unsigned long Version;        //!< [in] Version, currently it must be 'V003' (MEXIMGVERSION)<br>
  unsigned long Bytes;          //!< [in] Amount of bytes of this struct (sizeof(mexImg)).<br>
  int           dimX;           //!< [in/out] Horizontal pixel dimension of image<br>
  int           dimY;           //!< [in/out] Vertical pixel dimension of image<br>
  void*         pBw;            //!< [in/out] Pointer to memory for monochrome image or for the \link page27 DIB-format \endlink<br>
  void*         pRed;           //!< [in/out] Pointer to memory for red image channel<br>
  void*         pGreen;         //!< [in/out] Pointer to memory for green image channel<br>
  void*         pBlue;          //!< [in/out] Pointer to memory for blue image channel<br>
  int           channels;       //!< [in] to take from enum mex_channels<br>
  unsigned int  bitmask;        //!< [out] Bitmask of significant bits in image pixel data<br>
  unsigned long stdTicks;       //!< [out] Realized exposure standard ticks<br>
  unsigned long addTicks;       //!< [out] Realized exposure additional ticks<br>
  unsigned long channelBytes;   //!< [in/out] Realized amount of bytes per color channel<br>
  int           posX;           //!< [out] X-position code of a single image from an unassembled scan (::mexAcquisParams::runas = runas_scan)<br>
  int           posY;           //!< [out] Y-position code of a single image from an unassembled scan (::mexAcquisParams::runas = runas_scan)<br>
  unsigned long bytesperPixel;  //!< [in/out] Bytes per pixel and color (should be 1 or 2)<br>
  unsigned long orientation;    //!< [out] Flip conditions with respect to ::mex_orientation<br>
  unsigned long exposuretime;   //!< [out] exposure time - [microseconds}<br>
  int           colorMask;      //!< [out] A value from enum  ::mexColorMask <br>
  unsigned __int64 timestamp;   //!< [out] timestamp<br>
  unsigned long user_index;     //!< [in/out] instead of reserved[user_index]<br>
  unsigned long HdNr;           //!< Identifier, coming from frame header<br>
  unsigned long Time[4];        //!< time stamps for logging purposes<br>
  unsigned long reserved[4];    //!< Reserved for future use<br>
} mexImg;

I saw that my variable Mat frame has different parameters as: Mat frame;
frame.addref();
frame.allocator;
frame.channels();
frame.cols;
frame.rows;
frame.data;
frame.datastart;
frame.dataend;
frame.datalimit;
frame.depth();
frame.dims;
frame.flags;
frame.refcount;
frame.reserve();
frame.size;
frame.step;


I think that if I want to visualize the image by imshow("image", frame); I have to set all the parameters of the Mat structure.

Could you help to resolve the problem?

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2014-02-23 04:04:08 -0600

berak gravatar image

updated 2014-02-23 10:24:15 -0600

"I have to set all the parameters of the Mat structure." - no, you can use a Mat constructor for this:

grayscale img:

mexImg * mex;
Mat gray( mex->dimY, mex->dimX, CV_8U, (uchar*)mex->pBw );
imshow("image", gray);
waitKey(0);

color img:

mexImg * mex;
Mat rgb[3] = {
    Mat( mex->dimY, mex->dimX, CV_8U, (uchar*)mex->pBlue ),
    Mat( mex->dimY, mex->dimX, CV_8U, (uchar*)mex->pGreen ),
    Mat( mex->dimY, mex->dimX, CV_8U, (uchar*)mex->pRed )
};
Mat img;
merge(rgb,3,img);
imshow("image", img);
waitKey(0);
edit flag offensive delete link more

Comments

Thank you for the help. I have tried the color img example. There is no errors. I have an image data now but imshow("image" img) give me a gray window. I have checked the code in debug mode and I saw that: img.allocator has an error: _vfptr CXX0030:Error: expression can not be evaluated

Is it normal that error or not? May be the gray window means that my image is converted from RGB to GRAY, isn't it? Could you pleas help me to resolve this problem?

tiho_bg gravatar imagetiho_bg ( 2014-02-23 09:45:20 -0600 )edit

sorry, my bad ..

forgot the obvious waitKey() call. imshow won't update the window without ... see edit please.

berak gravatar imageberak ( 2014-02-23 10:29:36 -0600 )edit
1

berak's answer seems the way to go, just one comment: take a look at the member "bytesperPixel" of the struct mexImg, the value might be 1 or 2, so I would pay attention to it when defining the matrix dimensions (CV_8U or CV_16U depending on the value of bytesperPixel)

Martin Peris gravatar imageMartin Peris ( 2014-02-23 19:37:18 -0600 )edit

Thank you for your answers. The bytesperPixel is equal to 0 in my case. I have used CV_16U because otherwise it is in stripes. But now imshow("image", img) give me a black window. There are no errors. I have added waitKey(0). Could you please suggest something?

tiho_bg gravatar imagetiho_bg ( 2014-02-24 04:45:31 -0600 )edit

I have tried setting CV_F32 but there is an error. I wanted to try making my image colored. Why it is not allowed here?

tiho_bg gravatar imagetiho_bg ( 2014-02-24 04:47:10 -0600 )edit

Question Tools

Stats

Asked: 2014-02-23 02:07:07 -0600

Seen: 608 times

Last updated: Feb 23 '14