基于opencv的选择矩形区域并高亮显示,,并在其一个独立的表格画其RGB的像素数量。

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<cv.h>
#include<highgui.hpp>
using namespace std;
#include<iostream>
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;
	 }
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值