


Detailed explanation of simple drawing board function based on opencv
OpenCV provides many simple statements to implement complex functions. Based on the basic statements of color and mouse interaction, we can build a simple drawing board. Although it is simple, there are no fewer steps to make the frame.
If you are interested, you can write it according to my steps, or directly copy the code in the main program and run it.
Related learning recommendations: python video tutorial
1. Function
Clear the drawing board function, It is convenient for subsequent programming.
Target functions: Adjustable colors, drawing area, and mouse input.
2. Framework construction
There are two main steps:
The first step:Establishment The color selection can be set using the OpenCV slider statement cv.createTrackbar. cv.createTrackbar has a total of five parameter settings, which are (Trackbar name, target window name, starting value - also the default value, maximum value, callback function).
#创建画布 img=np.zeros((300,512,3),np.uint8) #给画布命名——需要提供给Trackbar cv.namedWindow("image") #创建RGB三种颜色的Trackbar,返回函数不需要做任何动作 cv.createTrackbar("R","image",0,255,nothing) cv.createTrackbar("G","image",0,255,nothing) cv.createTrackbar("B","image",0,255,nothing) #由于OpenCv里面无按钮函数,所以用0/1来表示开关 switch="0:OFF\n1:ON" cv.createTrackbar(switch,"image",0,1,nothing) #回调函数,不需要做任何操作 def nothing(x): pass
Step 2:Capture the mouse action status, and draw graphics in the drawing area based on the mouse action. Common mouse operation statements can be viewed in python using the statement *events = [i for i in dir(cv) if ‘EVENT’ in i]*.
#设置一个是否开始画画的开关 drawing=False #定义画画函数 def draw(event,x,y,flag,param): #将画画开关作为全局变量,以便于在每次动作以后改变它的值 global drawing #使用函数cv.EVENT_LBUTTONDOWN,如果鼠标左键按下,画圆,同时drawing为真 if event==cv.EVENT_LBUTTONDOWN: cv.circle(img,(x,y),3,(g,b,r),-1) drawing=True #如果鼠标移动,进行下面操作 elif event==cv.EVENT_MOUSEMOVE: #如果drawing为真,开始画圆 if drawing==True: cv.circle(img, (x, y), 3, (g, b, r), -1) #如果鼠标左键抬起,画最后一个圆,并将drawing设为False,此时就会停止画画 elif event==cv.EVENT_LBUTTONUP: cv.circle(img,(x,y),3,(g,b,r),-1) drawing=False
3. Main function
The main function is to integrate the above two parts.
while(1): #展示画布 cv.imshow("image",img) #设置终止按钮,为了保证在32/64位系统或者在不是ASCII编码的键盘上有正常的操作,使用&0xFF取低八位 k=cv.waitKey(1)&0xFF #如果k为27,即键盘上Esc的ASCII编码序号,退出画板 if k==27: break #获取每个Trackbar中的值 r=cv.getTrackbarPos("R","image") g = cv.getTrackbarPos("G", "image") b = cv.getTrackbarPos("B", "image") s = cv.getTrackbarPos(switch, "image") #如果开关是0,清空画板并禁止画画,如果是1,则允许 if s==0: img[:]=0 else: cv.setMouseCallback("image", draw) #最后不要忘记销毁窗口 cv.destroyAllWindows()
4. Running effect
##5. Summary
From this simple In the drawing board, we mainly learned and used the mouse function cv.setMouseCallback and cv.createTrackbar, two basic OpenCV functions, and explored the details and found out what needs attention.6. General Procedure
import cv2 as cv import numpy as np def nothing(x): pass img=np.zeros((300,512,3),np.uint8) cv.namedWindow("image") cv.createTrackbar("R","image",0,255,nothing) cv.createTrackbar("G","image",0,255,nothing) cv.createTrackbar("B","image",0,255,nothing) switch="0:OFF\n1:ON" cv.createTrackbar(switch,"image",0,1,nothing) drawing=False def draw(event,x,y,flag,param): global drawing if event==cv.EVENT_LBUTTONDOWN: cv.circle(img,(x,y),3,(g,b,r),-1) drawing=True elif event==cv.EVENT_MOUSEMOVE: if drawing==True: cv.circle(img, (x, y), 3, (g, b, r), -1) elif event==cv.EVENT_LBUTTONUP: cv.circle(img,(x,y),3,(g,b,r),-1) drawing=False while(1): cv.imshow("image",img) k=cv.waitKey(1)&0xFF if k==27: break r=cv.getTrackbarPos("R","image") g = cv.getTrackbarPos("G", "image") b = cv.getTrackbarPos("B", "image") s = cv.getTrackbarPos(switch, "image") if s==0: img[:]=0 else: cv.setMouseCallback("image", draw) cv.destroyAllWindows()
The above is the detailed content of Detailed explanation of simple drawing board function based on opencv. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Use the pip command to easily install OpenCV tutorial, which requires specific code examples. OpenCV (OpenSource Computer Vision Library) is an open source computer vision library. It contains a large number of computer vision algorithms and functions, which can help developers quickly build image and video processing related applications. Before using OpenCV, we need to install it first. Fortunately, Python provides a powerful tool pip to manage third-party libraries

