Python cv2 模块,THRESH_TOZERO_INV 实例源码

我们从Python开源项目中,提取了以下3个代码示例,用于说明如何使用cv2.THRESH_TOZERO_INV

项目:python-image-processing    作者:karaage0703    | 项目源码 | 文件源码
def extract_color( src, h_th_low, h_th_up, s_th, v_th ):
    hsv = cv2.cvtColor(src, cv2.COLOR_BGR2HSV)
    h, s, v = cv2.split(hsv)
    if h_th_low > h_th_up:
        ret, h_dst_1 = cv2.threshold(h, h_th_low, 255, cv2.THRESH_BINARY) 
        ret, h_dst_2 = cv2.threshold(h, h_th_up,  255, cv2.THRESH_BINARY_INV)

        dst = cv2.bitwise_or(h_dst_1, h_dst_2)
    else:
        ret, dst = cv2.threshold(h,   h_th_low, 255, cv2.THRESH_TOZERO) 
        ret, dst = cv2.threshold(dst, h_th_up,  255, cv2.THRESH_TOZERO_INV)
        ret, dst = cv2.threshold(dst, 0, 255, cv2.THRESH_BINARY)

    ret, s_dst = cv2.threshold(s, s_th, 255, cv2.THRESH_BINARY)
    ret, v_dst = cv2.threshold(v, v_th, 255, cv2.THRESH_BINARY)
    dst = cv2.bitwise_and(dst, s_dst)
    dst = cv2.bitwise_and(dst, v_dst)
    return dst
项目:CoffeeRobot    作者:ciauri    | 项目源码 | 文件源码
def houghlines(im,h):
    #im = cv2.imread('2.jpg')
    #ret,gray = cv2.threshold(im,40,255,cv2.THRESH_TOZERO_INV)
    #gray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
    #edges = cv2.Canny(gray,10,200)
    def getKey(item):
        return abs(item[1]-item[3])
    edges = r(im)
    lines = cv2.HoughLines(edges,20,np.pi/190,100)
    horizontal = []
    for line in lines:
        for rho,theta in line:
            a = np.cos(theta)
            b = np.sin(theta)
            x0 = a*rho
            y0 = b*rho
            x1 = int(x0 + 1000*(-b))   # Here i have used int() instead of rounding the decimal value, so 3.8 --> 3
            y1 = int(y0 + 1000*(a))    # But if you want to round the number, then use np.around() function, then 3.8 --> 4.0
            x2 = int(x0 - 1000*(-b))   # But we need integers, so use int() function after that, ie int(np.around(x))
            y2 = int(y0 - 1000*(a))
            #cv2.line(im,(x1,y1),(x2,y2),(0,255,0),2)
            #print(str(x1) + " " + str(y1) + " " + str(x2) + " " + str(y2))
            horizontal.append((x1,y1,x2,y2))

            #cv2.imshow('houghlines',im)
            #cv2.waitKey(0)
            #cv2.destroyAllWindows()
    horizontal = sorted(horizontal,key=getKey)
    i = 0
    votes = 0
    while True:
        cv2.line(im,(horizontal[i][0],horizontal[i][1]),(horizontal[i][2],horizontal[i][3]),(200,0,0),2)

        average = (horizontal[i][1]+horizontal[i][3])/2.0
        percent = average/h
        actual = 100-(percent*100)
        if actual > 80:
            i += 1
            print(actual)
        elif actual < 25:
            print(actual)
            votes +=1
            i +=1
        elif actual <30:
            print("the coffee pot is getting low " + str(actual) + "% full!")
            return votes,actual
        else:
            print("the coffee pot is " + str(actual) + "% full!")
            return votes,actual
项目:CoffeeRobot    作者:ciauri    | 项目源码 | 文件源码
def houghlines(im,h):
    #im = cv2.imread('2.jpg')
    #ret,gray = cv2.threshold(im,40,255,cv2.THRESH_TOZERO_INV)
    #gray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
    #edges = cv2.Canny(gray,10,200)
    def getKey(item):
        return abs(item[1]-item[3])
    edges = r(im)
    lines = cv2.HoughLines(edges,20,np.pi/190,100)
    horizontal = []
    for line in lines:
        for rho,theta in line:
            a = np.cos(theta)
            b = np.sin(theta)
            x0 = a*rho
            y0 = b*rho
            x1 = int(x0 + 1000*(-b))   # Here i have used int() instead of rounding the decimal value, so 3.8 --> 3
            y1 = int(y0 + 1000*(a))    # But if you want to round the number, then use np.around() function, then 3.8 --> 4.0
            x2 = int(x0 - 1000*(-b))   # But we need integers, so use int() function after that, ie int(np.around(x))
            y2 = int(y0 - 1000*(a))
            #cv2.line(im,(x1,y1),(x2,y2),(0,255,0),2)
            #print(str(x1) + " " + str(y1) + " " + str(x2) + " " + str(y2))
            horizontal.append((x1,y1,x2,y2))

            #cv2.imshow('houghlines',im)
            #cv2.waitKey(0)
            #cv2.destroyAllWindows()
    horizontal = sorted(horizontal,key=getKey)
    i = 0
    while True:
        cv2.line(im,(horizontal[i][0],horizontal[i][1]),(horizontal[i][2],horizontal[i][3]),(200,0,0),2)
        cv2.imshow('houghlines',im)
        cv2.waitKey(0)
        cv2.destroyAllWindows()
        cv2.imwrite("line.jpg",im)
        average = (horizontal[i][1]+horizontal[i][3])/2.0
        percent = average/h
        actual = 100-(percent*100)
        if actual > 80 or actual < 20:
            i += 1
            print(actual)
        elif actual <30:
            print("the coffee pot is getting low " + str(actual) + "% full!")

        else:
            print("the coffee pot is " + str(actual) + "% full!")
            onlineCoffee.updateCoffeeSite("The coffee pot is " + str(int(actual)) + "% full!")
            break