/cob_texture_categorization/common/src/run_meanshift_test.cpp

https://github.com/ipa320/cob_object_perception · C++ · 86 lines · 38 code · 19 blank · 29 comment · 5 complexity · 4c8f2b88554ba1217a72a2700bbd55e0 MD5 · raw file

  1. #include "opencv2/core/core.hpp"
  2. #include "opencv2/highgui/highgui.hpp"
  3. #include "opencv2/imgproc/imgproc.hpp"
  4. #include "opencv2/imgproc/imgproc_c.h"
  5. #include "cob_texture_categorization/run_meanshift_test.h"
  6. #include "cob_texture_categorization/meanshift.h"
  7. #include "cob_texture_categorization/meanshift_3d.h"
  8. run_meanshift_test::run_meanshift_test()
  9. {
  10. }
  11. void run_meanshift_test::run_test(cv::Mat* image, cv::Mat depth, std::vector < std::vector<cv::Mat> >* regions)
  12. {
  13. //IplImage *img = cvLoadImage("/home/rmb-dh/obst.jpg");
  14. IplImage *img = new IplImage(*image);
  15. cv::Mat imageO = (*image).clone();
  16. //IplImage *imgO = new IplImage(imageO);
  17. // Mean shift
  18. int **ilabels = new int *[img->height];
  19. for(int i=0;i<img->height;i++) ilabels[i] = new int [img->width];
  20. int regionCount = MeanShift3D(img, ilabels, &depth);
  21. std::vector<int> color(regionCount);
  22. CvRNG rng= cvRNG(cvGetTickCount());
  23. for(int i=0;i<regionCount;i++)
  24. color[i] = cvRandInt(&rng);
  25. // Mean shift3
  26. //int **ilabelsO = new int *[img->height];
  27. //for(int i=0;i<imgO->height;i++) ilabelsO[i] = new int [imgO->width];
  28. //int regionCountO = MeanShift(imgO, ilabelsO, &depth);
  29. //vector<int> colorO(regionCountO);
  30. //CvRNG rngO= cvRNG(cvGetTickCount());
  31. //for(int i=0;i<regionCountO;i++)
  32. //colorO[i] = cvRandInt(&rngO);
  33. (*regions).resize(regionCount);
  34. for(int i=0;i<regionCount;i++)
  35. {
  36. (*regions)[i].resize(2);
  37. (*regions)[i][0] = cv::Mat::zeros(480,640,CV_8UC3);
  38. (*regions)[i][1] = cv::Mat::zeros(480,640,CV_32F);
  39. }
  40. // Draw random color
  41. for(int i=0;i<img->height;i++)
  42. for(int j=0;j<img->width;j++)
  43. {
  44. //int cl = ilabels[i][j];
  45. //((uchar *)(img->imageData + i*img->widthStep))[j*img->nChannels + 0] = (color[cl])&255;
  46. //((uchar *)(img->imageData + i*img->widthStep))[j*img->nChannels + 1] = (color[cl]>>8)&255;
  47. //((uchar *)(img->imageData + i*img->widthStep))[j*img->nChannels + 2] = (color[cl]>>16)&255;
  48. //int clO = ilabelsO[i][j];
  49. //((uchar *)(imgO->imageData + i*imgO->widthStep))[j*imgO->nChannels + 0] = (colorO[clO])&255;
  50. //((uchar *)(imgO->imageData + i*imgO->widthStep))[j*imgO->nChannels + 1] = (colorO[clO]>>8)&255;
  51. //((uchar *)(imgO->imageData + i*imgO->widthStep))[j*imgO->nChannels + 2] = (colorO[clO]>>16)&255;
  52. int reg = ilabels[i][j];
  53. (*regions)[reg][0].at<cv::Vec3b>(i,j)[0]=(*image).at<cv::Vec3b>(i,j)[0];
  54. (*regions)[reg][0].at<cv::Vec3b>(i,j)[1]=(*image).at<cv::Vec3b>(i,j)[1];
  55. (*regions)[reg][0].at<cv::Vec3b>(i,j)[2]=(*image).at<cv::Vec3b>(i,j)[2];
  56. (*regions)[reg][1].at<float>(i,j)=depth.at<float>(i,j);
  57. }
  58. //cvNamedWindow("MeanShift",CV_WINDOW_AUTOSIZE);
  59. //cvShowImage("MeanShift3D",img);
  60. //cv::moveWindow("MeanShift3D", 10,10);
  61. //cvShowImage("MeanShift",imgO);
  62. //cv::moveWindow("MeanShift", 800,10);
  63. //cvWaitKey();
  64. //cvDestroyWindow("MeanShift");
  65. //
  66. //cvReleaseImage(&img);
  67. }