#include "BhBlocks.h" #include "opencv2/core/core.hpp" #include "opencv2/imgproc/imgproc.hpp" #include "opencv2/highgui/highgui.hpp" using namespace cv; using namespace std; 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(Mat srcImage, Scalar color, int thickness) { for (int i = 0; i < rowCount; i++) for (int j = 0; j < colCount; j++) rectangle(srcImage, rects[i][j], color); } vector BhBlocks::getRects() { vector result; for (int i = 0; i < rowCount; i++) for (int j = 0; j < colCount; j++) result.push_back(rects[i][j]); return result; } void BhBlocks::displayBlocks(char* title, IplImage* srcImage, CvScalar color, int thickness) { //IplImage* viewImg = cvCloneImage(srcImage); //drawBlocks(viewImg, color, thickness); //cvShowImage(title, viewImg); //cvWaitKey(0); //cvDestroyWindow(title); //cvReleaseImage(&viewImg); }