SoFunction
Updated on 2024-11-13

The Basics in Python Opencv

OpenCV is a popular open-source computer vision library available for different programming languages such as Python, C++, and JavaScript.It provides a rich set of tools to process and analyze images and videos, allowing you to do everything from resizing single images to building complex object recognition applications.

This article gives you an introduction to the basics in Python Opencv.

1. Create a window

import cv2
import numpy as np
def createWindow():
    #Read the picture
    img=('images/1 (1).jpg')
    #create windows flags=WINDOW_NORMAL means you can change the window size
    (winname='window',flags=cv2.WINDOW_NORMAL)
    # Scale the size of the window
    (winname='window',width=300,height=200)
    #Display Window
    ('window',img)
    # Get mouse or key value
    key=(0)
    if (key&0XFF==ord('Q')):
        ()
         # Destroy all windows
if __name__ == '__main__':
    print('PyCharm')
    createWindow()

2. Save the picture

import cv2
import numpy as np
def createWindow():
    #Read the picture
    img=('images/1 (1).jpg')
    #create windows flags=WINDOW_NORMAL means you can change the window size
    (winname='window',flags=cv2.WINDOW_NORMAL)
    # Scale the size of the window
    (winname='window',width=300,height=200)
    while True:
        #Display Window
        ('window',img)
        # Get mouse or key value
        key=(0)
        if (key&0XFF==ord('Q')):
            break
        elif (key&0xFF==ord('s')):
            # Save the picture
            # name-saved file name img-saved image
            ('save_pic.png', img)
            break
    ()
    # Destroy all windows
if __name__ == '__main__':
    print('PyCharm')
    createWindow()

3. Capture video

import os
import cv2
import numpy as np
def CollectVideo():
    #Creating windows
    (winname='window',flags=cv2.WINDOW_AUTOSIZE)
    (winname='window',width=450,height=300)
    # Turn on the camera
    cap=(0)
    fourcc = cv2.VideoWriter_fourcc(*'MJPG')
    # Get window size
    size = (int((cv2.CAP_PROP_FRAME_WIDTH)), int((cv2.CAP_PROP_FRAME_HEIGHT)))
    # Output files Multimedia file formats Video frame rate Resolution size
    vw = ('', fourcc, 25, size)
    while ():
        #Read video frames from the camera
        OK,frame=()
        if OK:
            #Display the camera screen
            ('window',frame)
            (winname='window', width=450, height=300)
            # Write the video frames captured from the camera to a file
            (frame)
        # Get keys from the mouse and keyboard, press ESC to exit.
        if (1)&0xFF==27:
            break
    # Resources released
    ()
    ()
    # Destroy all windows
    ()
if __name__ == '__main__':
    print('Pycharm')
    CollectVideo()

4. Mouse control

# Explanation of callback function parameters
#event: mouse movement, press left button;
#(x,y): mouse coordinates
#flags: mouse buttons and key combinations
import cv2
import numpy as np

# Callback function definition
def mouse_callback(event,x,y,flags,userdata):
    print(event,x,y,flags,userdata)


#Creating windows
(winname='mouse',flags=cv2.WINDOW_NORMAL)
(winname='mouse',width=450,height=300)
# Set callback function for "mouse" window.
('mouse',mouse_callback,'123')

img=(shape=(300,450,3),dtype=np.uint8)
while True:
    ('mouse',img)
    # Press ESC to exit
    if (1)&0xFF==27:
        break
()
if __name__ == '__main__':
    print('pycharm')

subassemblies

import os
import cv2
import numpy as np
(winname='trackbar',flags=cv2.WINDOW_NORMAL)
(winname='trackbar',width=450,height=300)
# Get the value of TrackBar
def TrackBarValue():
    #Get the value of the sub-window "R" under the window "window".
    value_R=  (trackbarname='R', winname='trackbar')
    value_G = (trackbarname='G', winname='trackbar')
    value_B = (trackbarname='B', winname='trackbar')
    return value_R,value_G,value_B
def callback():
    pass
# Define the TrackBar function
def TrackBarBGR():
    #value-value of the trackbar count-maximum count to set (minimum value is 0) OnChange-callback function
    ('R','trackbar',  0, 255, callback)
    ('G','trackbar',  0, 255, callback)
    ('B','trackbar', 0, 255, callback)
img=(shape=(450,300,3),dtype=np.uint8)
# Create trackbar component
TrackBarBGR()
while True:
    # Get the value of the trackbar
    R,G,B=TrackBarValue()
    img[:]=[B,G,R]
    # Change the color of the background after getting the value
    ('trackbar', img)
    # Press ESC to exit
    if (1)&0xFF==27:
        break
()
if __name__ == '__main__':
    print('Pycharm')

To this point this article on the basics of Python Opencv in the knowledge of the article is introduced to this, more related to the basics of Python Opencv content, please search for my previous articles or continue to browse the following related articles I hope that you will support me more in the future!