OpenCV学习--Mat::rowRange函数

                    版权声明:本文为博主原创文章,未经博主允许不得转载。                        https://blog.csdn.net/Zdreamcsdn/article/details/77542531                    </div>
                                                <link rel="stylesheet" href="https://csdnimg.cn/release/phoenix/template/css/ck_htmledit_views-3019150162.css">
                                    <link rel="stylesheet" href="https://csdnimg.cn/release/phoenix/template/css/ck_htmledit_views-3019150162.css">
            <div class="htmledit_views" id="content_views">

课本解释两者为:
rowRange为指定的行span创建一个新的矩阵头,可取指定行区间元素
colRange为指定的列span创建一个新的矩阵头,可取指定列区间元素

自我初步理解:
rowRange(int x, int y)   (其中y应小于等于行数,例如一个矩阵最大为5行,那么y最大为4) 的创建矩阵范围为从x行为首行开始,往后数y-x行。
例如:rowRange(0,3) 位 从第0行开始,往后数3行。即 0 、1、2行。
colRange(int x,int y)同理。


Mat.rowRange(int x,int y)和Mat.rowRange(range(int x,int y)得到的结果一样,函数取的实际行数y-x,只取到范围的左边界,而不取右边界。

特别注意:网上好多的文章虽然测试结果正确,但是结论却是错误的,曾被误导的飘过~




  
  

  
  
  1. #include<stdio.h>
  2. #include<opencv.hpp>
  3. #include<iostream>
  4. using namespace cv;
  5. using namespace std;
  6. int main()
  7. {
  8. //初始化一个3*3的矩阵
  9. Mat examples = (Mat_< float>( 3, 3) << 1, 0, 0, 0, 1, 0, 0, 0, 1);
  10. //取example中中特定范围的行列构成矩阵
  11. Mat temp_row1 = examples.rowRange(Range( 1, 2)); //取特定行
  12. Mat temp_row2 = examples.rowRange( 1, 2);
  13. Mat temp_row3 = examples.rowRange( 0, 2);
  14. Mat temp_col = examples.colRange( 1, 3); //取特定列
  15. //n,p,m,q计数,用于控制矩阵元素的输出格式
  16. int n = 0;
  17. int p = 0;
  18. int m = 0;
  19. int q = 0;
  20. int z = 0;
  21. //输出初始矩阵的元素
  22. cout << "examples:" << endl;
  23. for ( int i = 0; i<examples.rows; i++){
  24. for ( int j = 0; j<examples.cols; j++)
  25. {
  26. cout << examples.at< float>(i, j);
  27. p++;
  28. if (p % 3 == 0) cout << endl;
  29. }
  30. }
  31. cout << endl;
  32. //测试rowRange(Range(int x,int y))
  33. cout << "temp_row1:" << endl;
  34. for ( int i = 0; i<temp_row1.rows; i++)
  35. {
  36. for ( int j = 0; j<temp_row1.cols; j++){
  37. cout << temp_row1.at< float>(i, j);
  38. n++;
  39. if (n % 3 == 0)
  40. cout << endl;
  41. }
  42. }
  43. cout << endl;
  44. //测试rowRange(int x,int y)函数
  45. cout << "temp_row2:" << endl;
  46. for ( int i = 0; i<temp_row2.rows; i++)
  47. {
  48. for ( int j = 0; j<temp_row2.cols; j++){
  49. cout << temp_row2.at< float>(i, j);
  50. m++;
  51. if (m % 3 == 0)
  52. cout << endl;
  53. }
  54. }
  55. cout << endl;
  56. cout << "temp_row3:" << endl;
  57. for ( int i = 0; i < temp_row3.rows; i++)
  58. {
  59. for ( int j = 0; j < temp_row3.cols; j++)
  60. {
  61. cout << temp_row3.at< float>(i, j);
  62. z++;
  63. if (z % 3 == 0)
  64. cout << endl;
  65. }
  66. }
  67. cout << endl;
  68. //测试colRange(int x,int y)函数
  69. cout << "temp_col:" << endl;
  70. for ( int i = 0; i<temp_col.rows; i++)
  71. {
  72. for ( int j = 0; j<temp_col.cols; j++){
  73. cout << temp_col.at< float>(i, j);
  74. q++;
  75. if (q % 2 == 0)
  76. cout << endl;
  77. }
  78. }
  79. return 0;
  80. }


说明:部分代码来自网络

发布了3 篇原创文章 · 获赞 21 · 访问量 3万+
                    版权声明:本文为博主原创文章,未经博主允许不得转载。                        https://blog.csdn.net/Zdreamcsdn/article/details/77542531                    </div>
                                                <link rel="stylesheet" href="https://csdnimg.cn/release/phoenix/template/css/ck_htmledit_views-3019150162.css">
                                    <link rel="stylesheet" href="https://csdnimg.cn/release/phoenix/template/css/ck_htmledit_views-3019150162.css">
            <div class="htmledit_views" id="content_views">

猜你喜欢

转载自blog.csdn.net/Crystal_YS/article/details/98069918
今日推荐