opencv Mat 图像数据元素进行排序 、常见Mat数据元素统计计算

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

  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


  // 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 个 ==========


  //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个元素进行排序

  //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)的元素进行排序:

  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;
  sort(c1begin, c1end);
  cout << endl << "sort(c1begin, c1end)" << endl;
  cout << "c1: \n " << c1 << endl;









转载的 常见操作函数: 

OpenCV - Operations on Arrays 
对数组(矩阵)的一些操作
Function (函数名)Use (函数用处)
 
add矩阵加法,A+B的更高级形式,支持mask
scaleAdd矩阵加法,一个带有缩放因子dst(I) = scale * src1(I) + src2(I)
addWeighted矩阵加法,两个带有缩放因子dst(I) = saturate(src1(I) * alpha + src2(I) * beta + gamma)
subtract矩阵减法,A-B的更高级形式,支持mask
multiply矩阵逐元素乘法,同Mat::mul()函数,与A*B区别,支持mask
gemm一个广义的矩阵乘法操作
divide矩阵逐元素除法,与A/B区别,支持mask
abs对每个元素求绝对值
absdiff两个矩阵的差的绝对值
exp求每个矩阵元素 src(I) 的自然数 e 的 src(I) 次幂 dst[I] = esrc(I)
pow求每个矩阵元素 src(I) 的 p 次幂 dst[I] = src(I)p
log求每个矩阵元素的自然数底 dst[I] = log|src(I)| (if src != 0)
sqrt求每个矩阵元素的平方根
min, max求每个元素的最小值或最大值返回这个矩阵 dst(I) = min(src1(I), src2(I)), max同
minMaxLoc定位矩阵中最小值、最大值的位置
compare返回逐个元素比较结果的矩阵
bitwise_and, bitwise_not, bitwise_or, bitwise_xor每个元素进行位运算,分别是和、非、或、异或
cvarrToMat旧版数据CvMat,IplImage,CvMatND转换到新版数据Mat
extractImageCOI从旧版数据中提取指定的通道矩阵给新版数据Mat
randu以Uniform分布产生随机数填充矩阵,同 RNG::fill(mat, RNG::UNIFORM)
randn以Normal分布产生随机数填充矩阵,同 RNG::fill(mat, RNG::NORMAL)
randShuffle随机打乱一个一维向量的元素顺序
theRNG()返回一个默认构造的RNG类的对象 theRNG()::fill(...)
reduce矩阵缩成向量
repeat矩阵拷贝的时候指定按x/y方向重复
split多通道矩阵分解成多个单通道矩阵
merge多个单通道矩阵合成一个多通道矩阵
mixChannels矩阵间通道拷贝,如Rgba[]到Rgb[]和Alpha[]
sort, sortIdx为矩阵的每行或每列元素排序
setIdentity设置单元矩阵
completeSymm矩阵上下三角拷贝
inRange检查元素的取值范围是否在另两个矩阵的元素取值之间,返回验证矩阵
checkRange检查矩阵的每个元素的取值是否在最小值与最大值之间,返回验证结果bool
sum求矩阵的元素和
mean求均值
meanStdDev均值和标准差
countNonZero统计非零值个数
cartToPolar, polarToCart笛卡尔坐标与极坐标之间的转换
flip矩阵翻转
transpose矩阵转置,比较 Mat::t() AT
trace矩阵的迹
determinant行列式 |A|, det(A)
eigen矩阵的特征值和特征向量
invert矩阵的逆或者伪逆,比较 Mat::inv()
magnitude向量长度计算 dst(I) = sqrt(x(I)2 + y(I)2)
MahalanobisMahalanobis距离计算
phase相位计算,即两个向量之间的夹角
norm求范数,1-范数、2-范数、无穷范数
normalize标准化
mulTransposed矩阵和它自己的转置相乘 AT * A, dst = scale(src - delta)T(src - delta)
convertScaleAbs先缩放元素再取绝对值,最后转换格式为8bit型
calcCovarMatrix计算协方差阵
solve求解1个或多个线性系统或者求解最小平方问题(least-squares problem)
solveCubic求解三次方程的根
solvePoly求解多项式的实根和重根
dct, idct正、逆离散余弦变换,idct同dct(src, dst, flags | DCT_INVERSE)
dft, idft正、逆离散傅立叶变换, idft同dft(src, dst, flags | DTF_INVERSE)
LUT查表变换
getOptimalDFTSize返回一个优化过的DFT大小
mulSpecturms两个傅立叶频谱间逐元素的乘法

  • 4
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
OpenCV(Open Source Computer Vision Library)是一个广泛使用的计算机视觉库,提供了许多处理图像和视频的功能。在OpenCV中,Mat是一种常用的数据结构,用于存储和操作图像和矩阵数据Mat是一个多维数组,可以表示灰度图像、彩色图像、深度图像等。它由数据指针、行数、列数、通道数和其他一些元数据组成。 在OpenCV中,Mat数据类型可以是8位、16位、32位浮点型或64位浮点型。对于灰度图像,通常使用单通道的Mat表示,而对于彩色图像,则使用3通道(BGR顺序)的Mat。 你可以通过以下代码创建一个Mat对象并访问其数据: ```cpp #include <opencv2/opencv.hpp> int main() { cv::Mat image; // 创建一个空的Mat对象 // 从文件加载图像 image = cv::imread("image.jpg"); // 访问图像数据 int rows = image.rows; // 获取图像的行数 int cols = image.cols; // 获取图像的列数 int channels = image.channels(); // 获取图像的通道数 // 遍历图像 for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { // 访问像素值 cv::Vec3b pixel = image.at<cv::Vec3b>(i, j); // 对像素进行操作 pixel[0] = 255; // 修改蓝色通道值为255 // 更新像素值 image.at<cv::Vec3b>(i, j) = pixel; } } // 保存图像 cv::imwrite("modified_image.jpg", image); return 0; } ``` 这是一个简单的示例,展示了如何创建、加载、访问和保存图像数据Mat对象的数据可以通过at()方法访问,其中指定了要访问的像素的行和列。对于彩色图像,可以使用Vec3b类型的对象来表示每个像素的值,其中每个通道的值由0到255的整数表示。 希望这能帮到你!如果还有其他问题,请随时问。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值