Advertisement
zumixosan

Untitled

Jan 16th, 2019
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 16.15 KB | None | 0 0
  1. // CobaOpencv.cpp : Defines the entry point for the console application.
  2. //
  3. #include "stdafx.h"
  4. #include <iostream>
  5. #include <fstream>
  6. #include <windows.h>
  7. #include <NuiApi.h>
  8. #include <NuiImageCamera.h>
  9. //#include"NuiImageBuffer.h"
  10. #include <opencv\cv.h>
  11. #include <opencv\cxcore.h>
  12. #include <opencv\highgui.h>
  13. #include <opencv2\imgproc\imgproc.hpp>
  14. #include <opencv2\imgcodecs\imgcodecs.hpp>
  15. #include <opencv2\ml.hpp>
  16. #include <opencv2\objdetect.hpp>
  17. #include <opencv2\opencv.hpp>
  18. #include <time.h>
  19.  
  20. using namespace std;
  21. using namespace cv;
  22.  
  23. interface INuiInstance;
  24.  
  25. #define COLOR_WIDTH 640
  26. #define COLOR_HIGHT 480
  27. #define DEPTH_WIDTH 640
  28. #define DEPTH_HIGHT 480
  29. #define SKELETON_WIDTH 640
  30. #define SKELETON_HIGHT 480
  31. #define CHANNEL 3
  32.  
  33. BYTE buf[DEPTH_WIDTH*DEPTH_HIGHT*CHANNEL];
  34.  
  35. int drawColor(HANDLE h, IplImage* color)
  36. {
  37.     const NUI_IMAGE_FRAME * pImageFrame = NULL;
  38.     HRESULT hr = NuiImageStreamGetNextFrame(h, 0, &pImageFrame);
  39.  
  40.     if (FAILED(hr))
  41.     {
  42.         cout << "Get Image Frame Failed" << endl;
  43.         return -1;
  44.     }
  45.  
  46.     INuiFrameTexture * pTexture = pImageFrame->pFrameTexture;
  47.     NUI_LOCKED_RECT LockedRect;
  48.     pTexture->LockRect(0, &LockedRect, NULL, 0);
  49.  
  50.     if (LockedRect.Pitch != 0)
  51.     {
  52.         BYTE * pBuffer = (BYTE*)LockedRect.pBits;
  53.         cvSetData(color, pBuffer, LockedRect.Pitch);
  54.     }
  55.  
  56.     //cvShowImage("color image", color);
  57.     NuiImageStreamReleaseFrame(h, pImageFrame);
  58.     return 0;
  59. }
  60.  
  61. int drawDepth(HANDLE h, IplImage* depth)
  62. {
  63.     const NUI_IMAGE_FRAME * pImageFrame = NULL;
  64.     HRESULT hr = NuiImageStreamGetNextFrame(h, 0, &pImageFrame);
  65.     if (FAILED(hr))
  66.     {
  67.         cout << "Get Image Frame Failed" << endl;
  68.         return -1;
  69.     }
  70.  
  71.     //  temp1 = depth;
  72.     INuiFrameTexture * pTexture = pImageFrame->pFrameTexture;
  73.     NUI_LOCKED_RECT LockedRect;
  74.     pTexture->LockRect(0, &LockedRect, NULL, 0);
  75.     if (LockedRect.Pitch != 0)
  76.     {
  77.         USHORT * pBuff = (USHORT*)LockedRect.pBits;
  78.         for (int i = 0; i<DEPTH_WIDTH*DEPTH_HIGHT; i++)
  79.         {
  80.             BYTE index = pBuff[i] & 0x07;
  81.             USHORT realDepth = (pBuff[i] & 0xFFF8) >> 3;
  82.             BYTE scale = 255 - (BYTE)(256 * realDepth / 0x0fff); //realDepth = jarak, dibawa ke 12 bit
  83.             buf[CHANNEL*i] = buf[CHANNEL*i + 1] = buf[CHANNEL*i + 2] = 0;
  84.            
  85.             float jarak;
  86.  
  87.             switch (index)
  88.             {
  89.             case 0:
  90.                 buf[CHANNEL*i] = scale / 2;
  91.                 buf[CHANNEL*i + 1] = scale / 2;
  92.                 buf[CHANNEL*i + 2] = scale / 2;
  93.                 break;
  94.  
  95.             case 1:
  96.                 buf[CHANNEL*i] = scale;
  97.                 break;
  98.  
  99.             case 2:
  100.                 buf[CHANNEL*i + 1] = scale;
  101.                 break;
  102.  
  103.             case 3:
  104.                 buf[CHANNEL*i + 2] = scale;
  105.                 break;
  106.  
  107.             case 4:
  108.                 buf[CHANNEL*i] = scale;
  109.                 buf[CHANNEL*i + 1] = scale;
  110.                 break;
  111.  
  112.             case 5:
  113.                 buf[CHANNEL*i] = scale;
  114.                 buf[CHANNEL*i + 2] = scale;
  115.                 break;
  116.  
  117.             case 6:
  118.                 buf[CHANNEL*i + 1] = scale;
  119.                 buf[CHANNEL*i + 2] = scale;
  120.                 break;
  121.  
  122.             case 7:
  123.                 buf[CHANNEL*i] = 255 - scale / 2;
  124.                 buf[CHANNEL*i + 1] = 255 - scale / 2;
  125.                 buf[CHANNEL*i + 2] = 255 - scale / 2;
  126.                 break;
  127.             }
  128.             jarak = (3.2*scale/256)+0.8;
  129.             //printf("Jarak : %f\n",jarak);
  130.         }
  131.         cvSetData(depth, buf, DEPTH_WIDTH*CHANNEL);
  132.     }
  133.  
  134.     NuiImageStreamReleaseFrame(h, pImageFrame);
  135.     //cvShowImage("depth image", depth);
  136.     return 0;
  137.  
  138. }
  139.  
  140. /*void histogram(string const& name, Mat img)
  141. {
  142.     cvtColor(img, img, cv::COLOR_BGR2HSV);
  143.     int hbins = 30, sbins = 32;
  144.     int histSize[] = { hbins };
  145.  
  146.     float hranges[] = { 0, 180 };
  147.     float sranges[] = {0, 255};
  148.     const float* ranges[] = { hranges, sranges }; //, vranges
  149.     MatND hist;
  150.  
  151.     int channels[] = {0,1};
  152.  
  153.     calcHist(&img, 1, channels, Mat(), hist, 2, histSize, ranges, true, false);
  154.     double maxVal = 0;
  155.     minMaxLoc(hist, 0, &maxVal, 0, 0);
  156.  
  157.     int scale = 10;
  158.     Mat histImg = Mat::zeros(sbins*scale, hbins * 10, CV_8UC3);
  159.  
  160.     for (int h = 0; h < hbins; h++)
  161.         for (int s = 0; s < sbins; s++)
  162.         {
  163.         float binVal = hist.at<float>(h, s);
  164.         int intensity = cvRound(binVal * 255 / maxVal);
  165.         rectangle(histImg, Point(h*scale, s*scale),
  166.             Point((h + 1)*scale - 1, (s + 1)*scale - 1),
  167.             Scalar::all(intensity),
  168.             CV_FILLED);
  169.         }
  170.     cv::imshow(name, histImg);
  171. }*/
  172.  
  173. void histogram(string const& name, Mat img)
  174. {
  175.     cvtColor(img, img, cv::COLOR_BGR2HSV);
  176.     // Set histogram bins count
  177.     int hbins = 30; // sbins = 32; //vbins = 30
  178.     int histSize[] = {hbins}; //sbins, vbins
  179.    
  180.     // Set ranges for histogram bins
  181.     float hranges[] = {0, 180};
  182.     //float sranges[] = {0, 255};
  183.     //float vranges[] = {0, 255};
  184.     const float* ranges[] = {hranges}; //sranges, vranges
  185.    
  186.     // create matrix for histogram
  187.     MatND h_hist;
  188.     int channels[] = { 0 };
  189.  
  190.     // create matrix for histogram visualization
  191.     //int scale = 10;
  192.     //Mat histImg = Mat::zeros(sbins*scale, hbins*10, CV_8UC3);
  193.    
  194.     calcHist(&img, 1, channels, Mat(), h_hist, 1, histSize, ranges, true, false);
  195.     //calcHist(&img, 1, channels, Mat(), s_hist, 2, histSize, ranges, true, false);
  196.  
  197.     // Draw the histograms for H, S and V
  198.     int hist_w = 512; int hist_h = 400;
  199.     int bin_w = cvRound((double)hist_w / hbins);
  200.  
  201.     Mat histImage(hist_h, hist_w, CV_8UC3, Scalar(0,0,0));
  202.    
  203.     //Normalize
  204.     normalize(img, img, 0, 1, NORM_MINMAX, -1, Mat() );
  205.     //normalize(img, img, 0, 1, NORM_MINMAX, -1, Mat());
  206.    
  207.     //Draw for each channel
  208.     for (int i = 1; i < hbins; i++){
  209.         line(histImage, Point(bin_w*(i - 1), hist_h - cvRound(h_hist.at<float>(i - 1))),
  210.             Point(bin_w*(i), hist_h - cvRound(h_hist.at<float>(i))),
  211.             Scalar(255, 0, 0), 2, 8, 0);
  212.     }
  213.     /*double maxVal = 0;
  214.     minMaxLoc(h_hist, 0, &maxVal, 0, 0);
  215.  
  216.     // visualize each bin
  217.     for (int h = 0; h < hbins; h++)
  218.         for (int s = 0; s < sbins; s++)
  219.             {
  220.                 float binVal = hist.at<float>(h, s);
  221.                 int intensity = cvRound(binVal * 255 / maxVal);
  222.                 cv::line(histImg, cv::Point(h*scale, s*scal),
  223.                     cv::Point((h + 1)*scale - 1), (s + 1) * scale - 1),
  224.                     cv::Scalar::all(intensity),
  225.                     CV_FILLED);
  226.             }
  227.     */
  228.     cv::imshow(name, histImage);
  229. }
  230.  
  231. //PCAawal
  232. void read_csv(const string& filename, vector<Mat>& images, vector<int>& labels) {
  233.     std::ifstream file(filename.c_str(), ifstream::in);
  234.     if (!file)
  235.         throw std::exception();
  236.     std::string line, path, classlabel;
  237.     // For each line in the given file:
  238.     while (std::getline(file, line)) {
  239.         // Get the current line:
  240.         std::stringstream liness(line);
  241.         // Split it at the semicolon:
  242.         std::getline(liness, path, ';');
  243.         std::getline(liness, classlabel);
  244.         // And push back the data into the result vectors:
  245.         images.push_back(imread(path, IMREAD_GRAYSCALE));
  246.         labels.push_back(atoi(classlabel.c_str()));
  247.     }
  248. }
  249. // Normalizes a given image into a value range between 0 and 255.
  250. Mat norm_0_255(const Mat& src) {
  251.     // Create and return normalized image:
  252.     Mat dst;
  253.     switch (src.channels()) {
  254.     case 1:
  255.         cv::normalize(src, dst, 0, 255, NORM_MINMAX, CV_8UC1);
  256.         break;
  257.     case 3:
  258.         cv::normalize(src, dst, 0, 255, NORM_MINMAX, CV_8UC3);
  259.         break;
  260.     default:
  261.         src.copyTo(dst);
  262.         break;
  263.     }
  264.     return dst;
  265. }
  266.  
  267. // Converts the images given in src into a row matrix.
  268. Mat asRowMatrix(const vector<Mat>& src, int rtype, double alpha = 1, double beta = 0) {
  269.     // Number of samples:
  270.     size_t n = src.size();
  271.     // Return empty matrix if no matrices given:
  272.     if (n == 0)
  273.         return Mat();
  274.     // dimensionality of (reshaped) samples
  275.     size_t d = src[0].total();
  276.     // Create resulting data matrix:
  277.     Mat data(n, d, rtype);
  278.     // Now copy data:
  279.     for (int i = 0; i < n; i++) {
  280.         //
  281.         if (src[i].empty()) {
  282.             string error_message = format("Image number %d was empty, please check your input data.", i);
  283.             CV_Error(CV_StsBadArg, error_message);
  284.         }
  285.         // Make sure data can be reshaped, throw a meaningful exception if not!
  286.         if (src[i].total() != d) {
  287.             string error_message = format("Wrong number of elements in matrix #%d! Expected %d was %d.", i, d, src[i].total());
  288.             CV_Error(CV_StsBadArg, error_message);
  289.         }
  290.         // Get a hold of the current row:
  291.         Mat xi = data.row(i);
  292.         // Make reshape happy by cloning for non-continuous matrices:
  293.         if (src[i].isContinuous()) {
  294.             src[i].reshape(1, 1).convertTo(xi, rtype, alpha, beta);
  295.         }
  296.         else {
  297.             src[i].clone().reshape(1, 1).convertTo(xi, rtype, alpha, beta);
  298.         }
  299.     }
  300.     return data;
  301. }
  302. //PCAakhir
  303.  
  304. int main(int argc, char * argv[])
  305. {
  306.     IplImage* color = cvCreateImageHeader(cvSize(COLOR_WIDTH, COLOR_HIGHT), IPL_DEPTH_8U, 4);
  307.     IplImage* depth = cvCreateImageHeader(cvSize(DEPTH_WIDTH, DEPTH_HIGHT), IPL_DEPTH_8U, CHANNEL);
  308.     IplImage* skeleton = cvCreateImage(cvSize(SKELETON_WIDTH, SKELETON_HIGHT), IPL_DEPTH_8U, CHANNEL);
  309.     HRESULT hr = NuiInitialize(NUI_INITIALIZE_FLAG_USES_DEPTH | NUI_INITIALIZE_FLAG_USES_COLOR | NUI_INITIALIZE_FLAG_USES_SKELETON);
  310.  
  311.     if (hr != S_OK)
  312.     {
  313.         cout << "NuiInitialize failed" << endl;
  314.         return hr;
  315.     }
  316.  
  317.     HANDLE h1 = CreateEvent(NULL, TRUE, FALSE, NULL);
  318.     HANDLE h2 = NULL;
  319.     hr = NuiImageStreamOpen(NUI_IMAGE_TYPE_COLOR, NUI_IMAGE_RESOLUTION_640x480, 0, 2, h1, &h2);
  320.     if (FAILED(hr))
  321.     {
  322.         cout << "Could not open image stream video" << endl;
  323.         return hr;
  324.     }
  325.  
  326.     HANDLE h3 = CreateEvent(NULL, TRUE, FALSE, NULL);
  327.     HANDLE h4 = NULL;
  328.     hr = NuiImageStreamOpen(NUI_IMAGE_TYPE_DEPTH, NUI_IMAGE_RESOLUTION_640x480, 0, 2, h3, &h4);
  329.     if (FAILED(hr))
  330.     {
  331.         cout << "Could not open depth stream video" << endl;
  332.         return hr;
  333.     }
  334.  
  335.     HANDLE h5 = CreateEvent(NULL, TRUE, FALSE, NULL);
  336.     hr = NuiSkeletonTrackingEnable(h5, 0);
  337.     if (FAILED(hr))
  338.     {
  339.         cout << "Could not open skeleton stream video" << endl;
  340.         return hr;
  341.     }
  342.    
  343.     Mat tmplt, tmplt2, CropFace;
  344.     Rect rekt(0, 0, 0, 0), rekt2(0, 0, 0, 0), rektg(0, 0, 0, 0);
  345.     int flagtmplt = 0, flagtmplt2 = 0, flagtmpltc, flgfacetmplt = 0;
  346.    
  347.     int i = 0;
  348.     char nama[10];
  349.    
  350.     //humandetection
  351.     HOGDescriptor hog;
  352.     String nn = "HOGHalfUpperBody72x96.xml";
  353.     bool l = hog.load(nn);
  354.     cout << l << endl;
  355.    
  356.     //face detection
  357.     CascadeClassifier face_haar;
  358.     String fc = ("haarcascade_frontalface_alt2.xml");
  359.     bool f = face_haar.load(fc);
  360.     cout << f << endl;
  361.    
  362.     int cntr=0;
  363.     Mat his, his2, His_c, dst;
  364.     vector<Mat> hsv_t2, hsv_t1, hsv_tc;
  365.  
  366.     while (1)
  367.     {
  368.         WaitForSingleObject(h1, INFINITE);
  369.         drawColor(h2, color);
  370.         WaitForSingleObject(h3, INFINITE);
  371.         drawDepth(h4, depth);
  372.        
  373.         Mat cr = cvarrToMat(color);
  374.         Mat dp = cvarrToMat(depth);
  375.         Mat ImgNew, ImgDepth, CropImg;
  376.         //resize(cr, ImgNew, Size(320, 240));
  377.         //resize(m, ImgDepth, Size(320, 240));
  378.         Mat gray, tgt;
  379.         cvtColor(cr,gray, CV_BGR2GRAY);
  380.         int method = 1;
  381.         vector<Mat> db;
  382.  
  383.         //simpan gambar per frs
  384.         //sprintf(nama,"%d.jpg",i++);
  385.         //cv::imwrite(nama,m);
  386.        
  387.         //human detection
  388.         vector< Rect > detections; //template deteksi
  389.         vector< double > foundWeights;
  390.         hog.detectMultiScale(dp, detections, foundWeights);
  391.        
  392.         //face detection
  393.         vector<Rect> faces;
  394.         face_haar.detectMultiScale(cr, faces, 1.1, 3, 0 | CV_HAAR_SCALE_IMAGE, Size(30, 30));
  395.  
  396.         cntr += 1;
  397.         //humandetection
  398.         for (size_t j = 0; j < detections.size(); j++)
  399.         {
  400.             if (foundWeights[j] > 0.6) {
  401.                 Scalar color = Scalar(0, foundWeights[j] * foundWeights[j] * 200, 0);
  402.                 rectangle(cr, detections[j], color, cr.cols / 400 + 1);
  403.  
  404.                 int hgth = detections[j].height;
  405.                 int weght = detections[j].width;
  406.                 int hbg3 = hgth / 3;
  407.                 int wbg3 = weght / 3;
  408.                 Rect rec = Rect(wbg3, hbg3 * 2, wbg3, hbg3);
  409.                 rectangle(cr, rec, Scalar(255, 0, 255), 1);
  410.                 His_c = cr(rec);
  411.                 //cvtColor(His_c, His_c, CV_BGR2HSV);
  412.                 //resize(His_c, His_c, his.size());
  413.                 //split(His_c, hsv_tc);
  414.                 His_c.convertTo(His_c, CV_32F);
  415.                 histogram("Current Hist", His_c);
  416.                 flagtmpltc = 1;
  417.                
  418.                 if (flagtmplt == 1 && cntr > 15){
  419.                     his2 = His_c.clone();
  420.                     //cvtColor(his2, his2, CV_BGR2HSV);
  421.                     //resize(his2, his2, his.size());
  422.                     //split(his2, hsv_t2);
  423.                     resize(his2, his2, His_c.size());
  424.                     his2.convertTo(his2, CV_32F);
  425.                     histogram("Template 2", his2);
  426.                     //cntr = 0;
  427.                     flagtmplt2 = 1;
  428.                 }
  429.  
  430.                 if (tmplt.empty() && flagtmplt==0){
  431.                     Mat a = cr(detections[j]);
  432.                     imwrite("halo.jpg", a);
  433.                     tmplt = a.clone();
  434.                     a.release();
  435.                     Size s = tmplt.size();
  436.                     int hgt = s.height;
  437.                     int wght = s.width;
  438.                     int hb3 = hgt / 3;
  439.                     int wb3 = wght / 3;
  440.                     rekt = Rect(wb3, hb3*2, wb3, hb3); //rekt
  441.                     his = tmplt(rekt);
  442.                     resize(his, his, His_c.size());
  443.                     his.convertTo(his, CV_32F);
  444.                     histogram("Template 1", his);
  445.                     //cvtColor(his, his, CV_BGR2HSV);
  446.                     //split(his, hsv_t1);
  447.                     //imwrite("his_template", his);
  448.                     flagtmplt = 1;
  449.                 }
  450.                 else{
  451.                     rectangle(tmplt, rekt, Scalar(255, 0, 255), 1);
  452.                     imshow("Template", tmplt);
  453.                 }
  454.  
  455.  
  456.             }
  457.         }
  458.        
  459.         if (flagtmplt == 1 && flagtmplt2 == 1 && cntr > 5){
  460.             for (int i = 0; i < 2; i++)
  461.             {
  462.                 int compare_method = i;
  463.                 double er1 = compareHist(His_c, his, compare_method);
  464.                 double er2 = compareHist(His_c, his2, compare_method);
  465.  
  466.                 printf("error1 : %f, error2: %f", er1, er2 );
  467.             }
  468.         }
  469.  
  470.         /*if (flagtmplt == 1 && flagtmplt2 == 1 && cntr > 5){
  471.            
  472.             histogram("Hue t1", hsv_t1[0]);
  473.             int Ht1 = countNonZero(hsv_t1[0]);
  474.             int St1 = countNonZero(hsv_t1[1]);
  475.             int Vt1 = countNonZero(hsv_t1[2]);
  476.             int Ht2 = countNonZero(hsv_t2[0]); // H channel
  477.             int St2 = countNonZero(hsv_t2[1]); // S channel
  478.             int Vt2 = countNonZero(hsv_t2[2]); // V channel
  479.  
  480.             int Htc = countNonZero(hsv_tc[0]);
  481.             int Stc = countNonZero(hsv_tc[1]);
  482.             int Vtc = countNonZero(hsv_tc[2]);
  483.              
  484.             int EH1 = Ht2 - Htc;
  485.             int ES1 = St2 - Stc;
  486.             int EV1 = Vt2 - Vtc;
  487.  
  488.             int EH2 = Ht1 - Ht2;
  489.             int ES2 = St1 - St2;
  490.             int EV2 = Vt1 - Vt2;
  491.  
  492.             int EH_Now = EH2 - EH1;
  493.             int ES_Now = ES2 - ES1;
  494.             int EV_Now = EV2 - EV1;
  495.  
  496.             //if ((EH >= Ht1 || ES >= St1 || EV >= Vt1) || (EH >= Ht2 || ES >= St2 || EV >= Vt2))
  497.             //if ((EH <= Ht1 || ES <= St1 || EV <= Vt1) && (EH <= Ht2 || ES <= St2 || EV <= Vt2))
  498.            
  499.             //printf("Warna: tidak %d\n", wr);
  500.             if ((EH_Now <= Ht1 && ES_Now <= St1 && EV_Now <= Vt1) && (EH_Now <= Ht2 && ES_Now <= St2 && EV_Now <= Vt2))
  501.             {
  502.                     int wr = 1;
  503.                 printf("Warna EH : %d\n", EH_Now);
  504.                 printf("Warna: sama %d\n", wr);
  505.             }
  506.             else
  507.             {
  508.                 printf("Warna EH_TidakSama : %d\n", EH_Now);
  509.                 int wr = 0;
  510.                 printf("Warna EH : %d\n",EH);
  511.                 printf("Warna Ht1 : %d\n",Ht1);
  512.                 printf("Warna Ht2 : %d\n",Ht2);
  513.                 printf("Warna ES : %d\n",ES);
  514.                 printf("Warna St2 : %d\n", St2);
  515.                 printf("Warna EV : %d\n", EV);
  516.                 printf("Warna Vt2 : %d\n", Vt2);
  517.                 printf("Warna: tidak %d\n", wr);
  518.                 printf("orang lain");
  519.             }
  520.         }*/
  521.  
  522.         //facedetection
  523.         for (size_t i = 0; i < faces.size(); i++)
  524.         {
  525.             Rect r = faces[i];
  526.             Mat faceROI = gray(faces[i]);
  527.             int x = faces[i].x;
  528.             int y = faces[i].y;
  529.             int h = 0.1*y + faces[i].height;
  530.             int w = faces[i].width;
  531.             rectangle(cr, Point(x, y), Point(x + w, y + h), Scalar(255, 0, 255), 1, 4, 0);
  532.             CropFace = cr(r);
  533.             /*
  534.             if (flgfacetmplt == 0){
  535.                 imwrite("template.jpg", CropFace);
  536.                 imshow("tmplt.jgp", CropFace);
  537.                 flgfacetmplt = 1;
  538.             }
  539.             if (flgfacetmplt == 1 && cntr > 2) {
  540.                 imwrite("current.jpg", CropFace);
  541.                 imshow("current.jpg", CropFace);
  542.             }
  543.             string prefix = "orl_faces//";
  544.             Mat tmplt = imread("template.jpg", IMREAD_GRAYSCALE);
  545.             Mat current = imread("current.jpg", IMREAD_GRAYSCALE);
  546.             resize(current, current, tmplt.size());
  547.  
  548.             db.push_back(tmplt);
  549.             db.push_back(current);
  550.            
  551.             // Build a matrix with the observations in row:
  552.             Mat data = asRowMatrix(db, CV_32FC1);
  553.  
  554.             // Number of components to keep for the PCA:
  555.             int num_components = 10;
  556.  
  557.             // Perform a PCA:
  558.             PCA pca(data, Mat(), CV_PCA_DATA_AS_ROW, num_components);
  559.  
  560.             // And copy the PCA results:
  561.             Mat mean = pca.mean.clone();
  562.             Mat eigenvalues = pca.eigenvalues.clone();
  563.             Mat eigenvectors = pca.eigenvectors.clone();
  564.  
  565.             // The mean face:
  566.             imshow("avg", norm_0_255(mean.reshape(1, db[0].rows)));
  567.  
  568.             // The first three eigenfaces:
  569.             imshow("pc1", norm_0_255(pca.eigenvectors.row(0)).reshape(1, db[0].rows));
  570.             //imshow("pc2", norm_0_255(pca.eigenvectors.row(1)).reshape(1, db[0].rows));
  571.             // imshow("pc3", norm_0_255(pca.eigenvectors.row(2)).reshape(1, db[0].rows));
  572.             */
  573.         }
  574.        
  575.         //print hasil
  576.         imshow("Test", cr);
  577.         //imshow("TestDepth", dp);
  578.         //WaitForSingleObject(h5,INFINITE);
  579.         //drawSkeleton(skeleton);
  580.         //exit
  581.         int c = cvWaitKey(1);
  582.         if (c == 27 || c == 'q' || c == 'Q')
  583.             break;
  584.     }
  585.    
  586.     cvReleaseImageHeader(&depth);
  587.     cvReleaseImageHeader(&color);
  588.     cvReleaseImage(&skeleton);
  589.     NuiShutdown();
  590.  
  591.     return 0;
  592. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement