example.cpp 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. #include "opencv2/core.hpp"
  2. #include "opencv2/imgcodecs.hpp"
  3. #include "opencv2/hfs.hpp"
  4. using namespace cv;
  5. using namespace cv::hfs;
  6. int main(int argc, char *argv[])
  7. {
  8. // invalid number of command line parameter
  9. if( argc != 2 ) {
  10. return EXIT_FAILURE;
  11. }
  12. char* path = argv[1];
  13. // read in a pictrue to initialize the height and width
  14. Mat src = imread(path), res;
  15. int _h = src.rows, _w = src.cols;
  16. // initialize the HfsSegment object
  17. // In this example, we used default paramters.
  18. // However, bear in mind that you can pass in your
  19. // own parameters in with this function.
  20. Ptr<HfsSegment> h = HfsSegment::create( _h, _w );
  21. // segment and write the first result.
  22. res = h->performSegmentGpu(src);
  23. imwrite( "segment_default_gpu.jpg", res );
  24. // also, there is CPU interface for that
  25. res = h->performSegmentCpu(src);
  26. imwrite( "segment_default_cpu.jpg", res );
  27. // also, instead of getting a segmented image
  28. // from our interface, you can also choose to not to
  29. // draw the result on the Mat and only get a matrix
  30. // of index. Note that the data type of the returned
  31. // Mat in this case is CV_16U
  32. Mat idx_mat = h->performSegmentGpu( src, false );
  33. // also, you can change any parameters as you want
  34. h->setSlicSpixelSize(10);
  35. res = h->performSegmentGpu(src);
  36. imwrite( "segment_changed_param.jpg", res );
  37. return 0;
  38. }