2016-09-07 1 views
1

私は別の行列から1つの列だけを使って行列を作成しようとしていますが、もちろんデータをコピーしています。データをコピーして行列をサイズ変更するopencv

void redim(Mat in , Mat &out) { 

    for (int l=0 ; l < in.rows*in.cols ; l++){ 
    for(int j=0; j< in.rows ; j++){ 
     for(int i=0 ; i < in.cols; i++){ 
     out.at <float> (l,0)= in.at <float> (j,i); 
     } 
    } 
    } 
} 
int main(){ 
Mat It3; 
It3 = (Mat_<double>(2,3) << 0,4,6,7,8,9); 
Mat S= Mat :: zeros (It3.rows* It3.cols , 1, CV_32FC1) ; 
redim(It3,S); 
waitKey(); 
} 

しかし、私は結果として行列S=[0;0;0;0;0;0]を得ました。

答えて

1
  1. データタイプとしてfloatdoubleが混在しています。 1つを選択してください!
  2. forループは意味をなさない。

次のことが可能です。

    toSingleColumn2
  • cv::reshapeを使用するなど、forループを使用しますが、インデックスを使用してtoSingleColumn1
  • のよう
  • のように、まさにこれを実行している、あなたのforループを修正in toSingleColumn3

コードを参照してください以下のとおりです。 b1,b2およびb3は、等しくなる。

#include <opencv2\opencv.hpp> 
using namespace cv; 

// Use two for loops, with coordinates 
void toSingleColumn1(const Mat1f& src, Mat1f& dst) 
{ 
    int N = src.rows * src.cols; 
    dst = Mat1f(N, 1); 
    int i=0; 
    for (int r = 0; r < src.rows; ++r) { 
     for (int c = 0; c < src.cols; ++c) { 
      dst(i++, 0) = src(r, c); 
     } 
    } 
} 

// Use a single for loop, with indices 
void toSingleColumn2(const Mat1f& src, Mat1f& dst) 
{ 
    int N = src.rows * src.cols; 
    dst = Mat1f(N, 1); 

    for (int i = 0; i < N; ++i) { 
     dst(i) = src(i); 
    } 
} 

// Use cv::reshape 
void toSingleColumn3(const Mat& src, Mat& dst) 
{ 
    // The 'clone()' is needed to deep copy the data 
    dst = src.reshape(src.channels(), src.rows*src.cols).clone(); 
} 

int main() 
{ 
    Mat1f a = (Mat1f(2,3) << 0.f, 4.f, 6.f, 7.f, 8.f, 9.f); 

    Mat1f b1, b2, b3; 
    toSingleColumn1(a, b1); 
    toSingleColumn2(a, b2); 
    toSingleColumn3(a, b3); 

    return 0; 
} 
関連する問題