76 lines
2.1 KiB
C++
76 lines
2.1 KiB
C++
#include "LineHandle.h"
|
|
#include <iostream>
|
|
#include <SegDetector.h>
|
|
#include <NanoDetector.h>
|
|
#include <opencv2\imgproc\types_c.h>
|
|
#include<opencv2/imgproc/imgproc_c.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_<cv::Vec3b>::iterator it = MatTemp2.begin<cv::Vec3b>(); //由于利用图像迭代器处理图像像素,因此返回类型必须在编译时知道
|
|
//cv::Mat_<cv::Vec3b>::iterator itend = MatTemp2.end<cv::Vec3b>();
|
|
//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<int, int> a, std::pair<int, int> b)
|
|
{
|
|
return a.first < b.first;//对于first的升序
|
|
}
|
|
bool LineHandle::ComparePairSecond(std::pair<int, int>a, std::pair<int, int> 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;
|
|
|
|
}
|
|
|