createTrackbar

Posted skydaddy

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了createTrackbar相关的知识,希望对你有一定的参考价值。

import cv2
import numpy as np
#载入图片
#img_original=cv2.imread(‘1110_3_1_X_537_Y_1700_qly_100_fail_13d17h46m24s.bmp‘,0)
img_original=cv2.imread(‘1141_1_1_X_551_Y_1701_qly_100_pass_subtpl_25_update_1153677968_13d17h53m10s.bmp‘,0)
h,w=img_original.shape
img_original=cv2.resize(img_original,(3*w,3*h))
rangecolorimg=img_original.copy()
#设置窗口
cv2.namedWindow(‘Canny‘)
cv2.namedWindow(‘morphed‘)
cv2.namedWindow(‘colorRange‘)
#定义回调函数
def nothing(x):
    pass
#创建两个滑动条,分别控制threshold1,threshold2
cv2.createTrackbar(‘threshold1‘,‘Canny‘,50,100,nothing)
cv2.createTrackbar(‘threshold2‘,‘Canny‘,100,100,nothing)
cv2.createTrackbar(‘morph‘,‘morphed‘,5,20,nothing)
cv2.createTrackbar(‘range1‘,‘colorRange‘,0,255,nothing)
cv2.createTrackbar(‘range2‘,‘colorRange‘,100,255,nothing)
cv2.createTrackbar(‘replacevalue‘,‘colorRange‘,120,255,nothing)
while(1):
    #返回滑动条所在位置的值
    rangecolorimg=img_original.copy()
    blackcolorimg=img_original.copy()
    threshold1=cv2.getTrackbarPos(‘threshold1‘,‘Canny‘)
    threshold2=cv2.getTrackbarPos(‘threshold2‘,‘Canny‘)
    range1=cv2.getTrackbarPos(‘range1‘,‘colorRange‘)
    range2=cv2.getTrackbarPos(‘range2‘,‘colorRange‘)
    replacevalue=cv2.getTrackbarPos(‘replacevalue‘,‘colorRange‘)
    maskrange=np.zeros_like(img_original)
    blackrange=np.ones_like(img_original)*255
    x1=img_original[img_original>range1]
    x2=img_original[img_original<range2]
    #x3=(img_original>range1)&(img_original<range2)
    x3 = cv2.inRange(img_original, range1, range2)
    import pdb
    #pdb.set_trace()
    #x3=cv2.bitwise_and(x1, x2)
    #bitwise_and
    #rangecolorimg[x3]=255
   
    rangecolorimg[x3!=0]=replacevalue
    blackmask = 255 * np.ones(rangecolorimg.shape, rangecolorimg.dtype)
    #blackmask=src_mask
    width, height = img_original.shape
    center = (int(height/2), int(width/2))
    seamlessCloneimg=rangecolorimg
    #seamlessCloneimg = cv2.inpaint(seamlessCloneimg, x3, 10, cv2.INPAINT_NS)#cv2.INPAINT_NScv2.INPAINT_TELEA
   
    seamlessCloneimg = cv2.seamlessClone(cv2.cvtColor(img_original, cv2.COLOR_GRAY2BGR) ,cv2.cvtColor(rangecolorimg, cv2.COLOR_GRAY2BGR) , blackmask, center, cv2.MIXED_CLONE)   
    blackrange[x3!=0]=blackcolorimg[x3!=0]
    normalizeimg = np.zeros(blackrange.shape, dtype=np.float32)
    cv2.normalize(np.float32(rangecolorimg), dst=normalizeimg, alpha=1.0, beta=0, norm_type=cv2.NORM_MINMAX)   
    #Canny边缘检测
    img_edges=cv2.Canny(img_original,threshold1,threshold2)
    #显示图片
    img=img_edges
    morphradius=cv2.getTrackbarPos(‘morph‘,‘morphed‘)
    kernel   = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (morphradius,morphradius))
    morphed  = cv2.morphologyEx(img, cv2.MORPH_CLOSE, kernel)
    contours,_ = cv2.findContours(morphed, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
    import pdb
    #pdb.set_trace()
    rgbmorphed=cv2.cvtColor(morphed, cv2.COLOR_GRAY2BGR)   
    # for i in range(len(contours)):
        # #cv2.drawContours(immask,[contours[i]],-1,(x+np.random.randint(0, 30),x+np.random.randint(0, 30),x+np.random.randint(0, 30)),cv2.FILLED)
        # cv2.drawContours(rgbmorphed,[contours[i]],-1,(0,255,0),cv2.FILLED)
    cv2.drawContours(rgbmorphed,contours,-1,(255,255,255),cv2.FILLED)       
    import pdb
    #pdb.set_trace()
    if(0):
        cv2.imshow("morphed2",morphed)
        cv2.imshow("morphed",morphed)
        cv2.imshow(‘Canny‘,img_edges)
        cv2.imshow(‘rgbmorphed‘,rgbmorphed)
    #cv2.waitKey(0)   
    cv2.imshow("colorRange",rangecolorimg)
    cv2.imshow("x3",x3)
   
    cv2.imshow("blackrange",blackrange)   
    cv2.imshow(‘normalizeimg‘,np.uint8(normalizeimg*255))
    #cv2.waitKey(0)    
   
    cv2.imshow(‘original‘,img_original)
   
    cv2.imshow(‘seamlessCloneimg‘,seamlessCloneimg)    
    if cv2.waitKey(1)==ord(‘q‘):
        break
cv2.destroyAllWindows()
 

以上是关于createTrackbar的主要内容,如果未能解决你的问题,请参考以下文章