1. Theoretical basis of image pyramid Image pyramid is a kind of multi-scale expression of images. It is an effective but conceptually simple structure to explain images at multiple resolutions. An image pyramid is a collection of images with progressively lower resolutions arranged in a pyramid shape and derived from the same original image. It is obtained through ladder down sampling, and the sampling is not stopped until a certain termination condition is reached. We compare images layer by layer to a pyramid. The higher the level, the smaller the image and the lower the resolution. So why do we make an image pyramid? This is because changing the size of a pixel sometimes does not change its characteristics. For example, if you show you a picture of 10 million pixels, you can know that there is a person in it. If you show you a picture of 100,000 pixels, you can also know that there is a person in it. But against the plan

OpenCV is an open source library for computer vision and image processing, which is widely used in machine learning, image recognition, video processing and other fields. When developing using OpenCV, in order to better debug and run programs, many developers choose to use PyCharm, a powerful Python integrated development environment. This article will provide PyCharm users with an installation tutorial for OpenCV, with specific code examples. Step One: Install Python First, make sure you have Python installed

1. Project effect 2. Core process 1. openCV reads the video stream and draws a rectangle on each frame of the picture. 2. Use mediapipe to obtain the coordinates of finger key points. 3. Based on the coordinate position of the finger and the coordinate position of the rectangle, determine whether the finger point is on the rectangle. If it is, the rectangle will follow the finger movement. 3. Code process environment preparation: python:3.8.8opencv:4.2.0.32mediapipe:0.8.10.1 Note: 1. If the opencv version is too high or too low, there may be some problems such as the camera not being able to open, crashing, etc. The python version affects opencv Optional versions. 2. pipinstallmediapipe may cause op

The org.opencv.imgproc package of the JavaOpenCV library contains a class called Imgproc that provides various methods to process input images. It provides a set of methods for drawing geometric shapes on images. To draw an arrowed line, you need to call the arrowedLine() method of this class. The method accepts the following parameters: a Mat object representing the image on which the line is to be drawn. A Point object representing two points between lines. drawn. A Scalar object representing the line color. (BGR) An integer representing the thickness of the line (default: 1). Example importorg.opencv.core.Core;importo

How to implement video processing using PHP and OpenCV library? Abstract: Video processing has become an important technology in modern scientific and technological applications. This article will introduce how to use the PHP programming language combined with the OpenCV library to implement some basic video processing functions, and attach corresponding code examples. Keywords: PHP, OpenCV, video processing, code examples Introduction: With the development of the Internet and the popularity of smartphones, video content has become an indispensable part of people's lives. However, to achieve video editing and

Image segmentation and extraction: Foreground objects are segmented or extracted as target images in images. The background itself is not interested. The watershed algorithm and the GrabCut algorithm segment and extract the image. Use watershed algorithm to achieve image segmentation and extraction. The watershed algorithm vividly compares images to geographical terrain surfaces to achieve image segmentation. This algorithm is very effective. Algorithm Principle Any grayscale image can be regarded as a geographical terrain surface. Areas with high grayscale values can be seen as mountain peaks, and areas with low grayscale values can be seen as valleys. The image on the left is the original image, and the image on the right is its corresponding "topographic surface". This process separates the image into two distinct sets: catchment basins and watershed lines. The dam we constructed is the watershed line, that is, the original image

PyCharm is a powerful Python integrated development environment (IDE) developed by JetBrains. It provides a wealth of functions and tools to help Python developers write code, debug programs and manage projects. Using OpenCV, a powerful computer vision library, in PyCharm, you can easily perform image processing, video processing and other tasks. This article will detail the steps to install and configure OpenCV in PyCharm and provide specific code examples. 1.An
