简体   繁体   English

用于OpenCV中图片形态的跟踪栏(Python)

[英]Trackbar for picture morphology in opencv (python)

So my issue is that when I go to edit a photo I can't get the final settings from the trackbar to save and update the image. 所以我的问题是,当我去编辑照片时,无法从轨迹栏获取最终设置来保存和更新图像。

I found code on github that I based my code on which is here: 我在github上找到了基于我的代码的代码:

https://github.com/opencv/opencv/blob/master/samples/python/morphology.py https://github.com/opencv/opencv/blob/master/samples/python/morphology.py

This is just implementation of trackbars. 这只是跟踪栏的实现。 It doesn't even save between modes and the final image once you are out of the function definition does not change at all. 一旦超出功能定义,它甚至不会在模式之间保存,并且最终图像也不会保存。 So I have tried other ways. 所以我尝试了其他方法。 Here is one way that seemed promising: 这是一种看似有希望的方法:

def nothing(x):
pass

def isOrdered(img, cur_mode):
    str_mode = 'ellipse'
    cv2.namedWindow('edit')
    cv2.createTrackbar('op/size', 'edit', 11, 20, nothing)
    cv2.createTrackbar('iters', 'edit', 1, 10, nothing) 
    while(100):
        cv2.imshow('edit', img)
        k = cv2.waitKey(100) & 0xFF
        if k == 27:
            break
        sz = cv2.getTrackbarPos('op/size', 'edit')
        iters = cv2.getTrackbarPos('iters', 'edit')
        opers = cur_mode.split('/')
        if len(opers) > 1:
            sz = sz - 10
            op = opers[sz > 0]
            sz = abs(sz)
        else:
            op = opers[0]
        sz = sz*2+1

        str_name = 'MORPH_' + str_mode.upper()
        oper_name = 'MORPH_' + op.upper()
        st = cv2.getStructuringElement(getattr(cv2, str_name), (sz, sz))
        res = cv2.morphologyEx(img, getattr(cv2, oper_name), st, iterations=iters)

        draw_str(res, (10, 20), 'mode: ' + cur_mode)
        draw_str(res, (10, 40), 'operation: ' + oper_name)
        draw_str(res, (10, 60), 'structure: ' + str_name)
        draw_str(res, (10, 80), 'ksize: %d  iters: %d' % (sz, iters))
    cv2.destroyAllWindows()
    return(sz, iters, op)


modes = ['erode/dilate', 'open/close', 'blackhat/tophat', 'gradient']
str_mode = 'ellipse'
for cur_mode in modes:
    sz, iters, op = isOrdered(im, cur_mode)
    str_name = 'MORPH_' + str_mode.upper()
    oper_name = 'MORPH_' + op.upper()
    st = cv2.getStructuringElement(getattr(cv2, str_name), (sz, sz))
    im = cv2.morphologyEx(im, getattr(cv2, oper_name), st, iterations=iters)
cv2.imshow('final', im)
cv2.waitKey(0) & 0xFF
cv2.destroyAllWindows()

with this way I can't get the picture to update while I am changing the track bar settings to be able to see what it's doing and the final image is black even though the intermediate images between modes isn't. 这样,即使更改模式之间的中间图像,也无法更改轨迹栏设置以能够查看其操作,并且最终图像为黑色,因此无法更新图片。

Keep you original code, modify these lines. 保留原始代码,修改这些行。

    res = img.copy()
    while(100):
        #cv2.imshow("edit", img)
        cv2.imshow("edit", res)

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

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