コード例 #1
0
def gaugeRead(frame):
    img_cont = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    #frame_m, frame_n = np.shape(img_cont)
    img_cont = cv2.filter2D(img_cont, -1, mean_kernel)
    _, img_bin = cv2.threshold(img_cont, 80, 255, cv2.THRESH_BINARY)
    imgOut(img_bin, 'bin.jpg')

    #提取边沿
    binary, contours, hierarchy = cv2.findContours(img_bin, cv2.RETR_TREE,
                                                   cv2.CHAIN_APPROX_SIMPLE)

    figIndex = 0
    locs = []
    for c in contours:
        area = cv2.contourArea(c)
        if (area > 1000 and area < 3000):
            cv2.drawContours(frame, c, -1, (0, 255, 0), 2)
            figIndex = figIndex + 1
            x, y, w, h = cv2.boundingRect(c)
            cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 0, 255), 2)
            numFig = img_bin[y:y + h, x:x + w]
            imgOut(numFig, str(figIndex) + '.jpg')

            keyPoint = (x + w / 2, y + h / 2)
            bottomPoint = y + h
            res, _ = Templ.match(numFig)
            if (res > -1):
                print([keyPoint, bottomPoint, res])
            #print(figIndex, ' - ', res, ' ', _)

    imgOut(frame, 'frame.jpg')
    '''
コード例 #2
0
 templ = templ[y:y+h, x:x+w]
 imgOut(templ, 'templ.jpg')
 
 locs = []
 figIndex = 0
 for c in cnts:
     area = cv2.contourArea(c)
     if (area < 1500 and area > 500):
         figIndex = figIndex+1
         x, y, w, h = cv2.boundingRect(c)
         cv2.rectangle(gauge, (x,y), (x+w, y+h), (0, 255, 0), 2)
         numFig = warped[y:y+h, x:x+w]
         
         keyPoint = (x+w/2, y+h/2)
         bottomPoint = y+h
         res = Templ.match(numFig)
         if (res != -1):
             #print('Times ', figIndex, ': ', res)
             imgOut(numFig, str(figIndex)+'.jpg')
             locs.append([keyPoint, bottomPoint, res])
 
 dial = []
 for i in range(np.shape(locs)[0]):
     PointA = locs[i][0]
     if PointA[0] > gauge_n/2:
         continue
     for j in range(i+1, np.shape(locs)[0]):
         PointB = locs[j][0]
         dist = np.sqrt(np.power(PointA[0]-PointB[0], 2) + 1.5*np.power(PointA[1]-PointB[1], 2))
         if (dist < 100):
             if (PointA[0] > PointB[0]):
コード例 #3
0
def gaugeRead(frame):
    img_cont = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    #frame_m, frame_n = np.shape(img_cont)
    #img_cont = cv2.filter2D(img_gray, -1, my_kernel)

    #提取水尺边沿
    edge_flag, edge = edgeDetect(img_cont, frame)
    if (edge_flag):
        #获得近似四边形
        epsilon = 0.02 * cv2.arcLength(edge, True)
        approx = cv2.approxPolyDP(edge, epsilon, True)
        x, y, w, h = cv2.boundingRect(approx)

        for p in approx:
            cv2.circle(frame, (p[0][0], p[0][1]), 10, (0, 255, 0), -1)
        #cv2.rectangle(frame, (x,y), (x+w, y+h), (0, 0, 255), 2)
        #imgOut(frame, 'frame.jpg')

        #自适应四个顶点
        if (np.shape(approx)[0] >= 4):
            mindis = getDist((x, y), (x + w, y + h))

            #cv2.circle(frame, (x+w-1, y), 10, (0, 255, 0), -1)
            #imgOut(frame, 'frame.jpg')

            for point in approx:
                dis = getDist(point[0], (x + w - 1, y))
                if dis < mindis:
                    mindis = dis
                    ex1 = point[0][0]
                    ey1 = point[0][1]

            mindis = getDist((x, y), (x + w, y + h))
            for point in approx:
                dis = getDist(point[0], (x + w - 1, y + h - 1))
                if dis < mindis:
                    mindis = dis
                    ex2 = point[0][0]
                    ey2 = point[0][1]

            mindis = getDist((x, y), (x + w, y + h))
            for point in approx:
                dis = getDist(point[0], (x, y + h - 1))
                if dis < mindis:
                    mindis = dis
                    ex3 = point[0][0]
                    ey3 = point[0][1]

            mindis = getDist((x, y), (x + w, y + h))
            for point in approx:
                dis = getDist(point[0], (x, y))
                if dis < mindis:
                    mindis = dis
                    ex4 = point[0][0]
                    ey4 = point[0][1]

        else:
            return (False, 'NA', 'No edge')

        #计算变换矩阵
        maxlength = int(
            max(np.sqrt(np.power(ex1 - ex2, 2) + np.power(ey1 - ey2, 2)),
                np.sqrt(np.power(ex3 - ex4, 2) + np.power(ey3 - ey4, 2))))
        maxwidth = int(
            max(np.sqrt(np.power(ex3 - ex2, 2) + np.power(ey3 - ey2, 2)),
                np.sqrt(np.power(ex1 - ex4, 2) + np.power(ey1 - ey4, 2))))
        dst = np.array([[maxwidth - 1, 0], [maxwidth - 1, maxlength - 1],
                        [0, maxlength - 1], [0, 0]],
                       dtype="float32")
        rect = np.array([[ex1, ey1], [ex2, ey2], [ex3, ey3], [ex4, ey4]],
                        dtype="float32")
        M = cv2.getPerspectiveTransform(rect, dst)
        #将水尺变换为矩形
        gauge = cv2.warpPerspective(frame, M, (maxwidth, maxlength))
        gauge_m, gauge_n, _ = np.shape(gauge)
        #二值化
        warped = cv2.cvtColor(gauge, cv2.COLOR_BGR2GRAY)
        warped = cv2.filter2D(warped, -1, sharp_kernel)
        _, warped = cv2.threshold(warped, 210, 255, cv2.THRESH_BINARY_INV)
        #腐蚀膨胀
        warped = cv2.dilate(warped, erode_kernel, iterations=1)
        mor_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (4, 4))
        warped = cv2.morphologyEx(warped, cv2.MORPH_CLOSE, mor_kernel)
        warped = cv2.morphologyEx(warped, cv2.MORPH_OPEN, mor_kernel)
        _, warped = cv2.threshold(warped, 210, 255, cv2.THRESH_BINARY_INV)
        #提取边沿
        binary, cnts, hierarchy = cv2.findContours(warped, cv2.RETR_TREE,
                                                   cv2.CHAIN_APPROX_SIMPLE)
        #提取数字的边沿
        locs = []
        figIndex = 0
        for c in cnts:
            area = cv2.contourArea(c)
            if (area < 1500 and area > 500):
                figIndex = figIndex + 1
                x, y, w, h = cv2.boundingRect(c)
                cv2.rectangle(gauge, (x, y), (x + w, y + h), (0, 255, 0), 2)
                numFig = warped[y:y + h, x:x + w]
                #设置关键点、最底点和识别结果
                keyPoint = (x + w / 2, y + h / 2)
                bottomPoint = y + h
                res, _ = Templ.match(numFig)
                #imgOut(numFig, str(figIndex)+'.jpg')
                #在约束范围内的数字储存在数组中
                if (res != -1 and keyPoint[0] > 50
                        and keyPoint[0] < gauge_n / 2):
                    locs.append([keyPoint, bottomPoint, res])
        #imgOut(gauge, 'gauge.jpg')
        #生成比例尺
        dial = []
        for i in range(np.shape(locs)[0]):
            PointA = locs[i][0]
            for j in range(i + 1, np.shape(locs)[0]):
                PointB = locs[j][0]
                dist = np.sqrt(
                    np.power(PointA[0] - PointB[0], 2) +
                    10 * np.power(PointA[1] - PointB[1], 2))
                #当距离小于最大距离
                if (dist < 80):
                    #print(dist)
                    if (PointA[0] > PointB[0]):
                        index_i = j
                        index_j = i
                    else:
                        index_i = i
                        index_j = j
                    num = locs[index_i][2] + 0.1 * locs[index_j][2]
                    point = locs[index_i][1]
                    dial.append([point, num])
        #计算结果
        numA = 0
        numB = 0
        if (np.shape(dial)[0] >= 2):
            for i in range(np.shape(dial)[0]):
                if (dial[i][1] == 3.4 or dial[i][1] == 3.2 or dial[i][1] == 3.0
                        or dial[i][1] == 2.8):
                    if (numA == 0):
                        numA = dial[i][1]
                        potA = dial[i][0]
                        continue
                    if (numB == 0):
                        numB = dial[i][1]
                        potB = dial[i][0]
                        continue
                    break

            if (numA == 0 or numB == 0):
                return (False, 'NA', 'No Dial')

            ans = numA - 0.92 * (gauge_m - potA) * (numA - numB) / (potB -
                                                                    potA)
            return (True, ans, dial)
        else:
            return (False, 'NA', 'No Dial')

        #cv2.drawContours(gauge, locs, -1, (0, 255, 0), 2)

        #imgOut(gauge, 'gauge.jpg')
        #imgOut(warped, 'warped.jpg')

    else:
        return (False, 'NA', 'No edge')
コード例 #4
0
def reader_m2(gauge):
    gauge_m, gauge_n, _ = np.shape(gauge)

    #二值化
    warped = cv2.cvtColor(gauge, cv2.COLOR_BGR2GRAY)
    warped = cv2.filter2D(warped, -1, mean_kernel)
    _, warped = cv2.threshold(warped, 210, 255, cv2.THRESH_BINARY_INV)
    #腐蚀膨胀
    mor_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (4, 4))
    warped = cv2.morphologyEx(warped, cv2.MORPH_CLOSE, mor_kernel)
    warped = cv2.morphologyEx(warped, cv2.MORPH_OPEN, mor_kernel)
    _, warped = cv2.threshold(warped, 127, 255, cv2.THRESH_BINARY_INV)
    #提取边沿
    binary, cnts, hierarchy = cv2.findContours(warped, cv2.RETR_TREE,
                                               cv2.CHAIN_APPROX_SIMPLE)
    #提取数字的边沿
    locs = []
    nums = []
    figIndex = 0
    for c in cnts:
        area = cv2.contourArea(c)

        if (area < 1500 and area > 500):
            figIndex = figIndex + 1
            x, y, w, h = cv2.boundingRect(c)
            cv2.rectangle(gauge, (x, y), (x + w, y + h), (0, 255, 0), 2)
            numFig = warped[y:y + h, x:x + w]
            #设置关键点、最底点和识别结果
            keyPoint = (x + w / 2, y + h / 2)
            bottomPoint = y + h
            res, _ = Templ.match(numFig)
            #在约束范围内的数字储存在数组中
            if (res != -1 and keyPoint[0] > 110 and keyPoint[0] < gauge_n / 2):
                nums.append([keyPoint, bottomPoint, res])
            locs.append([keyPoint, bottomPoint, res])
    #生成比例尺

    if (np.shape(nums)[0] <= 0):
        return (False, 'No Dial', 'NA')

    dial = []
    numId = nums[0]
    minDis = gauge_m
    minIndex = -1
    for i in range(np.shape(locs)[0]):
        PointA = locs[i]
        if (numId[1] - PointA[1] > 0 and numId[1] - PointA[1] < minDis):
            minIndex = i
            minDis = numId[1] - PointA[1]
    dial.append([numId[1], 2 + 0.1 * numId[2]])
    dial.append([locs[minIndex][1], 2 + 0.1 * numId[2] + 0.1])

    #print(dial)
    #计算结果
    potA = dial[0][0]
    numA = dial[0][1]
    potB = dial[1][0]
    numB = dial[1][1]
    if (potB - potA != 0):
        ans = numA - 0.92 * (gauge_m - potA) * (numA - numB) / (potB - potA)
        return (True, ans, dial)
    else:
        return (False, 'No Dial', 'NA')
コード例 #5
0
def reader_m1(gauge):
    gauge_m, gauge_n, _ = np.shape(gauge)
    #二值化
    warped = cv2.cvtColor(gauge, cv2.COLOR_BGR2GRAY)
    warped = cv2.filter2D(warped, -1, sharp_kernel)
    _, warped = cv2.threshold(warped, 210, 255, cv2.THRESH_BINARY_INV)
    #腐蚀膨胀
    warped = cv2.dilate(warped, erode_kernel, iterations=1)
    mor_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (4, 4))
    warped = cv2.morphologyEx(warped, cv2.MORPH_CLOSE, mor_kernel)
    warped = cv2.morphologyEx(warped, cv2.MORPH_OPEN, mor_kernel)
    _, warped = cv2.threshold(warped, 210, 255, cv2.THRESH_BINARY_INV)
    #提取边沿
    binary, cnts, hierarchy = cv2.findContours(warped, cv2.RETR_TREE,
                                               cv2.CHAIN_APPROX_SIMPLE)
    #提取数字的边沿
    locs = []
    figIndex = 0
    for c in cnts:
        area = cv2.contourArea(c)
        if (area < 1500 and area > 500):
            figIndex = figIndex + 1
            x, y, w, h = cv2.boundingRect(c)
            cv2.rectangle(gauge, (x, y), (x + w, y + h), (0, 255, 0), 2)
            numFig = warped[y:y + h, x:x + w]
            #设置关键点、最底点和识别结果
            keyPoint = (x + w / 2, y + h / 2)
            bottomPoint = y + h
            res, _ = Templ.match(numFig)
            #在约束范围内的数字储存在数组中
            if (res != -1 and keyPoint[0] > 50 and keyPoint[0] < gauge_n / 2):
                locs.append([keyPoint, bottomPoint, res])
    #生成比例尺
    dial = []
    for i in range(np.shape(locs)[0]):
        PointA = locs[i][0]
        for j in range(i + 1, np.shape(locs)[0]):
            PointB = locs[j][0]
            dist = np.sqrt(
                np.power(PointA[0] - PointB[0], 2) +
                10 * np.power(PointA[1] - PointB[1], 2))
            #当距离小于最大距离
            if (dist < 80):
                #print(dist)
                if (PointA[0] > PointB[0]):
                    index_i = j
                    index_j = i
                else:
                    index_i = i
                    index_j = j
                num = locs[index_i][2] + 0.1 * locs[index_j][2]
                point = locs[index_i][1]
                dial.append([point, num])
    #计算结果
    numA = 0
    numB = 0
    if (np.shape(dial)[0] >= 2):
        for i in range(np.shape(dial)[0]):
            if (dial[i][1] == 3.4 or dial[i][1] == 3.2 or dial[i][1] == 3.0
                    or dial[i][1] == 2.8):
                if (numA == 0):
                    numA = dial[i][1]
                    potA = dial[i][0]
                    continue
                if (numB == 0):
                    numB = dial[i][1]
                    potB = dial[i][0]
                    continue
                break

        if (numA == 0 or numB == 0):
            return (False, 'No Dial', 'NA')

        ans = numA - 0.92 * (gauge_m - potA) * (numA - numB) / (potB - potA)
        return (True, ans, dial)
    else:
        return (False, 'No Dial', 'NA')
コード例 #6
0
def gaugeRead(frame):
    img_cont = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    imgOut(frame, 'frame.jpg')
    #frame_m, frame_n = np.shape(img_cont)
    #img_cont = cv2.filter2D(img_cont, -1, sharp_kernel)
    #img_cont = cv2.Canny(img_cont, 50, 100)
    #imgOut(img_cont, 'cont.jpg')
    #提取水尺边沿
    edge_flag, edge = edgeDetect(img_cont, frame)
    if (edge_flag):
        #获得近似四边形
        epsilon = 0.02 * cv2.arcLength(edge, True)
        approx = cv2.approxPolyDP(edge, epsilon, True)
        x, y, w, h = cv2.boundingRect(approx)

        #for p in approx:
        #    cv2.circle(frame, (p[0][0], p[0][1]), 10, (0, 0, 255), -1)
        #cv2.rectangle(frame, (x,y), (x+w, y+h), (0, 0, 255), 2)
        #imgOut(frame, 'frame.jpg')

        #自适应四个顶点
        if (np.shape(approx)[0] >= 4):
            mindis = getDist((x, y), (x + w, y + h))

            #cv2.circle(frame, (x+w-1, y), 10, (0, 255, 0), -1)
            for point in approx:
                dis = getDist(point[0], (x + w - 1, y))
                if dis < mindis:
                    mindis = dis
                    ex1 = point[0][0]
                    ey1 = point[0][1]

            mindis = getDist((x, y), (x + w, y + h))
            for point in approx:
                dis = getDist(point[0], (x + w - 1, y + h - 1))
                if dis < mindis:
                    mindis = dis
                    ex2 = point[0][0]
                    ey2 = point[0][1]

            mindis = getDist((x, y), (x + w, y + h))
            for point in approx:
                dis = getDist(point[0], (x, y + h - 1))
                if dis < mindis:
                    mindis = dis
                    ex3 = point[0][0]
                    ey3 = point[0][1]

            mindis = getDist((x, y), (x + w, y + h))
            for point in approx:
                dis = getDist(point[0], (x, y))
                if dis < mindis:
                    mindis = dis
                    ex4 = point[0][0]
                    ey4 = point[0][1]

            #cv2.rectangle(frame, (ex4, ey4), (ex2, ey2), (0, 0, 255), 2)
            #imgOut(frame, 'frame.jpg')

        else:
            return (False, 'NA', 'No edge')

        #计算变换矩阵
        maxlength = int(
            max(np.sqrt(np.power(ex1 - ex2, 2) + np.power(ey1 - ey2, 2)),
                np.sqrt(np.power(ex3 - ex4, 2) + np.power(ey3 - ey4, 2))))
        maxwidth = int(
            max(np.sqrt(np.power(ex3 - ex2, 2) + np.power(ey3 - ey2, 2)),
                np.sqrt(np.power(ex1 - ex4, 2) + np.power(ey1 - ey4, 2))))
        dst = np.array([[maxwidth - 1, 0], [maxwidth - 1, maxlength - 1],
                        [0, maxlength - 1], [0, 0]],
                       dtype="float32")
        rect = np.array([[ex1, ey1], [ex2, ey2], [ex3, ey3], [ex4, ey4]],
                        dtype="float32")
        M = cv2.getPerspectiveTransform(rect, dst)
        #将水尺变换为矩形
        gauge = cv2.warpPerspective(frame, M, (maxwidth, maxlength))
        gauge_m, gauge_n, _ = np.shape(gauge)

        #二值化
        warped = cv2.cvtColor(gauge, cv2.COLOR_BGR2GRAY)
        #warped = np.uint8(np.clip((1.2*warped - 50), 0, 255))
        warped = cv2.filter2D(warped, -1, mean_kernel)
        imgOut(warped, 'sharp.jpg')
        _, warped = cv2.threshold(warped, 210, 255, cv2.THRESH_BINARY_INV)
        #腐蚀膨胀
        #warped = cv2.dilate(warped, erode_kernel, iterations = 1)
        mor_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (4, 4))
        warped = cv2.morphologyEx(warped, cv2.MORPH_CLOSE, mor_kernel)
        warped = cv2.morphologyEx(warped, cv2.MORPH_OPEN, mor_kernel)
        _, warped = cv2.threshold(warped, 127, 255, cv2.THRESH_BINARY_INV)
        imgOut(warped, 'warped.jpg')
        #提取边沿
        binary, cnts, hierarchy = cv2.findContours(warped, cv2.RETR_TREE,
                                                   cv2.CHAIN_APPROX_SIMPLE)
        #提取数字的边沿
        locs = []
        nums = []
        figIndex = 0
        for c in cnts:
            area = cv2.contourArea(c)

            if (area < 1500 and area > 500):
                figIndex = figIndex + 1
                x, y, w, h = cv2.boundingRect(c)
                cv2.rectangle(gauge, (x, y), (x + w, y + h), (0, 255, 0), 2)
                numFig = warped[y:y + h, x:x + w]
                imgOut(numFig, str(figIndex) + '.jpg')
                #设置关键点、最底点和识别结果
                keyPoint = (x + w / 2, y + h / 2)
                bottomPoint = y + h
                res, _ = Templ.match(numFig)
                #imgOut(numFig, str(figIndex)+'.jpg')
                #在约束范围内的数字储存在数组中
                if (res != -1 and keyPoint[0] > 110
                        and keyPoint[0] < gauge_n / 2):
                    nums.append([keyPoint, bottomPoint, res])
                    #print(figIndex, res, _, keyPoint)
                locs.append([keyPoint, bottomPoint, res])
        #print(locs)
        imgOut(gauge, 'gauge.jpg')
        #生成比例尺

        if (np.shape(nums)[0] <= 0):
            return (False, 'NA', 'No Dial')

        dial = []
        numId = nums[0]
        minDis = gauge_m
        minIndex = -1
        for i in range(np.shape(locs)[0]):
            PointA = locs[i]
            if (numId[1] - PointA[1] > 0 and numId[1] - PointA[1] < minDis):
                minIndex = i
                minDis = numId[1] - PointA[1]
        dial.append([numId[1], 2 + 0.1 * numId[2]])
        dial.append([locs[minIndex][1], 2 + 0.1 * numId[2] + 0.1])

        #print(dial)
        #计算结果
        potA = dial[0][0]
        numA = dial[0][1]
        potB = dial[1][0]
        numB = dial[1][1]
        if (potB - potA != 0):
            ans = numA - 0.92 * (gauge_m - potA) * (numA - numB) / (potB -
                                                                    potA)
            return (True, ans, dial)
        else:
            return (False, 'NA', 'No Dial')

        #cv2.drawContours(gauge, locs, -1, (0, 255, 0), 2)

        #imgOut(gauge, 'gauge.jpg')
        #imgOut(warped, 'warped.jpg')

    else:
        return (False, 'NA', 'No edge')
コード例 #7
0
    locs = []
    figIndex = 0
    for c in cnts:
        area = cv2.contourArea(c)
        if (area < 1500 and area > 500):
            figIndex = figIndex + 1
            x, y, w, h = cv2.boundingRect(c)
            cv2.rectangle(gauge, (x, y), (x + w, y + h), (0, 255, 0), 2)
            numFig = warped[y:y + h, x:x + w]

            ##print('Figure: ', figIndex)
            imgOut(numFig, str(figIndex) + '.jpg')

            keyPoint = (x + w / 2, y + h / 2)
            bottomPoint = y + h
            res, _ = Templ.match(numFig)
            #print('    Result: ', res)

            if (res != -1 and keyPoint[0] > 50 and keyPoint[0] < gauge_n / 2):
                locs.append([keyPoint, bottomPoint, res])

    dial = []
    for i in range(np.shape(locs)[0]):
        PointA = locs[i][0]
        for j in range(i + 1, np.shape(locs)[0]):
            PointB = locs[j][0]
            dist = np.sqrt(
                np.power(PointA[0] - PointB[0], 2) +
                10 * np.power(PointA[1] - PointB[1], 2))
            if (dist < 80):
                #print(dist)