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)