首页 > 代码库 > 类matlab find函数

类matlab find函数

逻辑矩阵,找出元素1并记录其位置索引。

 

 1 int main(int argc, char** argv) 2 { 3     unsigned char acSrc[4][4] = 4     { 5         0, 1, 1, 0, 6         1, 0, 0, 1, 7         1, 0, 1, 1, 8         1, 1, 0, 0, 9     };10     printf("acSrc-data address:%p\n", acSrc);11 12     Mat src(4, 4, CV_8UC1, acSrc);13     printf("src-data address:%p\n", src.data);14 15     cout << src << endl;16 17     vector<int> vec_row;18     vector<int> vec_col;19 20     for (int j = 0; j < src.cols; ++j)    21     {22         for (int i = 0; i < src.rows; ++i)23         {24             if (1 == src.at<unsigned char>(i, j))25             {26                 vec_row.push_back(i);27                 vec_col.push_back(j);28             }29             //cout << src.at<int>(i, j) << endl;30         }31     }32 33     cout << "vec_row.size:" << vec_row.size() << endl;34     cout << "vec_col.size:" << vec_col.size() << endl;35 36     vector<int>::iterator iter_row = vec_row.begin();37     vector<int>::iterator iter_col = vec_col.begin();38 39     Mat m = Mat::zeros(4, 4, CV_8UC1); 40     cout << m << endl;41 42     for (; (iter_row != vec_row.end()) && (iter_col != vec_col.end()); ++iter_row, ++iter_col)43     {44         cout << *iter_row << ", " <<  *iter_col << endl; 45         46         m.at<uchar>(*iter_row, *iter_col) = 1;47     }48 49     cout << m << endl;50 51     return 0;52 }

 

类matlab find函数