Guest User

code_2

a guest
Feb 27th, 2013
391
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.97 KB | None | 0 0
  1. //Heasder inclusion
  2. #include <stdio.h>
  3. #include <cv.h>
  4. #include <highgui.h>
  5. #include <opencv2/imgproc/imgproc.hpp>
  6. #include <opencv2/video/tracking.hpp>
  7. #include <sstream>
  8. #include <string>
  9. #include <fstream>
  10. #include <pthread.h>
  11. #include <math.h>
  12. #include <iostream>
  13. #include <vector>
  14.  
  15. //Namespace Declaration
  16. using namespace cv;
  17. using namespace std;
  18.  
  19.  
  20. void measureEdgeFromMiddleWrist(Mat edgeImage){
  21.     Mat im_src =  edgeImage.clone();
  22.     Mat im_gray = im_src;
  23.     Mat img_bw = im_gray > 250;
  24.     Mat in  = img_bw;
  25.     imshow("IMAGE",img_bw);
  26.     int firstRow = 1;
  27.     bool oneEdgePixelFound = false;
  28.     int edgePixelCounter=0;
  29.     int edgeOnFirstLineRightLoc[1][2];
  30.     int edgeCount=0;
  31.     int total_img_width = img_bw.cols;
  32.     int middle = total_img_width/2;
  33.     cout << "total_img_width :" << total_img_width << endl;
  34.     cout << "half of it : " << middle << endl;
  35.     int cntr=0;
  36.  
  37.     // for(int row = 1; row <= 1; ++row) {
  38.     for(int row = firstRow; row <= firstRow; ++row) {
  39.         unsigned char* inp  = in.ptr<unsigned char>(row);
  40.         for (int col = middle; col < in.cols; ++col) {
  41.             if (*inp++ == 0) {
  42.                 // std::cout << '1';
  43.             } else {
  44.                 if(!oneEdgePixelFound){
  45.                     std::cout << "Row : " << row << " Col : " << col <<  " Value : 0 " << endl;
  46.                     int tmp = edgeCount++;
  47.                     edgeOnFirstLineRightLoc[tmp][0] =row ;
  48.                     edgeOnFirstLineRightLoc[tmp][1] =col ;
  49.                     edgePixelCounter++;
  50.                     if(edgePixelCounter==1){
  51.                         oneEdgePixelFound=true;
  52.                     }
  53.                 }
  54.             }
  55.             // std::cout << std::endl;
  56.         }
  57.     }
  58.  
  59.     int lastRow = img_bw.rows -1;
  60.  
  61.     oneEdgePixelFound = false;
  62.     edgePixelCounter=0;
  63.     int edgeOnLastLineRightLoc[1][2];
  64.     edgeCount=0;
  65.     for(int row = lastRow; row <= lastRow; ++row) {
  66.         unsigned char* inp  = in.ptr<unsigned char>(row);
  67.         for (int col = middle; col < in.cols; ++col) {
  68.             if (*inp++ == 0) {
  69.                 // std::cout << '1';
  70.             } else {
  71.                 if(!oneEdgePixelFound){
  72.                     std::cout << "Row : " << row << " Col : " << col <<  " Value : 0 " << endl;
  73.                     int tmp = edgeCount++;
  74.                     edgeOnLastLineRightLoc[tmp][0] =row ;
  75.                     edgeOnLastLineRightLoc[tmp][1] =col ;
  76.                     edgePixelCounter++;
  77.                     if(edgePixelCounter==1){
  78.                         oneEdgePixelFound=true;
  79.                     }
  80.                 }
  81.             }
  82.             // std::cout << std::endl;
  83.         }
  84.     }
  85.  
  86.  
  87.     // for (int i = 0; i < 2; ++i)
  88.     // {
  89.         for (int j = 0; j < 1; ++j)
  90.         {
  91.             cout <<  "Value at fisrt line Right Direction :" << edgeOnFirstLineRightLoc[j][1] << endl;
  92.         }
  93.        
  94.     // }
  95.  
  96.     // for (int i = 0; i < 2; ++i)
  97.     // {
  98.         for (int j = 0; j < 1; ++j)
  99.         {
  100.             cout <<  "Value at Last line Right Direction :" << edgeOnLastLineRightLoc[j][1] << endl;
  101.         }
  102.        
  103.     // }
  104.  
  105.  
  106.  
  107.  
  108.     bool oneEdgePixelsFound_1 = false;
  109.     int edgePixelCounter_1=0;
  110.     // int edgeOnLastLineLeftLoc_1[2][2];
  111.     int edgeOnFirstLineLeftLoc_1[1][1];
  112.     int edgeCount_1=0;
  113.  
  114.     // for(int row = 1; row <= 1; ++row) {
  115.     // for(int row = lastRow; row <= lastRow; ++row) {
  116.     for(int row = firstRow; row <= firstRow; ++row) {
  117.         unsigned char* inp  = in.ptr<unsigned char>(row);
  118.         for (int col = middle; col > 0; --col) {
  119.             if (*inp++ == 0) {
  120.                 // std::cout << '1';
  121.             } else {
  122.                 if(!oneEdgePixelsFound_1){
  123.                     std::cout << "Row : " << row << " Col : " << col <<  " Value : 0 " << endl;
  124.                     int tmp_1 = edgeCount_1++;
  125.                     edgeOnFirstLineLeftLoc_1[tmp_1][0]=row;
  126.                     edgeOnFirstLineLeftLoc_1[tmp_1][1]=col;
  127.                     edgePixelCounter_1++;
  128.                     if(edgePixelCounter_1==1){
  129.                         oneEdgePixelsFound_1=true;
  130.                     }
  131.                 }
  132.             }
  133.             // std::cout << std::endl;
  134.         }
  135.     }
  136.  
  137.     oneEdgePixelsFound_1 = false;
  138.     edgePixelCounter_1=0;
  139.     edgeCount_1=0;
  140.     int edgeOnLastLineLeftLoc_1[1][1];
  141.  
  142.     for(int row = lastRow; row <= lastRow; ++row) {
  143.         unsigned char* inp  = in.ptr<unsigned char>(row);
  144.         for (int col = middle; col > 0; --col) {
  145.             if (*inp++ == 0) {
  146.                 // std::cout << '1';
  147.             } else {
  148.                 if(!oneEdgePixelsFound_1){
  149.                     std::cout << "Row : " << row << " Col : " << col <<  " Value : 0 " << endl;
  150.                     int tmp_1 = edgeCount_1++;
  151.                     edgeOnLastLineLeftLoc_1[tmp_1][0]=row;
  152.                     edgeOnLastLineLeftLoc_1[tmp_1][1]=col;
  153.                     edgePixelCounter_1++;
  154.                     if(edgePixelCounter_1==1){
  155.                         oneEdgePixelsFound_1=true;
  156.                     }
  157.                 }
  158.             }
  159.             // std::cout << std::endl;
  160.         }
  161.     }
  162.  
  163.         for (int j = 0; j < 1; ++j)
  164.         {
  165.             cout <<  "Value at first line Left Direction :" << edgeOnFirstLineLeftLoc_1[j][1] << endl;
  166.         }
  167.        
  168.  
  169.  
  170.         for (int j = 0; j < 1; ++j)
  171.         {
  172.             cout <<  "Value at Last line Left Direction :" << edgeOnLastLineLeftLoc_1[j][1] << endl;
  173.         }
  174.        
  175.     int rowOneLengthRightPart = edgeOnFirstLineRightLoc[0][1] -80  ;
  176.     int rowOneLengthLeftPart = 80 - edgeOnFirstLineLeftLoc_1[0][1];
  177.  
  178.     cout << "rowOneLengthRightPart : " << rowOneLengthRightPart << " rowOneLengthLeftPart :" << rowOneLengthLeftPart << endl ;
  179.  
  180.     int rowLastLengthRightPart = edgeOnLastLineRightLoc[0][1] - 80;
  181.     int rowLastLengthLeftPart = 80 - edgeOnLastLineLeftLoc_1[0][1];
  182.  
  183.     cout << "rowLastLengthRightPart : " << rowLastLengthRightPart << " rowLastLengthLeftPart :" << rowLastLengthLeftPart << endl ;
  184.  
  185.  
  186.     // cout << "Difference(s) : Fisrt row " << endl;
  187.     int rowOneLength = 0;
  188.     rowOneLength = rowOneLengthLeftPart + rowOneLengthRightPart;
  189.     // for (int i = 0; i < 1; ++i){
  190.     //  for (int j = 0; j < 1; ++j){
  191.     //      cout << edgeOnFirstLineRightLoc[i+1][j+1] - edgeOnFirstLineRightLoc[i][j+1]  << endl;
  192.     //      rowOneLength = edgeOnFirstLineRightLoc[i+1][j+1] - edgeOnFirstLineRightLoc[i][j+1];
  193.  
  194.     //  }
  195.     // }
  196.  
  197.     // cout << "Difference(s) Last Row  : " << endl;
  198.     int rowLastLength=0;
  199.     rowLastLength = rowLastLengthLeftPart + rowLastLengthRightPart;
  200.     // for (int i = 0; i < 1; ++i){
  201.     //  for (int j = 0; j < 1; ++j){
  202.     //      cout << edgeOnLastLineLeftLoc_1[i+1][j+1] -  edgeOnLastLineLeftLoc_1[i][j+1] << endl;
  203.     //      rowLastLength = edgeOnLastLineLeftLoc_1[i+1][j+1] - edgeOnLastLineLeftLoc_1[i][j+1];
  204.     //  }
  205.     // }
  206.  
  207.     int maxDist =0;
  208.     if (  rowOneLength < rowLastLength){
  209.         maxDist = rowLastLength;
  210.         rowOneLength = maxDist;
  211.     }else{
  212.         maxDist = rowOneLength;
  213.         rowLastLength = maxDist;
  214.  
  215.     }
  216.    
  217.     int avgLength = ( rowOneLength + rowLastLength ) / 2;
  218.     cout << "Avegrage Difference(s) of two rows  : " << avgLength << endl;
  219.  
  220.     int CdDiameter = 160;
  221.     int ratio = 160/12;
  222.  
  223.     int wristSizeAvg = avgLength / ratio;
  224.  
  225.     cout << "Avegrage Wrist Size "<< wristSizeAvg << endl;
  226.  
  227.  
  228.     waitKey(0);
  229.    
  230. }
  231.  
  232.  
  233. int main(int argc, char const *argv[])
  234. {
  235.     Mat input = imread(argv[1],-1);
  236.     // Mat input_1;
  237.     // cvtColor(input,input_1,CV_BGR2GRAY);
  238.  
  239.     // measureEdgeFromMiddleWrist(input_1);
  240.     imshow("Input",input);
  241.     measureEdgeFromMiddleWrist(input);
  242.    
  243.     return 0;
  244. }
Advertisement
Add Comment
Please, Sign In to add comment