LineHandle.cpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #include "LineHandle.h"
  2. #include <iostream>
  3. #include <SegDetector.h>
  4. #include <NanoDetector.h>
  5. #include <opencv2\imgproc\types_c.h>
  6. #include<opencv2/imgproc/imgproc_c.h>
  7. using namespace cv;
  8. void LineHandle::crossPointsOfLines(cv::Vec4i lineA, cv::Vec4i lineB,cv::Point2f& crossPoints)
  9. {
  10. float ka = (float)(lineA[3]- lineA[1]) / float(lineA[2] - lineA[0]);//slope of LineA
  11. float kb = (float)(lineB[3] - lineB[1]) / float(lineB[2]- lineB[0]);;//slope of LineB
  12. crossPoints.x = float(ka * lineA[0] - lineA[1] - kb * lineB[0] + lineB[1]) / float(ka - kb);
  13. crossPoints.y = float(ka * kb * (lineA[0] - lineB[1]) - kb * lineA[1] + ka * lineB[1]) / float(ka - kb);
  14. }
  15. bool LineHandle::comp(const Point2f& a, const Point2f& b)
  16. {
  17. return a.y < b.y;
  18. }
  19. long LineHandle::CalcMatSum( Mat img) {
  20. //Mat MatTemp2;
  21. //img.convertTo(MatTemp2, CV_8U);
  22. //cv::Mat_<cv::Vec3b>::iterator it = MatTemp2.begin<cv::Vec3b>(); //由于利用图像迭代器处理图像像素,因此返回类型必须在编译时知道
  23. //cv::Mat_<cv::Vec3b>::iterator itend = MatTemp2.end<cv::Vec3b>();
  24. //long sum = 0;
  25. //for (; it != itend; ++it)
  26. //{
  27. // (*it)[0] = (*it)[0];
  28. // sum += (*it)[0];
  29. //}
  30. //return sum;
  31. int channel = img.channels();
  32. long sum = 0;
  33. int w = img.cols;
  34. int h = img.rows;
  35. //uchar* uc_pixel = nullptr;
  36. for (int row = 0; row < h; row++) {
  37. uchar* uc_pixel = img.data + row * img.step;
  38. int size = sizeof(uc_pixel);
  39. for (int col = 0; col < w; col++) {
  40. /*uc_pixel[0];*/
  41. sum += uc_pixel[0];
  42. uc_pixel += channel;
  43. }
  44. //delete uc_pixel;
  45. }
  46. //uc_pixel = nullptr;// uc_pixel;
  47. return sum;
  48. }
  49. bool LineHandle::ComparePairFirst(std::pair<int, int> a, std::pair<int, int> b)
  50. {
  51. return a.first < b.first;//对于first的升序
  52. }
  53. bool LineHandle::ComparePairSecond(std::pair<int, int>a, std::pair<int, int> b)
  54. {
  55. return a.second < b.second;//对于second的升序
  56. }
  57. double getDistance(CvPoint pointO, CvPoint pointA)
  58. {
  59. double distance;
  60. distance = powf((pointO.x - pointA.x), 2) + powf((pointO.y - pointA.y), 2);
  61. distance = sqrtf(distance);
  62. return distance;
  63. }