opencv画彩色矩形.
Posted Montauk
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了opencv画彩色矩形.相关的知识,希望对你有一定的参考价值。
#include <opencv2/opencv.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace cv;
#define WINDOW_NAME "line mix"
const int g_nMaxAlphaValue = 100;
int g_nAlphaValueSlider;
double g_dAlphaValue;
double g_dBetaValue;
void on_MouseHandle(int event, int x, int y, int flags, void* parma);
void DrawRectagle(cv::Mat & img, cv::Rect box);
void ShowHelpText();
Rect g_rectangle;
bool g_bDrawingBox = false;
RNG g_rng(12345);
Mat g_srcImage1;
Mat g_srcImage2;
Mat g_destImage;
void on_Trackbar(int, void*) {
g_dAlphaValue = (double)g_nAlphaValueSlider / g_nMaxAlphaValue;
g_dBetaValue = (1.0 - g_dAlphaValue);
//addWeighted(g_srcImage1, g_dAlphaValue, g_srcImage2, g_dBetaValue, 0.0, g_destImage);
addWeighted(g_srcImage2, g_dAlphaValue, g_srcImage1, g_dBetaValue, 0.0, g_destImage);
imshow(WINDOW_NAME, g_destImage);
}
int main(int argc, char**argv) {
g_rectangle = Rect(-1, -1, 0, 0);
Mat srcImage(600, 800, CV_8UC3), tempImage;
srcImage.copyTo(tempImage);
g_rectangle = Rect(-1, -1, 0, 0);
srcImage = Scalar::all(0);
namedWindow(WINDOW_NAME);
setMouseCallback(WINDOW_NAME, on_MouseHandle, (void*)&srcImage);
while (1) {
srcImage.copyTo(tempImage);
if (g_bDrawingBox)DrawRectagle(tempImage, g_rectangle);
imshow(WINDOW_NAME, tempImage);
if (waitKey(10) == 27)break;
}
return 0;
}
void on_MouseHandle(int event, int x, int y, int flags, void * param) {
Mat&image = *(cv::Mat*)param;
switch (event) {
case EVENT_MOUSEMOVE:
{
if (g_bDrawingBox) {
g_rectangle.width = x - g_rectangle.x;
g_rectangle.height = y - g_rectangle.y;
}
}
break;
case EVENT_LBUTTONDOWN:
{
g_bDrawingBox = true;
g_rectangle = Rect(x, y, 0, 0);
}
break;
case EVENT_LBUTTONUP:
{
g_bDrawingBox = false;
if (g_rectangle.width < 0) {
g_rectangle.x += g_rectangle.width;
g_rectangle.width *= -1;
}
if (g_rectangle.height < 0) {
g_rectangle.y += g_rectangle.height;
g_rectangle.height *= -1;
}
DrawRectagle(image, g_rectangle);
}
break;
}
}
void DrawRectagle(cv::Mat & img, cv::Rect box) {
rectangle(img, box.tl(), box.br(), Scalar(g_rng.uniform(0, 255), g_rng.uniform(0, 255), g_rng.uniform(0, 255)));
}
以上是关于opencv画彩色矩形.的主要内容,如果未能解决你的问题,请参考以下文章