using OpenCvSharp;
using System;
namespace ConsoleApp
{
internal class Program
{
public static Mat img = new Mat();
// 矩形和圆形模式切换
public static bool mode = false;
public static bool drawing = false;
public static int ix = -1, iy = -1;
public static void draw_circle(MouseEventTypes @event, int x, int y, MouseEventFlags flags, IntPtr userData)
{
int r = Cv2.GetTrackbarPos("R", "image");
int g = Cv2.GetTrackbarPos("G", "image");
int b = Cv2.GetTrackbarPos("B", "image");
// 当按下左键是返回起始位置坐标
if (@event == MouseEventTypes.LButtonDown)
{
drawing = true;
ix = x;
iy = y;
}
if (@event == MouseEventTypes.MouseMove)
{
if(drawing)
{
if (mode)
{
Cv2.Rectangle(img, new Point(ix, iy), new Point(x, y), new Scalar(b, g, r), -1);
}
else
{
// 绘制圆圈,小圆点连在一起就成了线,3 代表了笔画的粗细
Cv2.Circle(img, new Point(x, y), 3, new Scalar(b, g, r), -1);
//下面注释掉的代码是起始点为圆心,起点到终点为半径的
//int r = (int)(Math.Sqrt((x - ix) * (x - ix) + (y - iy) * (y - iy)));
//Cv2.Circle(img, new Point(x, y), r, new Scalar(b, g, r), -1);
}
}
}
// 当鼠标松开停止绘画。
if (@event == MouseEventTypes.LButtonUp)
{
drawing = false;
}
}
public static void Main(string[] args)
{
img = new Mat(new Size(512, 512), MatType.CV_8UC3, new Scalar(0, 0, 0));
Cv2.NamedWindow("image", WindowFlags.AutoSize);
Cv2.CreateTrackbar("R", "image", 255, null);
Cv2.CreateTrackbar("G", "image", 255, null);
Cv2.CreateTrackbar("B", "image", 255, null);
Cv2.SetMouseCallback("image", new MouseCallback(draw_circle));
while (true)
{
Cv2.ImShow("image", img);
if (Cv2.WaitKey(1) == 'm')
{
mode = !mode;
}
}
}
}
}
demo2:调节画布颜色,演示代码如下:
using OpenCvSharp;
namespace ConsoleApp
{
internal class Program
{
public static void Main(string[] args)
{
string swit = "0 : OFF \n1 : ON";
Mat img = new Mat(new Size(512, 300), MatType.CV_8UC3, new Scalar(0, 0, 0));
Cv2.NamedWindow("image", WindowFlags.AutoSize);
Cv2.CreateTrackbar("R", "image", 255, null);
Cv2.CreateTrackbar("G", "image", 255, null);
Cv2.CreateTrackbar("B", "image", 255, null);
Cv2.CreateTrackbar(swit, "image", 1, null);
while (true)
{
int r = Cv2.GetTrackbarPos("R", "image");
int g = Cv2.GetTrackbarPos("G", "image");
int b = Cv2.GetTrackbarPos("B", "image");
int s = Cv2.GetTrackbarPos(swit, "image");
if (s == 0)
img = new Mat(new Size(512, 300), MatType.CV_8UC3, new Scalar(0, 0, 0));
else
img = new Mat(new Size(512, 300), MatType.CV_8UC3, new Scalar(b, g, r));
Cv2.ImShow("image", img);
Cv2.WaitKey(1);
img.Dispose();
}
}
}
}
C++版本运行代码如下:
#include "opencv2/core/core.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <iostream>
using namespace cv;
using namespace std;
#define DEMOID 0
// 绘制不同颜色的圆和矩形演示代码如下
#if DEMOID == 0
// 当鼠标按下时变为 True
bool drawing = false;
// 矩形和圆形模式切换
bool mode = false;
// 如果 mode 为 true 绘制矩形。按下'm' 变成绘制曲线。 mode=True
int ix = -1, iy = -1;
void on_MouseHandle(int event, int x, int y, int flags, void* param)
{
int r = getTrackbarPos("R", "image");
int g = getTrackbarPos("G", "image");
int b = getTrackbarPos("B", "image");
Mat& img = *(cv::Mat*)param;
if (event == EVENT_LBUTTONDOWN)
{
drawing = true;
ix = x;
iy = y;
}
if (event == EVENT_MOUSEMOVE)
{
if (drawing)
{
if (mode)
{
rectangle(img, Point(ix, iy), Point(x, y), Scalar(b, g, r), -1);
}
else
{
// 绘制圆圈,小圆点连在一起就成了线,3 代表了笔画的粗细
circle(img, Point(x, y), 3, Scalar(b, g, r), -1);
//下面注释掉的代码是起始点为圆心,起点到终点为半径的
//int r = (int)(Math.Sqrt((x - ix) * (x - ix) + (y - iy) * (y - iy)));
//circle(img, new Point(x, y), r, Scalar(b, g, r), -1);
}
}
}
// 当鼠标松开停止绘画。
if (event == EVENT_LBUTTONUP)
{
drawing = false;
}
}
void main()
{
Mat img(512, 512, CV_8UC3);
namedWindow("image");
createTrackbar("R", "image", NULL, 255);
createTrackbar("G", "image", NULL, 255);
createTrackbar("B", "image", NULL, 255);
setMouseCallback("image", on_MouseHandle, (void*)&img);
while (true)
{
imshow("image", img);
if (waitKey(1) == 'm')
{
mode = !mode;
}
}
}
#endif
// 调节画布颜色
#if DEMOID == 1
void main()
{
string swit = "0 : OFF \n1 : ON";
Mat img(300, 512, CV_8UC3);
namedWindow("image");
createTrackbar("R", "image", NULL, 255);
createTrackbar("G", "image", NULL, 255);
createTrackbar("B", "image", NULL, 255);
createTrackbar(swit, "image", NULL, 1);
while (true)
{
int r = getTrackbarPos("R", "image");
int g = getTrackbarPos("G", "image");
int b = getTrackbarPos("B", "image");
int s = getTrackbarPos(swit, "image");
if (s == 0)
img = Scalar::all(0);
else
img = Scalar(b, g, r);
imshow("image", img);
waitKey(1);
}
}
#endif
Python版本运行效果及代码如下:
demo1:绘制不同颜色的圆和矩形演示效果和代码如下:
# -*- coding: utf-8 -*-
import cv2
import numpy as np
def nothing(x):
pass
# 当鼠标按下时变为 True
drawing = False
# 如果 mode 为 true 绘制矩形。按下'm' 变成绘制曲线。 mode=True
ix, iy = -1, -1
'''
cv2.getTrackbarPos() 函数的第一个参数是滑动条的名字
第二个参数 是滑动条被放置窗口的名字
第三个参数是滑动条的默认位置。
第四个参数是滑动条的最大值
第五个函数是回调函数, 每次滑动条的滑动都会调用回调函 数。
回调函数通常都会含有一个默认参数 就是滑动条的位置
'''
# 创建回调函数
def draw_circle(event, x, y, flags, param):
r = cv2.getTrackbarPos('R', 'image')
g = cv2.getTrackbarPos('G', 'image')
b = cv2.getTrackbarPos('B', 'image')
color = (b, g, r)
global ix, iy, drawing, mode
# 当按下左键是返回起始位置坐标
if event == cv2.EVENT_LBUTTONDOWN:
drawing = True
ix, iy = x, y
# 当鼠标左键按下并移动是绘制图形。event 可以查看移动,flag 查看是否按下
elif event == cv2.EVENT_MOUSEMOVE and flags == cv2.EVENT_FLAG_LBUTTON:
if drawing is True:
if mode is True:
cv2.rectangle(img, (ix, iy), (x, y), color, -1)
else:
# 绘制圆圈,小圆点连在一起就成了线,3 代表了笔画的粗细
cv2.circle(img, (x, y), 3, color, -1)
# 下面注释掉的代码是起始点为圆心,起点到终点为半径的
# r=int(np.sqrt((x-ix)**2+(y-iy)**2))
# cv2.circle(img,(x,y),r,(0,0,255),-1)
# 当鼠标松开停止绘画。
elif event == cv2.EVENT_LBUTTONUP:
drawing = False
# if mode==True:
# cv2.rectangle(img,(ix,iy),(x,y),(0,255,0),-1)
# else:
# cv2.circle(img,(x,y),5,(0,0,255),-1)
img = np.zeros((512, 512, 3), np.uint8)
mode = False
cv2.namedWindow('image')
cv2.createTrackbar('R', 'image', 0, 255, nothing)
cv2.createTrackbar('G', 'image', 0, 255, nothing)
cv2.createTrackbar('B', 'image', 0, 255, nothing)
cv2.setMouseCallback('image', draw_circle)
while True:
cv2.imshow('image', img)
k = cv2.waitKey(1) # & 0xFF
if k == ord('m'):
mode = not mode
elif k == ord("q"):
break
demo2:调节画布颜色演示效果和代码如下:
# -*- coding: utf-8 -*-
import cv2
import numpy as np
def nothing(x):
pass
# Create a black image, a window
img = np.zeros((300, 512, 3), np.uint8)
cv2.namedWindow('image', cv2.WINDOW_NORMAL)
# create trackbars for color change
cv2.createTrackbar('R', 'image', 0, 255, nothing)
cv2.createTrackbar('G', 'image', 0, 255, nothing)
cv2.createTrackbar('B', 'image', 0, 255, nothing)
# create switch for ON/OFF functionality
switch = '0 : OFF \n1 : ON'
cv2.createTrackbar(switch, 'image', 0, 1, nothing)
# 只有当 转换按钮 指向 ON 时 滑动条的滑动才有用,否则窗户 都是黑的。
while True:
# get current positions of four trackbars
r = cv2.getTrackbarPos('R', 'image')
g = cv2.getTrackbarPos('G', 'image')
b = cv2.getTrackbarPos('B', 'image')
s = cv2.getTrackbarPos(switch, 'image') # 另外一个重要应用就是用作转换按钮
if s == 0:
img[:] = 0
else:
img[:] = [b, g, r]
cv2.imshow('image', img)
k = cv2.waitKey(1) # & 0xFF
if k == ord("q"):
break
cv2.destroyAllWindows()
using OpenCvSharp;
using System;
namespace ConsoleApp
{
internal class Program
{
public static Mat img = new Mat();
// 矩形和圆形模式切换
public static bool mode = false;
public static bool drawing = false;
public static int ix = -1, iy = -1;
public static void draw_circle(MouseEventTypes @event, int x, int y, MouseEventFlags flags, IntPtr userData)
{
int r = Cv2.GetTrackbarPos("R", "image");
int g = Cv2.GetTrackbarPos("G", "image");
int b = Cv2.GetTrackbarPos("B", "image");
// 当按下左键是返回起始位置坐标
if (@event == MouseEventTypes.LButtonDown)
{
drawing = true;
ix = x;
iy = y;
}
if (@event == MouseEventTypes.MouseMove)
{
if(drawing)
{
if (mode)
{
Cv2.Rectangle(img, new Point(ix, iy), new Point(x, y), new Scalar(b, g, r), -1);
}
else
{
// 绘制圆圈,小圆点连在一起就成了线,3 代表了笔画的粗细
Cv2.Circle(img, new Point(x, y), 3, new Scalar(b, g, r), -1);
//下面注释掉的代码是起始点为圆心,起点到终点为半径的
//int r = (int)(Math.Sqrt((x - ix) * (x - ix) + (y - iy) * (y - iy)));
//Cv2.Circle(img, new Point(x, y), r, new Scalar(b, g, r), -1);
}
}
}
// 当鼠标松开停止绘画。
if (@event == MouseEventTypes.LButtonUp)
{
drawing = false;
}
}
public static void Main(string[] args)
{
img = new Mat(new Size(512, 512), MatType.CV_8UC3, new Scalar(0, 0, 0));
Cv2.NamedWindow("image", WindowFlags.AutoSize);
Cv2.CreateTrackbar("R", "image", 255, null);
Cv2.CreateTrackbar("G", "image", 255, null);
Cv2.CreateTrackbar("B", "image", 255, null);
Cv2.SetMouseCallback("image", new MouseCallback(draw_circle));
while (true)
{
Cv2.ImShow("image", img);
if (Cv2.WaitKey(1) == 'm')
{
mode = !mode;
}
}
}
}
}
demo2:调节画布颜色,演示代码如下:
using OpenCvSharp;
namespace ConsoleApp
{
internal class Program
{
public static void Main(string[] args)
{
string swit = "0 : OFF \n1 : ON";
Mat img = new Mat(new Size(512, 300), MatType.CV_8UC3, new Scalar(0, 0, 0));
Cv2.NamedWindow("image", WindowFlags.AutoSize);
Cv2.CreateTrackbar("R", "image", 255, null);
Cv2.CreateTrackbar("G", "image", 255, null);
Cv2.CreateTrackbar("B", "image", 255, null);
Cv2.CreateTrackbar(swit, "image", 1, null);
while (true)
{
int r = Cv2.GetTrackbarPos("R", "image");
int g = Cv2.GetTrackbarPos("G", "image");
int b = Cv2.GetTrackbarPos("B", "image");
int s = Cv2.GetTrackbarPos(swit, "image");
if (s == 0)
img = new Mat(new Size(512, 300), MatType.CV_8UC3, new Scalar(0, 0, 0));
else
img = new Mat(new Size(512, 300), MatType.CV_8UC3, new Scalar(b, g, r));
Cv2.ImShow("image", img);
Cv2.WaitKey(1);
img.Dispose();
}
}
}
}
C++版本运行代码如下:
#include "opencv2/core/core.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <iostream>
using namespace cv;
using namespace std;
#define DEMOID 0
// 绘制不同颜色的圆和矩形演示代码如下
#if DEMOID == 0
// 当鼠标按下时变为 True
bool drawing = false;
// 矩形和圆形模式切换
bool mode = false;
// 如果 mode 为 true 绘制矩形。按下'm' 变成绘制曲线。 mode=True
int ix = -1, iy = -1;
void on_MouseHandle(int event, int x, int y, int flags, void* param)
{
int r = getTrackbarPos("R", "image");
int g = getTrackbarPos("G", "image");
int b = getTrackbarPos("B", "image");
Mat& img = *(cv::Mat*)param;
if (event == EVENT_LBUTTONDOWN)
{
drawing = true;
ix = x;
iy = y;
}
if (event == EVENT_MOUSEMOVE)
{
if (drawing)
{
if (mode)
{
rectangle(img, Point(ix, iy), Point(x, y), Scalar(b, g, r), -1);
}
else
{
// 绘制圆圈,小圆点连在一起就成了线,3 代表了笔画的粗细
circle(img, Point(x, y), 3, Scalar(b, g, r), -1);
//下面注释掉的代码是起始点为圆心,起点到终点为半径的
//int r = (int)(Math.Sqrt((x - ix) * (x - ix) + (y - iy) * (y - iy)));
//circle(img, new Point(x, y), r, Scalar(b, g, r), -1);
}
}
}
// 当鼠标松开停止绘画。
if (event == EVENT_LBUTTONUP)
{
drawing = false;
}
}
void main()
{
Mat img(512, 512, CV_8UC3);
namedWindow("image");
createTrackbar("R", "image", NULL, 255);
createTrackbar("G", "image", NULL, 255);
createTrackbar("B", "image", NULL, 255);
setMouseCallback("image", on_MouseHandle, (void*)&img);
while (true)
{
imshow("image", img);
if (waitKey(1) == 'm')
{
mode = !mode;
}
}
}
#endif
// 调节画布颜色
#if DEMOID == 1
void main()
{
string swit = "0 : OFF \n1 : ON";
Mat img(300, 512, CV_8UC3);
namedWindow("image");
createTrackbar("R", "image", NULL, 255);
createTrackbar("G", "image", NULL, 255);
createTrackbar("B", "image", NULL, 255);
createTrackbar(swit, "image", NULL, 1);
while (true)
{
int r = getTrackbarPos("R", "image");
int g = getTrackbarPos("G", "image");
int b = getTrackbarPos("B", "image");
int s = getTrackbarPos(swit, "image");
if (s == 0)
img = Scalar::all(0);
else
img = Scalar(b, g, r);
imshow("image", img);
waitKey(1);
}
}
#endif
Python版本运行效果及代码如下:
demo1:绘制不同颜色的圆和矩形演示效果和代码如下:
# -*- coding: utf-8 -*-
import cv2
import numpy as np
def nothing(x):
pass
# 当鼠标按下时变为 True
drawing = False
# 如果 mode 为 true 绘制矩形。按下'm' 变成绘制曲线。 mode=True
ix, iy = -1, -1
'''
cv2.getTrackbarPos() 函数的第一个参数是滑动条的名字
第二个参数 是滑动条被放置窗口的名字
第三个参数是滑动条的默认位置。
第四个参数是滑动条的最大值
第五个函数是回调函数, 每次滑动条的滑动都会调用回调函 数。
回调函数通常都会含有一个默认参数 就是滑动条的位置
'''
# 创建回调函数
def draw_circle(event, x, y, flags, param):
r = cv2.getTrackbarPos('R', 'image')
g = cv2.getTrackbarPos('G', 'image')
b = cv2.getTrackbarPos('B', 'image')
color = (b, g, r)
global ix, iy, drawing, mode
# 当按下左键是返回起始位置坐标
if event == cv2.EVENT_LBUTTONDOWN:
drawing = True
ix, iy = x, y
# 当鼠标左键按下并移动是绘制图形。event 可以查看移动,flag 查看是否按下
elif event == cv2.EVENT_MOUSEMOVE and flags == cv2.EVENT_FLAG_LBUTTON:
if drawing is True:
if mode is True:
cv2.rectangle(img, (ix, iy), (x, y), color, -1)
else:
# 绘制圆圈,小圆点连在一起就成了线,3 代表了笔画的粗细
cv2.circle(img, (x, y), 3, color, -1)
# 下面注释掉的代码是起始点为圆心,起点到终点为半径的
# r=int(np.sqrt((x-ix)**2+(y-iy)**2))
# cv2.circle(img,(x,y),r,(0,0,255),-1)
# 当鼠标松开停止绘画。
elif event == cv2.EVENT_LBUTTONUP:
drawing = False
# if mode==True:
# cv2.rectangle(img,(ix,iy),(x,y),(0,255,0),-1)
# else:
# cv2.circle(img,(x,y),5,(0,0,255),-1)
img = np.zeros((512, 512, 3), np.uint8)
mode = False
cv2.namedWindow('image')
cv2.createTrackbar('R', 'image', 0, 255, nothing)
cv2.createTrackbar('G', 'image', 0, 255, nothing)
cv2.createTrackbar('B', 'image', 0, 255, nothing)
cv2.setMouseCallback('image', draw_circle)
while True:
cv2.imshow('image', img)
k = cv2.waitKey(1) # & 0xFF
if k == ord('m'):
mode = not mode
elif k == ord("q"):
break
demo2:调节画布颜色演示效果和代码如下:
# -*- coding: utf-8 -*-
import cv2
import numpy as np
def nothing(x):
pass
# Create a black image, a window
img = np.zeros((300, 512, 3), np.uint8)
cv2.namedWindow('image', cv2.WINDOW_NORMAL)
# create trackbars for color change
cv2.createTrackbar('R', 'image', 0, 255, nothing)
cv2.createTrackbar('G', 'image', 0, 255, nothing)
cv2.createTrackbar('B', 'image', 0, 255, nothing)
# create switch for ON/OFF functionality
switch = '0 : OFF \n1 : ON'
cv2.createTrackbar(switch, 'image', 0, 1, nothing)
# 只有当 转换按钮 指向 ON 时 滑动条的滑动才有用,否则窗户 都是黑的。
while True:
# get current positions of four trackbars
r = cv2.getTrackbarPos('R', 'image')
g = cv2.getTrackbarPos('G', 'image')
b = cv2.getTrackbarPos('B', 'image')
s = cv2.getTrackbarPos(switch, 'image') # 另外一个重要应用就是用作转换按钮
if s == 0:
img[:] = 0
else:
img[:] = [b, g, r]
cv2.imshow('image', img)
k = cv2.waitKey(1) # & 0xFF
if k == ord("q"):
break
cv2.destroyAllWindows()