对整个垫子图像进行排序并在 OpenCV 中存储索引
sort whole Mat image and store indices in OpenCV?
这个问题不是这个问题的重复
我想对整个Mat image
进行排序并存储类似于MatLab's [B , Ix] = sort(A);
的索引
但是在 openCV 中,sort() 和 sortIdx() 仅适用于 EACH_ROW
或 EACH_COLUMN
。
问题:那么,如何对整个Mat
进行排序并将Indices
也存储在 openCV 中?
PS:我想得到以下内容:
输入 =
2 0
4 1
dst_index =
11
22
dst_sorted =
0 1
2 4
有一个解决方案,但只有当图像在内存中连续时,它才有效。通常情况下,除非您的图像只是较大图像的投资回报率。您可以使用函数 isContinuous 来检查这一点。诀窍是创建使用与原始图像相同的内存缓冲区的 Mat,但不是将其视为 N 行和 M 列,而是将其视为 1 行和 M*N 列。这可以通过整形功能来完成。
Mat sameMemoryNewShape = image.reshape(1, 1);
现在你可以在sameMemoryNewShape上使用sort()或sortIdx()。
基于"Michael Burdinov"的解决方案,以下内容应该有效。
Mat A = (Mat_<uchar>(3, 4) << 0, 5, 2, 5, 2, 4, 9, 12, 3, 12, 11, 1); // example matrix
Mat B; //storing the 1D index result
Mat C = A.reshape(1, 1); //as mentioned by Michael Burdinov
sortIdx(C, B, SORT_EVERY_ROW + SORT_ASCENDING);
for (int i = 0; i < B.cols; i++) //from index 0 to index rows * cols of the original image
{
int val = B.at<int>(0, i); //access B, which is in format CV_32SC1
int y = val / A.cols; //convert 1D index into 2D index
int x = val % A.cols;
std::cout << "idx " << val << " at (x/y) " << x << "/" << y
<< " is " << (int) A.at<uchar>(y, x) << endl;
}
请注意,结果B
以 CV_32SC1(32 位整数)为单位。不考虑这可能会导致"奇怪"的行为,并可能导致1 0 0 0
。
相关文章:
- 数组索引的值没有增加
- C++,OpenCV,尝试显示图像时"OpenCV(4.3.0) Error: Assertion failed (size.width>0 && size.height>0)"此错误
- 尝试导入pybind-opencv模块时出现libgtk错误
- 在编译C++代码(具有dlib和opencv)到WASM时面临问题
- 芬威克树(BIT).找到具有给定累积频率的最小索引,单位为 O(logN)
- C++OpenCV中的快速索引
- 在 openCV 矩阵中查找列最大值的索引和值
- 将RGB颜色图像转换为OpenCV C 中的索引颜色图像类型
- OPENCV视频关注设备索引 /设备编号
- 对整个垫子图像进行排序并在 OpenCV 中存储索引
- Opencv Mat的元素索引产生垃圾
- 在OpenCV中使用Mat时exc_bad_access,尽管看起来我的索引是正确的
- 如何在opencv中创建cv::matrix的向量,并将图像数据的子矩阵分配给向量的所有索引上的矩阵
- OpenCV C++:从存储在 int 向量的索引中为垫子类型数据创建列范围
- 如何索引和修改OpenCV矩阵
- OpenCV距离变换中的像素索引
- 在opencv中索引矩阵的最佳方式
- c++ /OpenCV -我怎么能得到我的图像后使用flann::索引?(转炉)
- OpenCV稀疏矩阵:如何在一行/列中获得非零元素的索引
- OpenCV OCL逻辑索引