#include "LineHandle.h" using namespace cv; void LineHandle::crossPointsOfLines(cv::Vec4i lineA, cv::Vec4i lineB, cv::Point2f& crossPoints) { float ka = (float)(lineA[3] - lineA[1]) / float(lineA[2] - lineA[0]);//slope of LineA float kb = (float)(lineB[3] - lineB[1]) / float(lineB[2] - lineB[0]);;//slope of LineB crossPoints.x = float(ka * lineA[0] - lineA[1] - kb * lineB[0] + lineB[1]) / float(ka - kb); crossPoints.y = float(ka * kb * (lineA[0] - lineB[1]) - kb * lineA[1] + ka * lineB[1]) / float(ka - kb); } bool LineHandle::comp(const Point2f& a, const Point2f& b) { return a.y < b.y; } long LineHandle::CalcMatSum(Mat img) { //Mat MatTemp2; //img.convertTo(MatTemp2, CV_8U); //cv::Mat_::iterator it = MatTemp2.begin(); //由于利用图像迭代器处理图像像素,因此返回类型必须在编译时知道 //cv::Mat_::iterator itend = MatTemp2.end(); //long sum = 0; //for (; it != itend; ++it) //{ // (*it)[0] = (*it)[0]; // sum += (*it)[0]; //} //return sum; int channel = img.channels(); long sum = 0; int w = img.cols; int h = img.rows; //uchar* uc_pixel = nullptr; for (int row = 0; row < h; row++) { uchar* uc_pixel = img.data + row * img.step; int size = sizeof(uc_pixel); for (int col = 0; col < w; col++) { /*uc_pixel[0];*/ sum += uc_pixel[0]; uc_pixel += channel; } //delete uc_pixel; } //uc_pixel = nullptr;// uc_pixel; return sum; } bool LineHandle::ComparePairFirst(std::pair a, std::pair b) { return a.first < b.first;//对于first的升序 } bool LineHandle::ComparePairSecond(std::paira, std::pair b) { return a.second < b.second;//对于second的升序 } double getDistance(CvPoint pointO, CvPoint pointA) { double distance; distance = powf((pointO.x - pointA.x), 2) + powf((pointO.y - pointA.y), 2); distance = sqrtf(distance); return distance; }