cv2.createTrackbar参数在回调函数中未更新

2024-10-01 17:33:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试实现一个轨迹栏,允许放大和缩小图像。在本练习中,有两个轨迹栏:第一个轨迹栏处理比例因子,而第二个轨迹栏处理图像比例的增加或减小。第一个轨迹栏似乎可以工作,因为我可以随意增加图像的大小(scaleFactor)。但是,第二个轨迹栏不会更新函数中的值(scaleType),因此不会更改模式以缩小图像的比例

我认为错误在于我将变量传递到函数中的方式,因为第一个轨迹栏的文档和示例非常简单

代码如下:

import cv2


maxScaleUp = 100
scaleFactor = 1
scaleType = 0
maxType = 1

windowName = "Resize Image"
trackbarValue = "Scale"
trackbarType = "Type: \n 0: Scale Up \n 1: Scale Down"


# load an image
im = cv2.imread("truth.png")

# Create a window to display results
cv2.namedWindow(windowName, cv2.WINDOW_AUTOSIZE)

# Callback functions
def scaleImage(*args):
    global scaleFactor
    global scaleType


    if scaleType == 0:

        scaleFactor = 1+ args[0]/100.0
        if scaleFactor == 0:
            scaleFactor = 1
        scaledImage = cv2.resize(im, None, fx=scaleFactor,\
                fy = scaleFactor, interpolation = cv2.INTER_LINEAR)

    else:       
        scaleFactor = 1 - args[0]/100.0
        if scaleFactor == 0:
            scaleFactor = 1
        scaledImage = cv2.resize(im, None, fx=scaleFactor,\
                fy = scaleFactor, interpolation = cv2.INTER_LINEAR)
    print(scaleType)
    cv2.imshow(windowName, scaledImage)

def functionType( *args):
    return 0



cv2.createTrackbar(trackbarValue, windowName, scaleFactor, maxScaleUp, scaleImage)
cv2.createTrackbar(trackbarType, windowName, scaleType, maxType, functionType)


scaleImage(25)

while True:
    c = cv2.waitKey(20)

    if c==27:
        break

cv2.destroyAllWindows()

Tags: 函数图像if轨迹argscv2比例scale
1条回答
网友
1楼 · 发布于 2024-10-01 17:33:48

这似乎很管用。基本上有两个回调函数,它们接受参数并更新变量

import cv2


maxScaleUp = 100
scaleFactor = 1
scaleType = 0
maxType = 1

windowName = "Resize Image"
trackbarValue = "Scale"
trackbarType = "Type: \n 0: Scale Up \n 1: Scale Down"


# load an image
im = cv2.imread("truth.png")

# Create a window to display results
cv2.namedWindow(windowName, cv2.WINDOW_AUTOSIZE)

# Callback functions
def scaleImage(*args):
    global scaleFactor
    global scaleType


    if scaleType == 0:

        scaleFactor = 1+ args[0]/100.0
        if scaleFactor == 0:
            scaleFactor = 1
        scaledImage = cv2.resize(im, None, fx=scaleFactor,\
                fy = scaleFactor, interpolation = cv2.INTER_LINEAR)

    else:       
        scaleFactor = 1 - args[0]/100.0
        if scaleFactor == 0:
            scaleFactor = 1
        scaledImage = cv2.resize(im, None, fx=scaleFactor,\
                fy = scaleFactor, interpolation = cv2.INTER_LINEAR)
    print(scaleType)
    cv2.imshow(windowName, scaledImage)

def functionType( *args):
    global scaleType
    global scaleFactor
    scaleType = args[0]
    scaleFactor = 1 + scaleFactor/100.0
    if scaleFactor ==0:
        scaleFactor = 1
    scaledImage = cv2.resize(im, None, fx=scaleFactor,\
            fy = scaleFactor, interpolation = cv2.INTER_LINEAR)
    cv2.imshow(windowName, scaledImage)


cv2.createTrackbar(trackbarValue, windowName, scaleFactor, maxScaleUp, scaleImage)
cv2.createTrackbar(trackbarType, windowName, scaleType, maxType, functionType)

相关问题 更多 >

    热门问题