def get_color(frame, color1, color2):
    hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
    color_dict = colorList.getColorList()
    mask1 = cv2.inRange(hsv, color_dict[color1][0], color_dict[color1][1])
    mask2 = cv2.inRange(hsv, color_dict[color2][0], color_dict[color2][1])
    mask = mask1 + mask2
    cv2.imwrite(filename + color1 + color2 + '.jpg', mask)
Beispiel #2
0
    def findcolor(self, takeHSVFrame):

        night = 0
        color_dict = colorList.getColorList()
        # hsv = cv2.cvtColor(cutframe, cv2.COLOR_BGR2HSV)
        maxsum = 0
        color = 'None'
        d = 'red'
        # image = cutframe.copy()
        mask = cv2.inRange(takeHSVFrame, color_dict[d][0], color_dict[d][1])
        # cv2.imwrite(d + '.jpg', mask)
        binary = cv2.threshold(mask, 127, 255, cv2.THRESH_BINARY)[1]
        binary = cv2.dilate(binary, None, iterations=2)
        cnts, hiera = cv2.findContours(binary, cv2.RETR_EXTERNAL,
                                       cv2.CHAIN_APPROX_SIMPLE)

        # cv2.imshow("lunkuo",aa)
        # cv2.waitKey(1000)

        sum = 0
        for c in cnts:
            sum += cv2.contourArea(c)
        if sum < 100:
            night = 1
        return night
def get_color(frame):
    print('go in get_color')
    rate = {}
    hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
    color = None
    color_dict = colorList.getColorList()
    sum = 0
    for d in color_dict:
        mask = cv2.inRange(hsv, color_dict[d][0], color_dict[d][1])
        # cv2.imwrite(d + '.jpg', mask)
        binary = cv2.threshold(mask, 127, 255, cv2.THRESH_BINARY)[1]
        binary = cv2.dilate(binary, None, iterations=2)
        cnts, hiera = cv2.findContours(binary.copy(), cv2.RETR_EXTERNAL,
                                       cv2.CHAIN_APPROX_SIMPLE)
        single_color = 0
        for c in cnts:
            single_color += cv2.contourArea(c)
        if d == "red" or d == "white":
            pass
        else:
            sum += single_color
            rate[d] = single_color

    for i in rate:
        rate[i] = rate[i] / sum * 100
    return rate
Beispiel #4
0
def find_the_maximum_color(frame):
    color_dict = colorList.getColorList()
    hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
    mask = cv2.inRange(hsv, color_dict[get_color(frame)][0], color_dict[get_color(frame)][1])  # find the maximum color
    a = color_dict[get_color(frame)][0]
    b = color_dict[get_color(frame)][1]
    cap.release()
    print('find it !')
    return a,b
def get_color(frame, classFolder, imgName):
    #print('go in get_color')
    hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)

    color_dict = colorList.getColorList()
    for color in color_dict:
        mask = cv2.inRange(hsv, color_dict[color][0], color_dict[color][1])
        fileName = os.path.join(classFolder, color)
        cv2.imwrite(os.path.join(fileName, imgName[:-4] + '.jpg'), mask)

    return
Beispiel #6
0
def getColor(frame):
    hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
    maxsum = 0
    color = None
    color_dict = colorList.getColorList()

    # 对每个颜色进行判断
    for d in color_dict:
        # 根据阈值构建掩膜
        mask = cv2.inRange(hsv, color_dict[d][0], color_dict[d][1])
        # 腐蚀操作
        mask = cv2.erode(mask, None, iterations=2)
        # 膨胀操作,其实先腐蚀再膨胀的效果是开运算,去除噪点
        mask = cv2.dilate(mask, None, iterations=2)
        img, cnts, hiera = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL,
                                            cv2.CHAIN_APPROX_SIMPLE)

        # 有轮廓才进行后面的判断
        if len(cnts) > 0:
            # 计算识别区域的面积
            sum = 0
            for c in cnts:
                sum += cv2.contourArea(c)

            # 找到最大面积并找到质心
            if sum > maxsum:
                maxsum = sum
                if maxsum != 0:
                    color = d
                else:
                    color = None
                # 找到面积最大的轮廓
                c = max(cnts, key=cv2.contourArea)
                # 确定面积最大的轮廓的外接圆
                ((x, y), radius) = cv2.minEnclosingCircle(c)
                # 计算轮廓的矩
                M = cv2.moments(c)
                # 计算质心
                center = (int(M["m10"] / M["m00"]), int(M["m01"] / M["m00"]))

    return color, center
Beispiel #7
0
def get_color(frame):
    print('go in get_color')
    hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
    maxsum = -100
    color = None
    color_dict = colorList.getColorList()
    for d in color_dict:
        mask = cv2.inRange(hsv, color_dict[d][0], color_dict[d][1])
        cv2.imwrite(d + '.jpg', mask)
        binary = cv2.threshold(mask, 127, 255, cv2.THRESH_BINARY)[1]
        binary = cv2.dilate(binary, None, iterations=2)
        img, cnts, hiera = cv2.findContours(binary.copy(), cv2.RETR_EXTsERNAL,
                                            cv2.CHAIN_APPROX_SIMPLE)
        sum = 0
        for c in cnts:
            sum += cv2.contourArea(c)
        if sum > maxsum:
            maxsum = sum
            color = d

    return color
