LineHandle.cpp 2.0 KB

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