归并排序向量 c++ opencv
Posted
技术标签:
【中文标题】归并排序向量 c++ opencv【英文标题】:mergesort vector c+ opencv 【发布时间】:2016-09-07 19:53:26 【问题描述】:我需要为不存在的点订购一个坐标向量,但要订购通过将坐标指向矩阵中获得的值。例如
vector < Point > // v has two coordinates within
Mat D ( 2,2 ) ;
int a = v [ 0 ] .x ;
int b = v [ 0 ] .y ;
int c = v [ 1 ] .x ;
int d = v [ 1 ] .y ;
if ( D.at < int > ( a, b) < D.at<int> ( c, d ) )
// Sort in ascending order
我尝试使用合并排序并进行适当的更改来搜索“坐标值”:
void work::merge(vector<cv::Point>& Q1, int low, int high, int mid, Mat & Densita)
cout << "merge" << endl;
int i, j, k;
vector<cv::Point> c;
i = low;
k = low;
j = mid + 1;
while (i <= mid && j <= high)
cout << "-" << endl;
int X = Q1[i].x;
int Y = Q1[i].y;
int W = Q1[j].x;
int Q = Q1[j].y;
int a = Densita.at<int>(X, Y);
int b = Densita.at<int>(W,Q);
if (a < b)
cout << "min" << endl;
c.push_back(cv::Point(X, Y));
k++;
i++;
else
cout << "max" << endl;
c.push_back(cv::Point(W, Q));
k++;
j++;
while (i <= mid)
cout << "D" << endl;
int X = Q1[i].x;
int Y = Q1[i].y;
c.push_back(cv::Point(X, Y));
k++;
i++;
while (j <= high)
cout << "§" << endl;
int W = Q1[j].x;
int Q = Q1[j].y;
c.push_back(cv::Point(W, Q));
k++;
j++;
for (k = low; k < high; k++)
cout << "***" << endl;
Q1.at(k) = c.at(k);
void work::mergesort(vector<cv::Point>& Q1, int low, int high, Mat & Densita)
cout << "RichiamoMergesort" << endl;
int mid;
if (low < high)
mid = (low + high) / 2;
mergesort(Q1, low, mid, Densita);
mergesort(Q1, mid + 1, high, Densita);
merge(Q1, low, high, mid, Densita);
但进入合并后他工作了一点,然后返回给我这个错误
invalid vector<T> subscript ------>with vs2015
为什么?我究竟做错了什么?怎么解决 ?
【问题讨论】:
【参考方案1】:您应该通过为std::sort
函数提供自定义比较器来实现此目的,而不是通过重写它。大致如下:
struct PointCmp
Mat* d;
PointCmp(Mat* _d): d(_d)
inline bool operator() (const Point& p1, const Point& p2)
return d->at<int>(p1.x, p1.y) < d->at<int>(p2.x, p2.y);
;
int main()
// other stuff
std::vector<Point> v;
Mat d(2, 2);
std::sort(v.begin(), v.end(), PointCmp(&d));
【讨论】:
以上是关于归并排序向量 c++ opencv的主要内容,如果未能解决你的问题,请参考以下文章