Kinect for Windows V2 and V1 development ___ Color Data acquisition and OpenCV2.4.10 display

Source: Internet
Author: User

V1 Color Resolution: 640x480

V2 Color Resolution: 1920x1080


1. How to turn on the color image frame

for V1: Use Nuiimagestreamopen Method Open

hr = M_pnuisensor->nuiimagestreamopen (                                   nui_image_type_color,nui_image_resolution_640x480,0, 2,                                   m_ Hnextcolorframeevent,&m_hcolorstreamhandle);                          if (FAILED (HR))                          {                                   cout<< "Could notopen image stream video" <<endl;                                   return hr;                    } This way you can set the resolution

For V2:

First Use  m_pkinectsensor->open ();//Open Kinect         if (SUCCEEDED (HR))        {            hr =m_pkinectsensor->get_ Colorframesource (&pcolorframesource);        } The Get_colorframesource method opens the source of the colored frame. Then use     if (SUCCEEDED (HR))        {            hr =pcolorframesource->openreader (&m_pcolorframereader);        }        Saferelease (Pcolorframesource); method Openreader Open the color frame reader.


2. How to update color frames

For V1: Using the Nuiimagestreamgetnextframe method

Nuiimagestreamgetnextframe (m_hcolorstreamhandle,0, &pimageframe);//Get the frame data

For V2: Using the Acquirelatestframe method

if (!m_pcolorframereader)    {        return;    }     icolorframe* pcolorframe = NULL; HRESULT hr =m_pcolorframereader->acquirelatestframe (&pcolorframe);


3. How the data is processed

For V1: This data acquisition method is more clear to see the internal structure of the data,

Inuiframetexture *ptexture =pimageframe->pframetexture;                          Nui_locked_rect Lockedrect; Ptexture->lockrect (0, &lockedrect,null, 0);//extract the data frame to lockedrect. It contains two data objects: Pitch per line of bytes.                                   Pbits the first byte of the address if (lockedrect.pitch! = 0) {                                   Cvzero (Colorimage); for (int i=0; i<480; i++) {uchar* ptr = (u                                            char*) (Colorimage->imagedata+i*colorimage->widthstep); BYTE * pbuffer = (byte*) (lockedrect.pbits) +i*lockedrect.pitch;//each byte represents a color information.                                            Directly using byte for (int j=0; j<640; j + +)                                                     {PTR[3*J] =pbuffer[4*j];//internal data is 4 bytes, 0-1-2 is BGR, 4th is not currently used Ptr[3*J+1] =pbuffer[4*j+1];                                            PTR[3*J+2] =pbuffer[4*j+2];                                   }}//cvwriteframe (Wr_color,colorimage); Cvshowimage ("Colorimage", colorimage);//Display image

finally the form can be used OpenCV display.

For V2: The internal structure of such data is God horse appearance? Then how to display the image data with OpenCV? Unknown origin ...

rgbquad* m_pcolorrgbx;//Color Data storage location M_PCOLORRGBX (NULL)//constructor initialization//create heap storage for color pixel data in Rgbxformat  M_PCOLORRGBX = new Rgbquad[ccolorwidth *ccolorheight];        Below is acquirelatestframe after processing data INT64 ntime = 0;        iframedescription* pframedescription =null;        int nwidth = 0;        int nheight = 0;        Colorimageformat imageformat = Colorimageformat_none;        UINT nbuffersize = 0;         Rgbquad *pbuffer = NULL; if (SUCCEEDED (HR)) {if (imageformat = = Colorimageformat_bgra) {hr =pcolorfra            Me->accessrawunderlyingbuffer (&nbuffersize, reinterpret_cast<byte**> (&pBuffer));                } else if (M_PCOLORRGBX) {pbuffer = M_PCOLORRGBX;                Nbuffersize = ccolorwidth *ccolorheight * sizeof (RGBQUAD); hr = Pcolorframe->copyconvertedframedatatoarray (nbuffersize,reinterpret_cast<byte*> (PBuffer),          Colorimageformat_bgra);             } else {hr = E_FAIL;        }} if (SUCCEEDED (HR)) {Processcolor (Ntime, Pbuffer,nwidth, nheight); }

