2016-12-18 28 views
-1

我在txt文件2個的灰度圖像,其中之一是主圖像的較小塊。我已經將圖像讀入兩個不同的2d向量矩陣。C++圖像處理數據塊到陣列/指針

行和圖像的列是:

主:M = 768 N = 1024

SubImg:R = 49 C = 36

INT R = 49; int C = 36; //子圖像行/列

INT M = 768; int N = 1024; //主圖像行/列

我已經通過寬度爲49和高度爲36的塊遍歷主圖像,並且我想將每個塊放入一個數組,因此我可以將該數組與Sub圖像進行比較(使用最近鄰搜索),以查看哪個塊具有最接近結果提供給子圖像。

這是主圖像的循環代碼:一氣呵成

for (double bx = 0; bx < M; bx += R) 
    for (double by = 0; by < N; by += C) 
    { 
     for (int x = 0; ((x < R) && ((bx + x) < M)); ++x) 
     { 
      for (int y = 0; ((y < C) && ((by + y) < N)); ++y) 
      { 
       if ((bx + x) >= M) 
       { 
        std::cout << (bx + x) << (by + y) << " "; 

       } 
       cout << MainIMG_2DVector[bx + x][by + y] << " "; 
      } 
     } 
     cout << "\n\n\n" << endl; 
    } 

這個循環顯示所有的塊。我遇到的問題是我不知道如何將每個塊放入數組,因此我可以比較數據。 也就是它更好地使用指針,而不是一個數組來做到這一點?

感謝

回答

0

我不知道你到底想怎麼塊比較,但至於存儲塊,你可以做一個簡單的塊對象是這樣的:

const double R = 49, C = 36; 

// Block object (takes in your image 2D vector and x/y coordinates) 
class Block { 
public: 
    Block(std::vector<std::vector<int>> *img_vector, int bx, int by); 
    int compare(const Block &block) const; 

private: 
    std::vector<std::vector<int>> *img_vector; 
    int bx, by; 
}; 

Block::Block(std::vector<std::vector<int>> *img_vector, int bx, int by) { 
    this->img_vector = img_vector; 
    this->bx = bx; 
    this->by = by; 
} 

// Compare any two blocks 
int Block::compare(const Block &block) const { 
    for (int x = bx; x < bx + R; x++) { 
     for (int y = by; y < by + C; y++) { 
      // Do some comparing 
      std::cout << "Compare " << (*img_vector)[x][y] 
       << " with " << (*block.img_vector)[x][y] << std::endl; 
     } 
    } 
    return 0; // Return some value that indicates how they compare 
} 

然後圖像塊添加到一個向量:

// Add main image blocks to vector 
std::vector<Block> main_img_blocks; 
for (double bx = 0; bx < M; bx += R) { 
    for (double by = 0; by < N; by += C) 
     main_img_blocks.push_back(Block(&MainIMG_2DVector, bx, by)); 
} 

// Do the same for sub image blocks... 

// Invoke the compare function between 2 blocks at a time 
int comp_value = main_img_blocks[0].compare(main_img_blocks[1]); 

希望幫助:)

+0

你好,謝謝你的回答,但如何我可以申報Block嗎?我繼續得到這個錯誤信息(沒有默認構造函數存在類「塊」。對不起,我是新來的C++。乾杯。 – QWERTY

+0

有幾種不同的方法可以做到這一點,通常類定義在自己的頭文件和在一個相應的.cpp文件中實現。一個通用的C++對象教程是[here](http://www.cplusplus.com/doc/tutorial/classes/)幫助你入門。 –