2015-12-14 46 views
0

我試圖比較16幅圖像(N = 4,M = 4)的大小和方向,並檢查它們之間的連續性。比較set_intersection(公共元素)的大小和方向使用?

所以,我比較像0與圖像1,圖像2 ...圖像15.

然後,圖像1,用圖像2,圖像3 ..image 15

每一個形象,尺寸200x200和CV_32F類型。

我有一個:

vector<Mat> smallImages; 
smallImages.reserve(N * M); //(N = 4 , M = 4) 

我已經計算出了梯度:

vector<Mat> grad_x; 
    vector<Mat> grad_y; 
    grad_x.resize(N*M); 
    grad_y.resize(N*M); 

    // gradients x and y 
    for (int idx = 0; idx < N*M; idx++) 
    { 
     Sobel(smallImages[ idx ], grad_x[ idx ], ddepth, 1, 0, 3, scale, delta, BORDER_DEFAULT); 
     Sobel(smallImages[ idx ], grad_y[ idx ], ddepth, 0, 1, 3, scale, delta, BORDER_DEFAULT); 
    } 

然後,大小和方向:

// magnitude and direction 
    vector<Mat> magnitude; 
    vector<Mat> direction; 
    magnitude.resize(N*M); 
    direction.resize(N*M); 

    for (int idx = 0; idx < N*M; idx++) 
    { 
     cartToPolar(grad_x[ idx ], grad_y[ idx ], magnitude[ idx ], direction[ idx ]); 
    } 

然後,我想比較(例如像[this]那樣的幅度)1

vector<Mat> MagnitudeComp; 
    MagnitudeComp.resize(N*M * N*M); 

    std::vector<cv::Mat>::iterator it; 
    std::sort(magnitude.begin(), magnitude.end()); 


    it = std::set_intersection(magnitude.begin(), magnitude.end(), direction.begin(), direction.end(), MagnitudeComp.begin()); 
    MagnitudeComp.resize(it - MagnitudeComp.begin()); 

我不知道爲什麼的std ::排序不起作用(錯誤很多喜歡:)

/usr/include/c++/5.2.0/bits/predefined_ops.h:71:25: error: cannot convert ‘cv::MatExpr’ to ‘bool’ in return 
     { return __val < *__it; } 

我不知道是否有更好的方法來獲得我想要的(用於保持幅度和方向差異的矩陣)。

我知道我能做到:

CV ::排序(大小,數量,CV_SORT_EVERY_ROW + CV_SORT_ASCENDING);

但我不確定是否可以做std::set_intersection

回答

0

原因std::sort()不起作用的是,默認情況下它使用operator <()作爲值類型,在你的情況下是矩陣。由於comparing兩個矩陣導致第三個矩陣作爲輸出,因此它不能用作排序的謂詞。

所以你需要一個謂詞來命令你的矩陣(在這個說法中「嚴格弱排序」)。我想這個問題是回到你身上的:你如何定義系統中矩陣的排序?一旦你做到了這一點,你需要實現這樣的斷言:

bool matrixOrder(const Mat& m1, const Mat& m2); 

而且使用這樣的:

sort(magnitude.begin(), magnitude.end(), matrixOrder); 
+0

:我可以使用CV ::排序。我的問題是如何找到2個矩陣(幅度[0]和幅度[1])之間的共同元素。 – George

+0

你能更清楚地定義「共同元素」嗎?單個矩陣中的兩個元素是共同的嗎?兩個矩陣中的不同位置上的兩個元素是共同的嗎? –

+0

我想找到2個矩陣(例如幅度[0]和幅度[1])之間的元素(也可能是它們的位置)。但是,正如我在帖子的開始部分所說的那樣。我想要比較例如幅度[1],幅度[2]等。然後,幅度[1]與幅度[2],幅度[3]等等。 – George