It feels like the pbuffer is the stored color data. The question is how to use OPENCV to display it?


4,OPENCV Display

<span style= "White-space:pre" ></span>int width = 0;int height = 0;pdescription->get_width (&width); 1920pdescription->get_height (&height);  1080unsigned int buffersize = width * Height * 4 * sizeof (unsigned char);//Create 4-Channel 8-bit image with dimensions of height x width mat buffermat ( Height, width, cv_8uc4); Mat Colormat (HEIGHT/2, WIDTH/2, CV_8UC4); while (1) {//Update color frame icolorframe* pcolorframe = Nullptr;hresult = Pcolorreade R->acquirelatestframe (&pcolorframe); if (SUCCEEDED (HRESULT)) {HRESULT = Pcolorframe-> Copyconvertedframedatatoarray (buffersize, reinterpret_cast<byte*> (Buffermat.data), ColorImageFormat:: Colorimageformat_bgra), if (SUCCEEDED (HResult)) {Resize (Buffermat, Colormat, Cv::size (), 0.5, 0.5);}} Saferelease (Pcolorframe); Imshow ("Color", Colormat);

When used with Kinect's copyconvertedframedatatoarray function, the image frames are converted to matrix data for display.


5. V2+VS2012+OPENCV Code

#include <Windows.h> #include <Kinect.h> #include <opencv2/opencv.hpp>using namespace std;using Namespace cv;//release interface requires custom template<class interface>inline void Saferelease (Interface *& pinterfacetorelease) {if (pinterfacetorelease! = null) {pinterfacetorelease->release ();p interfacetorelease = NULL;}} int main (int argc, char **argv[]) {//OPENCV to turn on the CPU's Hardware instruction optimization function setuseoptimized (TRUE);//Open kinectikinectsensor* psensor; HRESULT HRESULT = S_ok;hresult = Getdefaultkinectsensor (&psensor); if (FAILED (HResult)) {cerr << "Error:get Defaultkinectsensor "<< std::endl;return-1;} HResult = Psensor->open (), if (FAILED (HResult)) {Cerr << "Error:ikinectsensor::open ()" << Std::endl;retu rn-1;} Color frame source icolorframesource* Pcolorsource;hresult = Psensor->get_colorframesource (&pcolorsource), if (FAILED ( HResult) {cerr << "Error:ikinectsensor::get_colorframesource ()" << std::endl;return-1;} Color frame Read icolorframereader* PcolorReader;hresult = Pcolorsource->openreader (&pcolorreader), if (FAILED (HResult)) {Cerr << Error:icolorfra Mesource::openreader () "<< std::endl;return-1;} descriptioniframedescription* Pdescription;hresult = pcolorsource->get_framedescription (&pDescription); if (FAILED (HResult)) {Cerr << "error:icolorframesource::get_framedescription ()" << std::endl;return-1;} int width = 0;int height = 0;pdescription->get_width (&width); 1920pdescription->get_height (&height);  1080unsigned int buffersize = width * Height * 4 * sizeof (unsigned char);//Create 4-Channel 8-bit image with dimensions of height x width mat buffermat ( Height, width, cv_8uc4); Mat Colormat (HEIGHT/2, WIDTH/2, CV_8UC4); while (1) {//Update color frame icolorframe* pcolorframe = Nullptr;hresult = Pcolorreade R->acquirelatestframe (&pcolorframe); if (SUCCEEDED (HRESULT)) {HRESULT = Pcolorframe-> Copyconvertedframedatatoarray (buffersize, reinterpret_cast<byte*> (buffermat.data), ColorImAgeformat::colorimageformat_bgra), if (SUCCEEDED (HResult)) {Resize (Buffermat, Colormat, Cv::size (), 0.5, 0.5);}} Saferelease (Pcolorframe); Imshow ("Color", Colormat); if (waitkey () = = Vk_escape) {break;}} Saferelease (Pcolorsource); Saferelease (Pcolorreader); Saferelease (pdescription); if (psensor) {psensor->close ();} Saferelease (psensor); return 0;}


The resolution is greatly improved AH ~ ~

Kinect for Windows V2 and V1 development ___ Color Data acquisition and OpenCV2.4.10 display

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.