2013-12-13 16 views
0

假設我有一個名爲image的數組,它是一個長的連續的「大小」元素數組 - 如下文所定義的。 我想裁剪這個數組(然後得到一個子矩陣),然後,通過修改子數組,我將自動修改源圖像(引用)。C++裁剪一個連續的二維數組

請注意裁剪操作將返回一個非連續陣列。這實際上是問題所在。

有沒有辦法在C++中優雅地做到這一點?

順便說一句,我用OpenCV的,升壓,等等

感謝您的幫助我不感興趣

template <class Type> class Image 
{ 
private: 
    Type *image; 

public: 

    int rows; 
    int cols; 
    int size; 

    Image(int rows, int cols): rows(rows), cols(cols) 
    { 
     image = new Type[size = rows*cols]; 
    } 
    ~Image() 
    { 
     delete [] image; 
    } 
    Type & at(int i, int j) 
    { 
     return image[cols*i + j]; 
    } 
    void print() 
    { 
     for(int i = 0; i < rows; ++i) 
     { 
       for(int j = 0; j < cols; ++j) 
        cout << at(i,j) << " "; 
       cout << endl; 
     } 
     cout << endl; 

    } 

}; 
+0

如果不採取原始連續數據的子集,你能詳細說明「裁剪」嗎? –

回答

3

可以創建類CroppedImage保存引用或指針到原始圖像和偏移,並提供其自己的方法,其中添加的偏移量,然後調用原始圖像的方法:

template <class Type> class CroppedImage 
{ 
    private: 
     Image<Type> *original; 
     int offsetX; 
     int offsetY; 
    public: 
     int rows; 
     int cols; 
     int size; 
     CroppedImage(Image<Type> *orig, int offX, int offY, int width, int height) 
     { 
      original = orig; 
      offsetX = offX; 
      offsetY = offY; 
      rows = height; 
      cols = width; 
      size = rows*cols; 
     } 
     ~CroppedImage(){} 
     Type & at(int i, int j) 
     { 

      return original->at(i+offsetX, j+offsetY); 
     } 
     void print() 
     { 
      for(int i = 0; i < rows; ++i) 
      { 
        for(int j = 0; j < cols; ++j) 
         cout << at(i,j) << " "; 
        cout << endl; 
      } 
      cout << endl; 
     } 

} 

我沒有測試過,可能會有一些錯別字和其他錯誤。 如果你不想創建一個新的類,你可以將代碼合併到你的Image類中。

+0

不錯,謝謝... – PhonoDots