0

所以我的问题是,当我去编辑照片时,我无法从轨迹栏获得最终设置来保存和更新图像。

我在 github 上找到了基于我的代码的代码:

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

这只是轨迹栏的实现。一旦你离开函数定义,它甚至不会在模式之间保存,最终图像根本不会改变。所以我尝试了其他方法。这是一种看起来很有希望的方法:

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()

通过这种方式,当我更改轨迹栏设置以便能够看到它在做什么时,我无法更新图片,即使模式之间的中间图像不是,最终图像也是黑色的。

4

1 回答 1

0

保留原始代码,修改这些行。

    res = img.copy()
    while(100):
        #cv2.imshow("edit", img)
        cv2.imshow("edit", res)
于 2017-11-23T19:18:17.997 回答