OpenCV创建跟踪栏

x6492ojm  于 2023-02-09  发布在  其他
关注(0)|答案(1)|浏览(139)

当我运行这段代码时:

def nothing(x):
    pass

cv.createTrackbar(‘R’, ‘image’, 0,255,nothing)

我得到这个错误:使用“value”指针不安全且已弃用。请使用NULL作为值指针。若要获取跟踪条值,请安装回调。
我不知道该怎么办。

cbjzeqam

cbjzeqam1#

你没有显示最小的工作代码,这使得问题,所以我只能猜测。
在使用createTrackbar()之前,必须创建窗口"image"

cv2.namedWindow('image')
cv2.createTrackbar('R', 'image', 0, 255, function)

最小工作示例:

import cv2

# --- functions ---

def function(value):
    print(value)

    new_img = img.copy()
    new_img[:,:,2] = value

    cv2.imshow('image', new_img)

# --- main ---
    
img = cv2.imread('lenna.png')

cv2.namedWindow('image')
cv2.createTrackbar('R', 'image', 0, 255, function)

cv2.imshow('image', img)
cv2.waitKey(0)

cv2.destroyAllWindows()
    • 编辑:**

更复杂的示例,其中三个跟踪条使用相同的函数,但索引不同。

import cv2

# --- functions ---

def function(index, value):
    percent = (value/100)
    #print(index, value, percent)

    img[:,:,index] = original_img[:,:,index] * percent

    cv2.imshow('image', img)
   
# --- main ---
 
img = cv2.imread('lenna.png')

original_img = img.copy()

cv2.namedWindow('image')

cv2.createTrackbar('R (%)', 'image', 100, 100, lambda value:function(2, value))
cv2.createTrackbar('G (%)', 'image', 100, 100, lambda value:function(1, value))
cv2.createTrackbar('B (%)', 'image', 100, 100, lambda value:function(0, value))

cv2.imshow('image', img)
cv2.waitKey(0)

cv2.destroyAllWindows()

图片Lenna来自维基百科

    • 编辑:**

您的问题是窗口使用了两个名称-imgimage-但您应该在namedWindow()createTrackbar()getTrackbarPos()imshow()中使用相同的名称
顺便说一句:如果你用'0 : OFF \n1 : ON'创建跟踪条,那么你必须使用它来获得s = cv.getTrackbarPos(switch, 'image')的值

    • 编辑:**

您的代码似乎与文档中的演示代码相同,并且demo在所有命令中都使用"image"demo还使用cv.getTrackbarPos(switch, ...)

import cv2 as cv
import numpy as np

def nothing(x): 
    pass

img = np.array ((512,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', 1, 1, nothing)

while True:
    cv.imshow('image', img)
    
    key = cv.waitKey(1) & 0xFF
    
    if key == 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 
        # reset trackbars
        #cv.setTrackbarPos('R', 'image', 0)
        #cv.setTrackbarPos('G', 'image', 0)
        #cv.setTrackbarPos('B', 'image', 0)
    else:
        img[:] = [b,g,r]
        
cv.destroyAllWindows()

nothing中的代码相同,因此仅当您更改任何跟踪条中的值时才会执行。

import cv2 as cv
import numpy as np

# --- functions ---

def nothing(value): 
    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
        # reset trackbars
        #cv.setTrackbarPos('R', 'image', 0)
        #cv.setTrackbarPos('G', 'image', 0)
        #cv.setTrackbarPos('B', 'image', 0)
    else:
        img[:] = [b,g,r]
        
# --- main ---

img = np.array ((512,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', 1, 1, nothing)

while True:
    cv.imshow('image', img)
    
    key = cv.waitKey(1) & 0xFF
    
    if key == 27:
        break
    
cv.destroyAllWindows()

相关问题