Ask Your Question
3

Divide picture into blocks

asked 2013-02-18 09:12:03 -0600

Milanista gravatar image

updated 2020-12-05 12:00:46 -0600

Hello. Could somebody help me with this: After I load an image I want to separate it into 30x30 pixel blocks then apply wiener filter on it. Now I'm only asking for the separation, how should I do it?

edit retag flag offensive close merge delete

4 answers

Sort by ยป oldest newest most voted
5

answered 2013-02-18 13:48:46 -0600

updated 2013-02-18 13:53:24 -0600

I have written a class for blockwise process.

BhBlocks.h

class BhBlocks
{
  private:
      int colCount , rowCount;
      float colSize, rowSize;
      CvRect rct;
      void compute();
  public:
      vector<vector<CvRect>> rects;
      void setColsRowsCount(int colCount,int rowCount);
      void setColRowSize(float colSize,float rowSize);
      void setRect(IplImage* srcImage);
      void setRect(const CvRect& srcRect);
      int getColCount();
      int getRowCount();
      float getColSize();
      float getRowSize();

      BhBlocks(const IplImage* srcImage,float colSize=-1,float rowSize=-1,int colCount=-1,int rowCount=-1);
      BhBlocks(const Mat& srcMat,float colSize=-1,float rowSize=-1,int colCount=-1,int rowCount=-1);
      BhBlocks(const CvRect& srcImage,float colSize=-1,float rowSize=-1,int colCount=-1,int rowCount=-1);


      void drawBlocks(IplImage* srcImage,CvScalar color,int thickness);
      void displayBlocks(char* title,IplImage* srcImage,CvScalar color,int thickness);
};

BhBlocks.cpp

void BhBlocks::setColsRowsCount(int colCount,int rowCount)
{
    this->colSize = -1;
    this->rowSize = -1;
    this->colCount = colCount;
    this->rowCount = rowCount;

}

void BhBlocks::setColRowSize(float colSize,float rowSize)
{
    this->colCount = -1;
    this->rowCount = -1;
    this->colSize = colSize;
    this->rowCount = rowCount;
    compute();

}

void BhBlocks::setRect(IplImage* srcImage)
{
    rct = cvGetImageROI(srcImage);
    compute();
}

void BhBlocks::setRect(const CvRect& srcRect)
{
    rct = srcRect;
    compute();
}

int BhBlocks::getColCount()
{
    return colCount;
}

int BhBlocks::getRowCount()
{
    return rowCount;
}

float BhBlocks::getColSize()
{
    return colSize;
}

float BhBlocks::getRowSize()
{
    return rowSize;
}
BhBlocks::BhBlocks(const IplImage* srcImage,float colSize,float rowSize,int colCount,int rowCount)
{
    rct = cvGetImageROI(srcImage);
    this->colCount = colCount;
    this->rowCount = rowCount;
    if (colSize ==-1 && colSize ==-1)
        return;
    if (rowSize == -1)
        rowSize = colSize;

    this->colSize = colSize;
    this->rowSize = rowSize;
    compute();

}

BhBlocks::BhBlocks(const Mat& srcMat,float colSize,float rowSize,int colCount,int rowCount)
{
    rct.x = 0;
    rct.y = 0;
    rct.width = srcMat.cols;
    rct.height = srcMat.rows;

    this->colCount = colCount;
    this->rowCount = rowCount;
    if (colSize == -1 && colSize == -1 && colCount ==-1 && rowCount == -1)
        return ;
    if ( rowSize == -1)
        rowSize = colSize;

    this->colSize = colSize;
    this->rowSize = rowSize;
    compute();
}

BhBlocks::BhBlocks(const CvRect& srcRect,float colSize,float rowSize,int colCount,int rowCount)
{
    rct = srcRect;
    this->colCount = colCount;
    this->rowCount = rowCount;
    if (colSize ==-1 && colSize ==-1)
        return;
    if (rowSize == -1)
        rowSize = colSize;

    this->colSize = colSize;
    this->rowSize = rowSize;
    compute();
}



void BhBlocks::compute()
{


    float cSize;
    float rSize;
    if (colSize != -1 && rowSize != -1)
    {
        colCount = int(rct.width / colSize);
        rowCount = int(rct.height / rowSize);

        cSize = (float)rct.width / colCount;
        rSize = (float)rct.height / rowCount;
    }
    else if (colCount != -1 && rowCount != -1)
    {
        colSize = (float)rct.width / colCount;
        rowSize = (float)rct.height / rowCount;
        cSize = colSize;
        rSize = rowSize;

    }
    else return;
    rects.clear();
    rects.resize(rowCount);
    for (int i=0; i < rowCount;i++)
        rects[i].resize(colCount);

    float sumCol = 0;

    for (int j=0; j < colCount;j++)
    {
        float newSumCol = sumCol + cSize;
        float curColSize = int( newSumCol - int(sumCol) );
        for (int i=0; i < rowCount;i++)
        {
            rects[i][j].width = curColSize;
            rects[i][j].x = sumCol;
        }
        sumCol = newSumCol;

    }

    float sumRow = 0;
    for (int i=0; i < rowCount;i++)
    {
        float newSumRow = sumRow + rSize;
        float curRowSize = int( newSumRow - int(sumRow) );
        for (int j=0; j < colCount;j++)
        {
            rects[i][j].height = curRowSize;
            rects[i][j].y = sumRow;
        }

       sumRow = newSumRow;
    }

}
void BhBlocks::drawBlocks(IplImage* srcImage,CvScalar color,int thickness)
{
    for (int i=0; i < rowCount;i ...
(more)
edit flag offensive delete link more
3

answered 2013-03-17 13:42:52 -0600

The code on the link given is still based on the old C - style API. In order to work fluently and much more readable, you could use the C++ interface easily for this.

// Original image
Mat inputImage = imread( 'location.jpg' );
// Use region of interest approach
Mat outputImage = inputImage( Rect(x, y, width, heigth) );

This can be easily placed into a loop for optimal going through the image.

edit flag offensive delete link more

Comments

When you create the a new Mat by borrowing elements from another Mat, the matrix is not continuous. This will not pass isContinuous() tests in various methods. isContinuous()

caganarslan gravatar imagecaganarslan ( 2017-10-09 10:10:04 -0600 )edit

Thats true, but in most cases it is enough. If you want to avoid this, then use Mat outputImage = inputImage( Rect(x, y, width, heigth) ).clone(); and copy back the end result.

StevenPuttemans gravatar imageStevenPuttemans ( 2017-10-10 06:13:13 -0600 )edit
0

answered 2013-03-17 13:08:07 -0600

Milanista gravatar image

Thank you, I managed to make it :)

edit flag offensive delete link more
0

answered 2013-02-18 09:25:49 -0600

otterb gravatar image
edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2013-02-18 09:12:03 -0600

Seen: 12,054 times

Last updated: Mar 17 '13