简体   繁体   English

奇数仅在 Python 的 OpenCV 轨迹栏上?

[英]Odd numbers only on OpenCV trackbar for Python?

I am learning how to use OpenCV on Python for skin segmentation and right now I am mostly in the experimental phase, where I am playing with the Gaussian Blue to reduce the sharp contrasts which I am getting with Otsu's Binarization.我正在学习如何在 Python 上使用 OpenCV 进行皮肤分割,现在我主要处于实验阶段,在那里我正在使用高斯蓝来减少我使用 Otsu 的二值化得到的鲜明对比。

One stratergy that I found very useful in my experimentation was to use the trackbar functionality on the display window to change various parameters such as selection of the kernel size and standard deviation of the Gaussian function.我在实验中发现非常有用的一种策略是使用显示窗口上的轨迹栏功能来更改各种参数,例如选择内核大小和高斯函数的标准偏差。 The trackbar works great when I change the std, but my program crashes when I do the same for kernel size.当我更改 std 时,轨迹栏工作得很好,但是当我对内核大小执行相同操作时,我的程序崩溃了。

The reason for this is that kernel size takes only odd numbers > 1 as a tuple of two values.这样做的原因是内核大小仅将奇数 > 1 作为两个值的元组。 Since the track bar is continuous, when I move it and the trackbar reads an even number, the Gaussian function throws an error.由于轨迹栏是连续的,当我移动它并且轨迹栏读取偶数时,高斯函数会引发错误。

I was hoping that you could provide me with a solution to create a trackbar with only odd numbers or even only numbers from an array, if possible.如果可能的话,我希望您能为我提供一个解决方案来创建一个只有奇数或什至只有数组中的数字的轨迹栏。 Thanks!谢谢!

# applying otsu binerization to video stream
feed = cv2.VideoCapture(0)

# create trackbars to control the amount of blur 
cv2.namedWindow('blur')
# callback function for trackbar
def blur_callback(trackbarPos):
    pass
# create the trackbar 
cv2.createTrackbar('Blur Value', 'blur', 1, 300, blur_callback)
# cv2.createTrackbar('Kernel Size', 'blur', 3, 51, blur_callback)

while True:
    vid_ret, frame = feed.read()
    # flip the frames 
    frame = cv2.flip(frame, flipCode=1)

    # convert the feed to grayscale
    frame_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    # get blur value from trackbar and apply gaussian blur to frame_gray
    blurVal = cv2.getTrackbarPos('Blur Value', 'blur')
#     kernelSize = cv2.getTrackbarPos('Kernel Size', 'blur')
    frame_blur = cv2.GaussianBlur(frame_gray, (11, 11), blurVal)

    # apply Otsu binerization on vanilla grayscale
    otsu_ret, otsu = cv2.threshold(frame_gray, 0, 255, cv2.THRESH_OTSU)

    # apply Otsu binerization on blurred grayscale
    otsu_blue_ret, otsu_blur = cv2.threshold(frame_blur, 0, 255, cv2.THRESH_OTSU)


    # show the differnt images
    cv2.imshow('color', frame)
#     cv2.imshow('gray', frame_gray)
    cv2.imshow('blur', frame_blur)
    cv2.imshow('otsu', otsu)
    cv2.imshow('otsu_blur', otsu_blur)
    # exit key
    if cv2.waitKey(10) & 0xFF == ord('q'):
        break

# release the feed and close all windows
feed.release()
cv2.destroyAllWindows()

Use remainder function & add 1 value for even number, so you won't crash使用余数函数并为偶数添加 1 个值,这样您就不会崩溃

Here you go:干得好:

cv2.createTrackbar('Blur Value', 'blur', 1, 20, blur_callback)

while True:

    gaus = cv2.getTrackbarPos('Blur Value', 'blur')

    if gaus == 0:
        ......
    else:
        count = gaus % 2 

        if (count == 0):
            gaus += 1
            frame_blur = cv2.GaussianBlur(frame_gray, (gaus, gaus), 0)
        else:
            frame_blur = cv2.GaussianBlur(frame_gray, (gaus, gaus), 0)

I've been doing this function, not really perfect but it runs.我一直在做这个功能,不是很完美,但它运行。 Do check it out on my Github: https://github.com/jiasuan/opencv/blob/master/Trackbar.py请在我的 Github 上查看: https : //github.com/jiasuan/opencv/blob/master/Trackbar.py

It's easier for you to observe image when you use trackbar.当您使用轨迹栏时,您更容易观察图像。 But the arrangement of each functions will affect result of your data.但是每个功能的排列会影响您的数据结果。 For example, blur before grayscale and blur after grayscale, result will be vary.例如,灰度前模糊和灰度后模糊,结果会有所不同。 You have to carefully arrange it before you run.你必须在跑步之前仔细安排它。

To add to Louis' answer, you can move the trackbar to the odd position by using setTrackbarPos()要添加到 Louis 的答案中,您可以使用 setTrackbarPos() 将轨迹栏移动到奇数位置

see: https://docs.opencv.org/trunk/d7/dfc/group__highgui.html#ga67d73c4c9430f13481fd58410d01bd8d见: https : //docs.opencv.org/trunk/d7/dfc/group__highgui.html#ga67d73c4c9430f13481fd58410d01bd8d

Try this:尝试这个:

cv2.createTrackbar('Blur Value', 'blur', 1, 20, blur_callback)

while True:
    gaus = cv2.getTrackbarPos('Blur Value', 'blur')
    if gaus%2 == 0:
        gaus+=1 # here all even numbers will be converted to odd. Make range as 1-19 than 1-20 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM