Ask Your Question

Revision history [back]

although there are many simple solution,i tried to write a class to solve your problem. i hope you like it.

i tried to explain how to use it by comments on the code.

#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>

using namespace cv;
using namespace std;

class ImageCells
{
public:
    ImageCells(int rows, int cols, int width, int height);
    virtual ~ImageCells() {}

    int width() const {return m_width;}
    int height() const { return m_height;}
    int cols() const { return m_cols;}
    int rows() const { return m_rows;}

    void setCell( int col, int row, Mat img );
    void setImage( Mat img );
    Mat getCell( int col, int row );
    Mat image;

protected:
    int  m_width;
    int  m_height;
    int  m_cols;
    int  m_rows;
};

ImageCells::ImageCells( int rows, int cols, int width, int height)
{
    image = Mat::zeros( rows * height, cols * width, CV_8UC3);
    m_width = width;
    m_height = height;
    m_cols = cols;
    m_rows = rows;
}

void ImageCells::setCell( int col, int row, Mat img )
{
    if(img.cols == m_width & img.rows == m_height)
    {
        Mat roi = image( Rect(col * m_width, row * m_height, m_width, m_height) );
        img.copyTo(roi);
    }
}

Mat ImageCells::getCell( int col, int row )
{
    Mat roi = image( Rect(col * m_width, row * m_height, m_width, m_height) );
    return roi.clone();
}

void ImageCells::setImage( Mat img )
{
    img.copyTo(image);
}

int main( int argc, char** argv )
{
    ImageCells cells(4,3,70,90); // it creates a ImageCells class having 4 rows and 3 cols, cell witdh = 70 cell height = 90 you can change these values according your needs

    Mat img = Mat( 90, 70, CV_8UC3,Scalar(0,0,255)); // a test mat to use with cells.setCell important note is : img witdh&height must be same to cell witdh&height

for(int i=0; i < cells.cols(); i++)
    for(int j =0; j < cells.rows(); j++ )
{
   cells.setCell(i,j,img); // here you see how to use  setCell
   randu(img,30*i,160*j); // to show purpose changes img
   imshow("cells.image",cells.image);
   waitKey();
}

for(int i=0; i < cells.cols(); i++)
    for(int j =0; j < cells.rows(); j++ )
{
    imshow("cells",cells.getCell(i,j)); // here you see how to use  getCell
    waitKey();
}
    return 0;
}

although there are many simple solution,i tried to write a simple class to solve your problem. i hope you like it.

i tried to explain how to use it by comments on the code.

#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>

using namespace cv;
using namespace std;

class ImageCells
{
public:
    ImageCells(int rows, int cols, int width, int height);
    virtual ~ImageCells() {}

    int width() cellwidth() const {return m_width;}
    int height() cellheight() const { return m_height;}
    int cols() const { return m_cols;}
    int rows() const { return m_rows;}

    void setCell( int col, int row, Mat img );
    void setImage( Mat img );
    Mat getCell( int col, int row );
    Mat image;

protected:
    int  m_width;
    int  m_height;
    int  m_cols;
    int  m_rows;
};

ImageCells::ImageCells( int rows, int cols, int width, int height)
{
    image = Mat::zeros( rows * height, cols * width, CV_8UC3);
    m_width = width;
    m_height = height;
    m_cols = cols;
    m_rows = rows;
}

void ImageCells::setCell( int col, int row, Mat img )
{
    if(img.cols == m_width & img.rows == m_height)
    {
        Mat roi = image( Rect(col * m_width, row * m_height, m_width, m_height) );
        img.copyTo(roi);
    }
}

Mat ImageCells::getCell( int col, int row )
{
    Mat roi = image( Rect(col * m_width, row * m_height, m_width, m_height) );
    return roi.clone();
}

void ImageCells::setImage( Mat img )
{
    if(img.cols >= image.cols & img.rows >= image.rows)
    img.copyTo(image);
}

int main( int argc, char** argv )
{
    ImageCells cells(4,3,70,90); // it creates a ImageCells class having 4 rows and 3 cols, cell witdh = 70 cell height = 90 you can change these values according your needs

    Mat img = Mat( 90, 70, CV_8UC3,Scalar(0,0,255)); // a test mat to use with cells.setCell important note is : img witdh&height must be same to cell witdh&height

for(int i=0; i < cells.cols(); i++)
    for(int j =0; j < cells.rows(); j++ )
{
   cells.setCell(i,j,img); // here you see how to use  setCell
   randu(img,30*i,160*j); // to show purpose changes img
   imshow("cells.image",cells.image);
   waitKey();
}

for(int i=0; i < cells.cols(); i++)
    for(int j =0; j < cells.rows(); j++ )
{
    imshow("cells",cells.getCell(i,j)); // here you see how to use  getCell
    waitKey();
}
    return 0;
}