opencv Mat 图像数据元素进行排序

本文详细介绍了C++标准库中的sort函数使用方法,包括常见错误示例及正确实现方式,特别针对数组和OpenCV Mat类型的排序进行了说明。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

原文:http://blog.csdn.net/qing101hua/article/details/52817373


sortIdx 函数 对元素进行排序, 返回对应的排序索引

  1. Mat c1 = (Mat_<double>(3,3) << 1, 5 , 6 , 2 , 4, 2, 5, 9, 4);  
  2. Mat c2(c1);  
  3. sortIdx(c1, c2, SORT_EVERY_COLUMN + SORT_ASCENDING);  
  4. cout << "c1: \n" << c1 << endl;  
  5. cout << "c2: \n" << c2 << endl;  
  Mat c1 = (Mat_<double>(3,3) << 1, 5 , 6 , 2 , 4, 2, 5, 9, 4);
  Mat c2(c1);
  sortIdx(c1, c2, SORT_EVERY_COLUMN + SORT_ASCENDING);
  cout << "c1: \n" << c1 << endl;
  cout << "c2: \n" << c2 << endl;



sort 排序函数, C++中sort函数 详细介绍参见 :  http://blog.csdn.net/qing101hua/article/details/52822060


  1. // sort  Mat(3,3)  
  2. int* c1begin = c1.ptr<int>(0);  
  3. int* c1end = c1.ptr<int>(2);  
  4. sort(c1begin[0], c1end[2]); // 该行代码 编译报错  
  5. cout << "c1: \n " << c1 << endl;  
  // sort  Mat(3,3)
  int* c1begin = c1.ptr<int>(0);
  int* c1end = c1.ptr<int>(2);
  sort(c1begin[0], c1end[2]); // 该行代码 编译报错
  cout << "c1: \n " << c1 << endl;

1>C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\algorithm(3093): error : operand of "*" must be a pointer
1>C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\algorithm(3093): error : operand of "*" must be a pointer
1>C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\algorithm(3093): error : no instance of function template "std::less<void>::operator()" matches the argument list
1>C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\algorithm(2288): error : operand of "*" must be a pointer
1>C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\algorithm(2288): error : operand of "*" must be a pointer
1>C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\algorithm(2292): error : operand of "*" must be a pointer
1>C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\algorithm(2292): error : operand of "*" must be a pointer
1>  main.cu
1>C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\V120\BuildCustomizations\CUDA 7.5.targets(604,9): error MSB3721: 命令“"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v7.5\bin\nvcc.exe" -gencode=arch=compute_20,code=\"sm_20,compute_20\" --use-local-env --cl-version 2013 -ccbin "C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\bin\x86_amd64"  -I"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v7.5\include" -I"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v7.5\include"  -G   --keep-dir x64\Debug -maxrregcount=0  --machine 64 --compile -cudart static  -g   -DWIN32 -DWIN64 -D_DEBUG -D_CONSOLE -D_MBCS -Xcompiler "/EHsc /W3 /nologo /Od /Zi /RTC1 /MDd " -o x64\Debug\main.cu.obj "D:\work\test\wb\wb\main.cu"”已退出,返回代码为 2。
========== 全部重新生成:  成功 0 个,失败 1 个,跳过 0 个 ==========


  1. //sort int[9];  
  2. int d1[] = { 2, 4, 5, 2, 1, 8, 6, 7, 9 };  
  3. sort(d1[0], d1[7]);   // 报错 ,编译不过去  
  4. //cout << "d1: \n" << d1 << endl;  
  //sort int[9];
  int d1[] = { 2, 4, 5, 2, 1, 8, 6, 7, 9 };
  sort(d1[0], d1[7]);   // 报错 ,编译不过去
  //cout << "d1: \n" << d1 << endl;

报错:

1>C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\algorithm(3093): error : no instance of function template "std::less<void>::operator()" matches the argument list
1>C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\algorithm(2288): error : operand of "*" must be a pointer
1>C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\algorithm(2288): error : operand of "*" must be a pointer
1>C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\algorithm(2292): error : operand of "*" must be a pointer
1>C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\algorithm(2292): error : operand of "*" must be a pointer
1>  main.cu
1>C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\V120\BuildCustomizations\CUDA 7.5.targets(604,9): error MSB3721: 命令“"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v7.5\bin\nvcc.exe" -gencode=arch=compute_20,code=\"sm_20,compute_20\" --use-local-env --cl-version 2013 -ccbin "C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\bin\x86_amd64"  -I"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v7.5\include" -I"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v7.5\include"  -G   --keep-dir x64\Debug -maxrregcount=0  --machine 64 --compile -cudart static  -g   -DWIN32 -DWIN64 -D_DEBUG -D_CONSOLE -D_MBCS -Xcompiler "/EHsc /W3 /nologo /Od /Zi /RTC1 /MDd " -o x64\Debug\main.cu.obj "D:\work\test\wb\wb\main.cu"”已退出,返回代码为 2。
========== 生成:  成功 0 个,失败 1 个,最新 0 个,跳过 0 个 ==========


解决:

将sort输入参数 改成数据索引的指针

1、对int[9] 进行测试, 对int[9]的前6个元素进行排序

  1. //sort int[9];  
  2. int d1[9] = { 2, 4, 5, 2, 1, 8, 6, 7, 9 };  
  3. cout << "d1[]: " << endl;  
  4. for (int i = 0; i < 9; i++)  
  5.  cout << d1[i] << endl;  
  6.   
  7. sort(d1, d1+5);   // 注意: 参数如果不是指针变量的话 会报错 ,编译不过去  
  8. cout << "d1[]: " << endl;  
  9. for (int i = 0; i < 6; i++)  
  10.  cout << d1[i] << endl;  
  //sort int[9];
  int d1[9] = { 2, 4, 5, 2, 1, 8, 6, 7, 9 };
  cout << "d1[]: " << endl;
  for (int i = 0; i < 9; i++)
	  cout << d1[i] << endl;

  sort(d1, d1+5);   // 注意: 参数如果不是指针变量的话 会报错 ,编译不过去
  cout << "d1[]: " << endl;
  for (int i = 0; i < 6; i++)
	  cout << d1[i] << endl;


2、对Mat(3,3)的元素进行排序:

  1. int* c1begin = c1.ptr<int>(0);  
  2. int* c1end = c1.ptr<int>(2);  
  3. sort(c1begin, c1end+2);  
  4. cout << endl<<"sort(c1begin, c1end+2)" << endl;  
  5. cout << "c1: \n " << c1 << endl;  
  6.   
  7. sort(c1begin, c1end + 3);  
  8. cout <<endl << "sort(c1begin, c1end+3)" << endl;  
  9. cout << "c1: \n " << c1 << endl;  
  int* c1begin = c1.ptr<int>(0);
  int* c1end = c1.ptr<int>(2);
  sort(c1begin, c1end+2);
  cout << endl<<"sort(c1begin, c1end+2)" << endl;
  cout << "c1: \n " << c1 << endl;
  
  sort(c1begin, c1end + 3);
  cout <<endl << "sort(c1begin, c1end+3)" << endl;
  cout << "c1: \n " << c1 << endl;
  1. sort(c1begin, c1end);  
  2. cout << endl << "sort(c1begin, c1end)" << endl;  
  3. cout << "c1: \n " << c1 << endl;  
  sort(c1begin, c1end);
  cout << endl << "sort(c1begin, c1end)" << endl;
  cout << "c1: \n " << c1 << endl;


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值