opencv第四章的第三题:
#include "opencv_4_3.h"
#include "function.h"
opencv_4_3::opencv_4_3()
{
img_all = cvLoadImage("..//..//dog.bmp");
}
opencv_4_3::~opencv_4_3()
{
}
void opencv_4_3::ans_1() {
cvNamedWindow("dog", CV_WINDOW_AUTOSIZE);
cvSetMouseCallback("dog", my_mouse_callback, (void *)img_all);
img_c = cvCloneImage(img_all);
while (1) {
cvCopy(img_all, img_c);
if (temp)drawing_rect(img_c, &rect);
cvShowImage("dog", img_c);
cvWaitKey(33);
}
cvDestroyWindow("dog");
}
下面是function.h
#pragma once
#include
#include
using namespace std;
#include
using namespace cv;
CvRect rect=cvRect(-1,-1,0,0);
bool temp=false;
bool again= false;
IplImage * img_c;
IplImage * img_all;
void drawing_rect(IplImage * img, CvRect * rect) {
cvRectangle(
img,
CvPoint(rect->x, rect->y),
CvPoint(rect->x + rect->width, rect->y + rect->height),
CvScalar(0, 0, 255)
);
double alpha = 1.2;
double beta = 30;
uchar *ptr;
for (int i = rect->x; i < rect->x + rect->width; i++) {
for (int j = rect->y; j < rect->y + rect->height; j++) {
ptr = cvPtr2D(img, j, i);
ptr[0] = ptr[0] * alpha + beta;
ptr[1] = ptr[1] * alpha + beta;
ptr[2] = ptr[2] * alpha + beta;
}
}
}
CvSize image_size = cvSize(256, 300);
IplImage * draw_single_hist(IplImage * img, CvScalar value) {
int size = 256;
float range[] = { 0,255 };
float *ranges[] = { range };
CvHistogram * hist = cvCreateHist(1, &size, CV_HIST_ARRAY, ranges, 1);
cvCalcHist(&img, hist, 0, NULL);
float max_value = 0;
cvGetMinMaxHistValue(hist, NULL, &max_value, NULL, NULL);
IplImage *dst = cvCreateImage(image_size, IPL_DEPTH_8U, 3);
cvSet(dst, cvScalarAll(255));
double bin_width = (double)dst->width / 256;
double bin_unith = (double)dst->height / max_value;
for (int i = 0; i < 256; i++) {
CvPoint p0 = cvPoint(i + bin_width, dst->height);
CvPoint p1 = cvPoint((i + 1)*bin_width, dst->height - cvGetReal1D
(hist->bins, i)*bin_unith);
cvRectangle(dst, p0, p1, value, -1, 8, 0);
}
return dst;
}
void draw_hist(IplImage * img, CvRect rect) {
cvSetImageROI(img, rect);
IplImage *src_rect = cvCreateImage(cvSize(rect.width, rect.height)
, img->depth, img->nChannels);
cvCopy(img, src_rect);
cvResetImageROI(img);
IplImage * r_img = cvCreateImage(cvGetSize(src_rect),
src_rect->depth, 1);
IplImage * b_img = cvCreateImage(cvGetSize(src_rect),
src_rect->depth, 1);
IplImage * g_img = cvCreateImage(cvGetSize(src_rect),
src_rect->depth, 1);
cvSplit(src_rect, b_img, g_img, r_img, NULL);
IplImage * r_hist = draw_single_hist(r_img, cvScalar(0, 0, 255));
IplImage * b_hist = draw_single_hist(r_img, cvScalar(255, 0, 0));
IplImage * g_hist = draw_single_hist(r_img, cvScalar(0, 255, 0));
IplImage * hist = cvCreateImage(CvSize(r_hist->width * 3, r_hist->height),
r_hist->depth, r_hist->nChannels);
cvZero(hist);
cvSetImageROI(hist, CvRect(0, 0, r_hist->width, r_hist->height));
cvCopy(r_hist, hist);
cvSetImageROI(hist, CvRect(r_hist->width, 0, r_hist->width, r_hist->height));
cvCopy(g_hist, hist);
cvSetImageROI(hist, CvRect(r_hist->width * 2, 0, r_hist->width, r_hist->height));
cvCopy(b_hist, hist);
cvResetImageROI(hist);
cvNamedWindow("hist");
cvShowImage("hist", hist);
cvWaitKey(0);
cvDestroyWindow("hist");
}
void my_mouse_callback(int event, int x, int y, int flags, void* param) {
IplImage * img = (IplImage *)param;
switch (event)
{
case CV_EVENT_RBUTTONDOWN:
{
if(again ==false)
again=true;
break;
}
case CV_EVENT_LBUTTONDOWN: {
cout << "asdL:" << again << endl;
rect = CvRect(x, y, 0, 0);
temp = true;
break;
}
case CV_EVENT_RBUTTONUP: {
draw_hist(img_all,rect);
break;
}
case CV_EVENT_MOUSEMOVE: {
if (temp) {
rect.width = x - rect.x;
rect.height = y- rect.y;
}
break;
}
case CV_EVENT_LBUTTONUP: {
if (temp) {
temp = false;
if (rect.width < 0) {
rect.x += rect.width;
rect.width *= -1;
}
if (rect.height < 0) {
rect.y += rect.height;
rect.height *= -1;
}
}
drawing_rect(img,& rect);
if (!again) {
cvCopy(img_c, img_all);
}
break;
}
}
if (again == true) {
img_all = cvLoadImage("..//..//dog.bmp");
again = false;
}
}