/scratchProc.cpp

https://bitbucket.org/jinna/tracking_cpp · C++ · 117 lines · 97 code · 20 blank · 0 comment · 13 complexity · 1ba4ec575beb41cc375cf56d4be72691 MD5 · raw file

  1. # include <iostream>
  2. # include <opencv/cv.h>
  3. # include <opencv/highgui.h>
  4. # include "utils.h"
  5. using namespace cv;
  6. int main(int argc, char** argv) {
  7. int cwa = CV_WINDOW_AUTOSIZE;
  8. Mat img, mask;
  9. std::string inpath = argv[1];
  10. mask = imread(inpath.c_str(), GRAYSCALE_IMAGE);
  11. if (mask.data == NULL) {
  12. std::cerr << "ERROR reading image 2" << std::endl;
  13. exit(-1);
  14. }
  15. namedWindow("mask", cwa);
  16. imshow("mask", mask);
  17. vector<vector<Point> > contours;
  18. findContours(mask, contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE);
  19. std::cout << "contours size" << contours.size() << std::endl;
  20. std::cout << "1st contour size" << contours[0].size() << std::endl;
  21. Mat disp;
  22. cvtColor(mask, disp, CV_GRAY2RGB);
  23. drawContours(disp, contours, -1, Scalar(0,255,0),2,4);
  24. namedWindow("contours", cwa);
  25. imshow("contours", disp);
  26. waitKey(0);
  27. float x = atof(argv[2]);
  28. float y = atof(argv[3]);
  29. Point midpt(x,y);
  30. float dx = atof(argv[4]);
  31. float dy = atof(argv[5]);
  32. float z = sqrt(dx*dx + dy*dy);
  33. dx = dx/z;
  34. dy = dy/z;
  35. Vec2f norm(dx, dy);
  36. std::cout << "in params: " << x << " " << y << " " << dx << " " << dy << std::endl;
  37. float radius = 100;
  38. float ex_armw = 23;
  39. Point point1(norm[0]*ex_armw + x, norm[1]*ex_armw + y);
  40. Point point2(-norm[0]*ex_armw + x, -norm[1]*ex_armw + y);
  41. vector<Point> chosenPts1;
  42. vector<Point> chosenPts2;
  43. for (int i=0; (unsigned)i<contours.size(); i++) {
  44. for (int j=0; (unsigned)j<contours[i].size(); j++) {
  45. Point diff1 = point1 - contours[i][j];
  46. Point diff2 = point2 - contours[i][j];
  47. float dist1sq = diff1.x*diff1.x + diff1.y*diff1.y;
  48. float dist2sq = diff2.x*diff2.x + diff2.y*diff2.y;
  49. if (dist1sq <= radius*radius) {
  50. Point newpoint(contours[i][j]);
  51. chosenPts1.push_back(newpoint);
  52. }
  53. if (dist2sq <= radius*radius) {
  54. Point newpoint(contours[i][j]);
  55. chosenPts2.push_back(newpoint);
  56. }
  57. }
  58. }
  59. Mat Dx, Dy;
  60. inpath = argv[6];
  61. Dx = imread(inpath.c_str(), GRAYSCALE_IMAGE);
  62. if (Dx.data == NULL) {
  63. std::cerr << "ERROR reading image 3" << std::endl;
  64. exit(-1);
  65. }
  66. inpath = argv[7];
  67. Dy = imread(inpath.c_str(), GRAYSCALE_IMAGE);
  68. if (Dy.data == NULL) {
  69. std::cerr << "ERROR reading image 4" << std::endl;
  70. exit(-1);
  71. }
  72. namedWindow("Dx", cwa);
  73. imshow("Dx", Dx);
  74. namedWindow("Dy", cwa);
  75. imshow("Dy", Dy);
  76. std::cout << "Dx, Dy type: " << Dx.type() << std::endl;
  77. float dirSimThresh = .9;
  78. Point bestPt1(0,0);
  79. float bestscore = -10000000;
  80. for (int i=0; (unsigned)i<chosenPts1.size(); i++) {
  81. int r = (int)chosenPts1[i].y;
  82. int c = (int)chosenPts1[i].x;
  83. float gradx = Dx.at<float>(r,c);
  84. float grady = Dy.at<float>(r,c);
  85. float dirSim = (gradx*dx + grady*dy)/sqrt(gradx*gradx + grady*grady);
  86. float gradStrength = sqrt(gradx*gradx + grady*grady);
  87. if (dirSim > dirSimThresh) {
  88. std::cout << chosenPts1[i].x << " " << chosenPts1[i].y << std::endl;
  89. if (gradStrength > bestscore) {
  90. bestPt1.x = chosenPts1[i].x;
  91. bestPt1.y = chosenPts1[i].y;
  92. bestscore = gradStrength;
  93. }
  94. }
  95. }
  96. std::cout << "bestPt1=" << bestPt1.x << " " << bestPt1.y << std::endl;
  97. }