在PC上运行良好的opencv代码在Mac上不起作用

时间:2019-07-08 14:53:10

标签: python macos opencv

我使用openCV在我的实验室PC上编写了一个简单的图像分析代码功能,并且运行良好。由于某种原因,它无法在我的Mac上在家工作。弹出的图片(用于使用跟踪栏)冻结并且不响应。 有什么想法吗?

我试图找到解决方案,但找不到。

def leaf2score(file_name):


    #assign strings for ease of coding
    hh='Hue High'
    hl='Hue Low'
    sh='Saturation High'
    sl='Saturation Low'
    vh='Value High'
    vl='Value Low'
    cv2.namedWindow("Tracking") #creating a window named "Tracking"
    cv2.createTrackbar(hl, "Tracking", 0, 179, nothing)
    cv2.createTrackbar(sl, "Tracking", 0, 179, nothing)
    cv2.createTrackbar(vl, "Tracking", 0, 255, nothing)
    cv2.createTrackbar(hh, "Tracking", 255, 255, nothing)
    cv2.createTrackbar(sh, "Tracking", 255, 255, nothing)
    cv2.createTrackbar(vh, "Tracking", 255, 255, nothing)

    #assigning the trackbar values to control the masking of the img
    while True:
        img = cv2.imread(file_name) #reading the image using CV2 
        hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV) #transform the img from BGR to HSV

        frame=cv2.GaussianBlur(hsv,(5,5),0)

        l_h = cv2.getTrackbarPos(hl, "Tracking")
        l_s = cv2.getTrackbarPos(sl, "Tracking")
        l_v = cv2.getTrackbarPos(vl, "Tracking")

        u_h = cv2.getTrackbarPos(hh, "Tracking")
        u_s = cv2.getTrackbarPos(sh, "Tracking")
        u_v = cv2.getTrackbarPos(vh, "Tracking")

        l_green = np.array([l_h, l_s, l_v])  #determining the lower limit for color thresholding (hue, saturation, value)
        u_green = np.array([u_h, u_s, u_v])     #determining the upper limit for color thresholding

        mask = cv2.inRange(frame, l_green, u_green) #creating a mask for our filtration

        res = cv2.bitwise_and(frame, frame, mask=mask) #the RESULT image after applying the mask

        cv2.imshow("frame", frame) #showing each of the images throughout the process
        cv2.imshow("mask", mask)
        cv2.imshow("res", res)

        key = cv2.waitKey(1)  #close image windows
        if key == 27:
            break

    cv2.destroyAllWindows()

    bgr = cv2.cvtColor(res, cv2.COLOR_HSV2BGR) #converting the HSV back to BGR
    b,g,r = cv2.split(bgr) #Splitiing the channels of the image

    mean_g = cv2.mean(g)
    mean_r = cv2.mean(r)
    print(mean_g)
    print(mean_r)
    score = mean_g[0] + mean_r[0]  #calculating the score of the color by summing the green & red channels 


    return (score)

0 个答案:

没有答案