/** * projections.c * OpenCV Introduction: Demo 3 * David Johnston, 2013-01-29 */ #include #include #include "cv.h" #include "highgui.h" void printHistLine(int, int); void display(IplImage* img) { cvShowImage("DEBUG", img); cvWaitKey(0); } /** * A similar solution could be accomplished more simply via cvReduce(). */ int main(const int argc, const char* argv[]) { int i; IplImage* src = cvLoadImage("inputs/test.png", CV_LOAD_IMAGE_GRAYSCALE); int numRows = src->height; int numCols = src->width; CvScalar avg = {0}; cvThreshold(src, src, 160, 0xFF, CV_THRESH_BINARY_INV); cvShowImage("Filtered", src); /* * Allocate the CvMat struct. We do not yet initialize its fields, and * we do not allocate the matrix data. * * This struct will be used as header to point to some sliver of the data in 'src'. */ CvMat vec; /** * Find the average of each row. * * For each row in 'src', call cvGetRow() to make 'vec' to be an * interface to the i-th row of pixels of the 'src' image data. */ puts("*** ROW AVERAGES ***"); for (i = 0; i < numRows; i++) { cvGetRow(src, &vec, i); avg = cvAvg(&vec, NULL); printHistLine(i, (int) avg.val[0]); } /** * Find the x-axis bounds of the staff lines using vertical projection. */ puts("\n*** COLUMN AVERAGES ***"); for (i = 0; i < numCols; i++) { cvGetCol(src, &vec, i); avg = cvAvg(&vec, NULL); printHistLine(i, (int) avg.val[0]); } cvWaitKey(0); } void printHistLine(int lineNum, int val) { int numStars = val / 8; printf("%3d: ", lineNum); for (int i = 0; i < numStars; i++) putchar('*'); putchar('\n'); }