《学习OpenCV》第四章练习题4

来源:互联网 发布:iphone7没有4g网络 编辑:程序博客网 时间:2024/06/05 02:11
#include<iostream>#include"highgui.h"#include"cv.h"using namespace std;using namespace cv;CvRect rect;IplImage* test = cvLoadImage("test.jpg");IplImage* test1 = cvCreateImage(cvSize(test->width, test->height), test->depth, test->nChannels);IplImage* test2 = cvCreateImage(cvSize(test->width, test->height), test->depth, test->nChannels);long g_sum[8] = {0}, r_sum[8] = { 0 }, b_sum[8] = { 0 };CvPoint point;int flag= 0;//确定像素所在范围int cmp(int x) {    if (x < 32) return 0;    else if (x < 64)    return 1;    else if (x < 96)    return 2;    else if (x < 128)   return 3;    else if (x < 160)   return 4;    else if (x < 192)   return 5;    else if (x < 224)   return 6;    else return 7;}//计算总数void getCount(CvPoint p){    CvScalar s;    int flagi;    int flagj;    for (int i = 0; i < 8; i++) {        r_sum[i] = 0;        b_sum[i] = 0;        g_sum[i] = 0;    }    for (int i = (point.x<p.x?point.x:p.x); i <= (point.x>p.x ? point.x : p.x); i++) {        for (int j = (point.y<p.y ? point.y : p.y); j<=(point.y>p.y ? point.y : p.y); j++) {            s = cvGet2D(test,i,j);            b_sum[cmp((int)s.val[0])]++;            g_sum[cmp((int)s.val[1])]++;            r_sum[cmp((int)s.val[2])]++;        }    }}//在test2中画出直方图void showCount(IplImage* sum) {    int width = test->width;    int height = test->height;    int widthave = (int)(width / 24);    int max = 1;    for (int i = 0; i < 8; i++) {        if (max < r_sum[i]) max = r_sum[i];        if (max < g_sum[i]) max = g_sum[i];        if (max < b_sum[i]) max = b_sum[i];    }    height *= 0.8;    double h;    int x = 0, y;    for (int i = 0; i < 8; i++) {        h = r_sum[i] / (double)max;        if (h == 0) h = 0.01;        y = (int)((1 - 0.5*h)*height);        cvSetImageROI(sum, cvRect(x, (int)((1.25-h)*height), widthave, (int)(h*height)));        cvSet(sum, CV_RGB(255, 0, 0));        cvResetImageROI(sum);        x += widthave;        h = g_sum[i] / (double)max;        if (h == 0) h = 0.01;        y = (int)((1 - 0.5*h)*height);        cvSetImageROI(sum, cvRect(x, (int)((1.25 - h)*height), widthave, (int)(h*height)));        cvSet(sum, CV_RGB(0, 255, 0));        cvResetImageROI(sum);        x += widthave;        h = b_sum[i] / (double)max;        if (h == 0) h = 0.01;        y = (int)((1 - 0.5*h)*height);        cvSetImageROI(sum, cvRect(x, (int)((1.25 - h)*height), widthave, (int)(h*height)));        cvSet(sum, CV_RGB(0, 0, 255));        cvResetImageROI(sum);        x += widthave;    }}void my_mouse_callback(int event, int x, int y, int flags, void* param) {    CvScalar s;    string str;    switch (event) {    case CV_EVENT_MOUSEMOVE:         if (flag) { cvCopy(test1, test); cvRectangle(test, point, cvPoint(x, y), CV_RGB(0, 255, 0));         cvShowImage("test", test);}        break;    case CV_EVENT_LBUTTONDOWN:  cvCopy(test1, test); point = cvPoint(x, y); flag = 1;        break;    case CV_EVENT_LBUTTONUP: cvRectangle(test, point, cvPoint(x, y), CV_RGB(0, 255, 0)); flag = 0;        getCount(cvPoint(x,y));        cvSetZero(test2);        showCount(test2);        cvShowImage("count", test2);        break;    case CV_EVENT_RBUTTONDOWN:  cvCopy(test1, test);    }}int main(void) {    cvCopy(test, test1);    cvSetZero(test2);    cvNamedWindow("test");    cvNamedWindow("count");    cvSetMouseCallback("test", my_mouse_callback);    while (1) {        cvShowImage("test", test);        cvShowImage("count",test2);        char c = cvWaitKey(30);        if (c == 27)    break;    }    return 0;}

这里写图片描述

原创粉丝点击