def get_color_samefolder(frame, classFolder, imgName):
    #print('go in get_color')
    hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)

    color_dict = colorList.getColorList()
    for color in color_dict:
        mask = cv2.inRange(hsv, color_dict[color][0], color_dict[color][1])
        #fileName = os.path.join(classFolder, color)
        fileName = classFolder
        binary = cv2.threshold(mask, 127, 255, cv2.THRESH_BINARY)[1]
        binary = cv2.dilate(binary, None, iterations=2)
        img, cnts, hiera = cv2.findContours(binary.copy(), cv2.RETR_EXTERNAL,
                                            cv2.CHAIN_APPROX_SIMPLE)
        sum = 0
        for c in cnts:
            sum += cv2.contourArea(c)
        if sum >= 20000:
            cv2.imwrite(os.path.join(fileName, imgName[:-4] + color + '.jpg'),
                        mask)

    return
Beispiel #9
0
def get_color_structure(filename):
    frame = cv2.imread(filename)
    arr = []
    hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
    min_count = 10000
    color_dict = colorList.getColorList()
    for d in color_dict:
        mask = cv2.inRange(hsv, color_dict[d][0], color_dict[d][1])
        binary = cv2.threshold(mask, 127, 255, cv2.THRESH_BINARY)[1]
        binary = cv2.dilate(binary, None, iterations=2)
        # img, cnts, hiera = cv2.findContours(binary.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
        cnts, hiera = cv2.findContours(binary.copy(), cv2.RETR_EXTERNAL,
                                       cv2.CHAIN_APPROX_SIMPLE)
        sum = 0
        for c in cnts:
            sum += cv2.contourArea(c)
        # print("%s: %s" % (d, str(sum)))
        # if len(cnts) > 0:
        #     cv2.imwrite('./output/' + d + '.jpg', mask)
        if sum > min_count:
            arr.append(d)
    return arr
Beispiel #10
0
def get_color(path):
    frame = cv2.imread(path)

    #area = frame.shape[0]*frame.shape[1]
    hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
    areadic = {}
    color_dict = colorList.getColorList()
    for d in color_dict:
        mask = cv2.inRange(hsv, color_dict[d][0], color_dict[d][1])
        #cv2.imwrite(d+'.jpg',mask)
        #binary = cv2.threshold(mask, 0, 255, cv2.THRESH_BINARY+cv2.THRESH_OTSU)[1]
        #binary = cv2.adaptiveThreshold(mask,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY,11,2)
        binary = cv2.threshold(mask, 127, 255, cv2.THRESH_BINARY)[1]
        # cv2.imwrite(d+'.png',binary)
        #kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5, 5))
        #binary = cv2.morphologyEx(binary, cv2.MORPH_OPEN, kernel)  # 开运算
        binary = cv2.dilate(binary, None, iterations=0)
        img, cnts, hiera = cv2.findContours(binary.copy(), cv2.RETR_EXTERNAL,
                                            cv2.CHAIN_APPROX_SIMPLE)
        # cv2.imwrite(d+'.png',img)
        sum = 0
        for c in cnts:
            sum += cv2.contourArea(c)
        areadic[d] = sum

    # areadic['Red'] += areadic['Red2']
    # del areadic['Red2']

    area = 0
    for c in areadic.keys():
        area += areadic[c]
    if area == 0:
        return 0
    for c in areadic.keys():
        areadic[c] = round(areadic[c] / area, 2)

    #sorted_area = sorted(areadic.items(), key=lambda d: d[1], reverse=True)
    return areadic
Beispiel #11
0
                print('← ', count, ' times')  #Move left
                count = 1
        if ans_lj[i] == 'R':
            if i + 1 == len(ans_lj):
                print('→ ', count, ' times')
                break
            if ans_lj[i + 1] == ans_lj[i]:
                count += 1
            else:
                print('→ ', count, ' times')  #Move right
                count = 1


if __name__ == '__main__':
    cost = time.time()
    color_dict = colorList.getColorList()
    frame = cv2.imread(filename, 1)
    maze = get_rid_of_color(frame)  #create maze graph
    res = mazeMatrix(maze)  #resize maze

    ball = get_ball_color(frame)  #get ball
    ball_maze = mazeMatrix(ball)
    bx, by = ballMatrix(ball_maze)  #find ball coord
    print('The coord of ball is', bx, by)

    #res[6][0]=1
    #res[6][1]=1
    ans = bfs(res)
    if ans == False: print("No way to get out of this maze")
    else:
        print('There are', ans, 'steps to get out')