[Letter from Hanzi & Python Encyclopedia] - Season 2 - Part 2 of opencv

Dear readers and bloggers:

Hello everyone, I am Hanzi. Let's continue today. If you don't know the content of the previous chapter, please go back to the previous article from the link or the homepage and read the previous article first, otherwise today's content will be difficult to understand.

(2 messages) [Letter from Hanzi & Python Encyclopedia]——Second Season——opencv first article details/128643200?spm=1001.2014.3001.5502 Also, if there is a problem with pip, please copy the following two pieces of code and enter it in the cmd command window!

pip install opencv-python

pip install numpy

Okay, enough nonsense, let's start the journey of learning


Table of contents

1. Slider

1.1. What is a slider?

1.2. Principle of sliding bar

1.3. Program code

2. Reference content

2.1. My code reports an error, what should I do?

2.2. Reference content

2.3.numpy contents


1. Slider

1.1. What is a slider?

Everyone must know that the slider bar is a bar with a sliding block inside, and the variable can be changed according to the position of the block!

1.2. Principle of sliding bar

The block position of the slider can change the variable, but the system cannot know how the programmer wants to change the variable, so a function is needed to let the program know how you want to change it. Be careful not to use generic lambda functions.

1.3. Program code

First we need to write a function, something like this:

def change(value):
    global x
    x = value

Remember that value needs to be added to the parameters of the function, otherwise the value cannot be changed and an error will be reported.

Next we need a big change!

The slider has a feature that it does not know which window to place it in, so it needs a named window that has not been displayed!

Use the function below to do it!

cv2.namedWindow(window_name)

Next is the very important function!

cv2.createTrackbar(barName, value, startValue, functionForBarChange)

Congratulations you're halfway there! Finally, you can join imshow!

Note: There is no need to add parentheses and parameters when adding functions.

All codes:

import cv2

cv2.namedWindow('demo')

pic = cv2.imread(r"C:\Users\用户名\Desktop\pic.jpg")

x = 0

def change(value):
    global x
    x = value

cv2.createTrackbar("change", (0, 100), 0, change)

# 下面的窗口名必须和namedWindow名称一致
cv2.imshow('demo', pic)
cv2.waitKey(0)

2. Reference content

2.1. My code reports an error, what should I do?

Your program may report an error because you did not use namedWindow. Or your function name is wrong, or your function may not add the value parameter

2.2. Reference content

Use of OpenCV -Python scroll bar function cv2.createTrackerbar()

NumPy Tutorial | Rookie Tutorial (runoob.com) https://www.runoob.com/numpy/numpy-tutorial.html

2.3.numpy contents

NumPy (Numerical Python) is an extended program library of the Python language that supports a large number of dimensional arrays and matrix operations, and also provides a large number of mathematical function libraries for array operations.

NumPy's predecessor, Numeric, was first developed by Jim Hugunin and other collaborators. In 2005, Travis Oliphant combined the features of another program library of the same nature, Numarray, in Numeric, and added other extensions to develop NumPy. NumPy is open source and developed by many collaborators.

One format for NumPy:

[0 0 0 0 0] or [[0 0 0] [0 0 0]]

Want to list? Yes, it's just that this storage format is separated by spaces! Can be created using numpy.zeros.

After opencv reads the picture, it is stored in this format, which involves hsv and bgr channels. This is even more difficult! From design to image filtering and color channels, we will continue to talk about these contents in the next issue! Alright, that's all for today's letter from Hanzi! See you next time!

Hanzi

2023/1/12

Guess you like

Origin blog.csdn.net/B20111003/article/details/128653848