Ask Your Question

Revision history [back]

Although @LorenaGdL answered your question, maybe an alternative code for doing what you want will be helpful. a class named ImageCells that i posted it earlier. i hope you will understand how to use it by testing the code below ( it needs some improvements, recently i added some new features )

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

using namespace cv;
using namespace std;

class ImageCells
{
public:
    ImageCells( Mat img );
    ImageCells( int rows, int cols, int width, int height );
    ImageCells( int rows, int cols, Mat img );


    virtual ~ImageCells() {}

    int width() const {return m_cellwidth;}
    int height() const { return m_cellheight;}
    int cols() const { return m_cols;}
    int rows() const { return m_rows;}

    void setCellDimensions( int width, int height       );
    void setCell( int col, int row, Mat img );
    void setImage( Mat img );
    Mat getCell( int col, int row, bool clone = false );
    Mat image;

protected:
    int  m_cellwidth;
    int  m_cellheight;
    int  m_cols;
    int  m_rows;
};

ImageCells::ImageCells( Mat img )
{
    image = img;
    m_cols = 1;
    m_rows = 1;
    m_cellwidth = image.cols;
    m_cellheight = image.rows;
}

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

ImageCells::ImageCells( int rows, int cols, Mat img )
{
    image = img;
    m_cols = cols;
    m_rows = rows;
    m_cellwidth = image.cols / m_cols;
    m_cellheight = image.rows / m_rows;
}


void ImageCells::setCellDimensions( int width, int height )
{
    m_cellwidth = width ;
    m_cellheight = height;
    m_cols = image.cols / m_cellwidth;
    m_rows = image.rows / m_cellheight;
}

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

Mat ImageCells::getCell( int col, int row, bool clone )
{
    Mat roi = image( Rect(col * m_cellwidth, row * m_cellheight, m_cellwidth, m_cellheight) );
    if ( clone ) return roi.clone();
    return roi;
}

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( imread("fruits.jpg") ); // creates a ImageCells class and set its image
    cells.setCellDimensions( 40, 40 );

for(int i=0; i < cells.cols();  i++)
    for(int j =0; j < cells.rows(); j++ )
{
   Mat img = cells.getCell( i, j ); // cells.getCell( i, j, true ) returns a clone of cell
   img = img * 0.8 + Scalar( 0, 0, rand()&127 );
   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;
}

Result Image ( willingly i added random red for each cell )

image description

Although @LorenaGdL answered your question, maybe an alternative code for doing what you want will be helpful. a class named ImageCells that i posted it earlier. i hope you will understand how to use it by testing the code below ( it needs some improvements, recently i added some new features )

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

using namespace cv;
using namespace std;

class ImageCells
{
public:
    ImageCells( Mat img );
    ImageCells( int rows, int cols, int width, int height );
    ImageCells( int rows, int cols, Mat img );


    virtual ~ImageCells() {}

    int width() const {return m_cellwidth;}
    int height() const { return m_cellheight;}
    int cols() const { return m_cols;}
    int rows() const { return m_rows;}

    void setCellDimensions( int width, int height       );
    void setCell( int col, int row, Mat img );
    void setImage( Mat img );
    Mat getCell( int col, int row, bool clone = false );
    Mat image;

protected:
    int  m_cellwidth;
    int  m_cellheight;
    int  m_cols;
    int  m_rows;
};

ImageCells::ImageCells( Mat img )
{
    if( img.empty() )
    {
        image = Mat::zeros( rows * height, cols * width, CV_8UC3);
    }   
    else
    image = img;
     m_cols = 1;
    m_rows = 1;
    m_cellwidth = image.cols;
    m_cellheight = image.rows;
}

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

ImageCells::ImageCells( int rows, int cols, Mat img )
{
    image = img;
    m_cols = cols;
    m_rows = rows;
    m_cellwidth = image.cols / m_cols;
    m_cellheight = image.rows / m_rows;
}


void ImageCells::setCellDimensions( int width, int height )
{
    m_cellwidth = width ;
    m_cellheight = height;
    m_cols = image.cols / m_cellwidth;
    m_rows = image.rows / m_cellheight;
}

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

Mat ImageCells::getCell( int col, int row, bool clone )
{
    Mat roi = image( Rect(col * m_cellwidth, row * m_cellheight, m_cellwidth, m_cellheight) );
    if ( clone ) return roi.clone();
    return roi;
}

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( imread("fruits.jpg") ); // creates a ImageCells class and set its image
    cells.setCellDimensions( 40, 40 );

for(int i=0; i < cells.cols();  i++)
    for(int j =0; j < cells.rows(); j++ )
{
   Mat img = cells.getCell( i, j ); // cells.getCell( i, j, true ) returns a clone of cell
   img = img * 0.8 + Scalar( 0, 0, rand()&127 );
   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;
}

Result Image ( willingly i added random red for each cell )

image description

Although @LorenaGdL answered your question, maybe an alternative code for doing what you want will be helpful. a class named ImageCells that i posted it earlier. i hope you will understand how to use it by testing the code below ( it needs some improvements, recently i added some new features )

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

using namespace cv;
using namespace std;

class ImageCells
{
public:
    ImageCells( Mat img );
    ImageCells( int rows, int cols, int width, int height );
    ImageCells( int rows, int cols, Mat img );


    virtual ~ImageCells() {}

    int width() const {return m_cellwidth;}
    int height() const { return m_cellheight;}
    int cols() const { return m_cols;}
    int rows() const { return m_rows;}

    void setCellDimensions( int width, int height       );
    void setCell( int col, int row, Mat img );
    void setImage( Mat img );
    Mat getCell( int col, int row, bool clone = false );
    Mat image;

protected:
    int  m_cellwidth;
    int  m_cellheight;
    int  m_cols;
    int  m_rows;
};

ImageCells::ImageCells( Mat img )
{
    if( img.empty() )
    {
        image = Mat::zeros( rows * height, cols * width, CV_8UC3);
256, 256, CV_8UC3 );
    }   
    else
    image = img;

    m_cols = 1;
    m_rows = 1;
    m_cellwidth = image.cols;
    m_cellheight = image.rows;
}

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

ImageCells::ImageCells( int rows, int cols, Mat img )
{
    image = img;
    m_cols = cols;
    m_rows = rows;
    m_cellwidth = image.cols / m_cols;
    m_cellheight = image.rows / m_rows;
}


void ImageCells::setCellDimensions( int width, int height )
{
    m_cellwidth = width ;
    m_cellheight = height;
    m_cols = image.cols / m_cellwidth;
    m_rows = image.rows / m_cellheight;
}

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

Mat ImageCells::getCell( int col, int row, bool clone )
{
    Mat roi = image( Rect(col * m_cellwidth, row * m_cellheight, m_cellwidth, m_cellheight) );
    if ( clone ) return roi.clone();
    return roi;
}

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( imread("fruits.jpg") ); // creates a ImageCells class and set its image
    cells.setCellDimensions( 40, 40 );

for(int i=0; i < cells.cols();  i++)
    for(int j =0; j < cells.rows(); j++ )
{
   Mat img = cells.getCell( i, j ); // cells.getCell( i, j, true ) returns a clone of cell
   img = img * 0.8 + Scalar( 0, 0, rand()&127 );
   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;
}

Result Image ( willingly i added random red for each cell )

image description