예제 #1
0
    def post_process1(self, image, mask):
        w, h = image.size
        img = cv2.cvtColor(np.asarray(image), cv2.COLOR_RGB2BGR)
        contours, pmask = self.find_contours(image, mask)
        result = {}
        rect = order_points(contours)
        if len(contours) > 500:
            lt, rt, rb, lb = rect
            if abs(lt[1] - rt[1]) > 5 or abs(rb[1] - lb[1]) > 5:
                xb1, yb1, xb2, yb2 = lb[0], lb[1], rb[0], rb[1]
                xt1, yt1, xt2, yt2 = lt[0], lt[1], rt[0], rt[1]
                if abs(yb1 - yb2) > abs(yt1 - yt2):
                    pts1 = np.float32([lt, lb, rt, rb])
                    if yb1 > yb2:
                        pts2 = np.float32([lt, lb, rt, [rb[0], lb[1]]])
                    else:
                        pts2 = np.float32([lt, [lb[0], rb[1]], rt, rb])
                    M = cv2.getPerspectiveTransform(pts1, pts2)
                    warp_img = cv2.warpPerspective(img, M, (w, h))
                else:
                    pts1 = np.float32([lt, lb, rt, rb])
                    if yt1 < yt2:
                        pts2 = np.float32([lt, lb, [rt[0], lt[1]], rb])
                    else:
                        pts2 = np.float32([[lt[0], rt[1]], lb, rt, rb])
                    M = cv2.getPerspectiveTransform(pts1, pts2)
                    warp_img = cv2.warpPerspective(img, M, (w, h))
                result = {
                    'flag': 1,
                    'warp_M': M,
                    'keyboard_rect': None,
                    'warp_img': warp_img
                }
            else:
                lr, rt, rb, lb = rect
                sx, ex = int(min(lt[0], lb[0])), int(max(rt[0], rb[0]))
                sy, ey = int(min(lt[1], rt[1])), int(max(lb[1], rb[1]))
                flag, keyboard_rect = self.find_rect(pmask, sx, sy, ex, ey)
                result = {
                    'flag': flag,
                    'warp_M': None,
                    'keyboard_rect': keyboard_rect,
                    'warp_img': None
                }

        else:
            result = {
                'flag': 0,
                'warp_M': None,
                'keyboard_rect': None,
                'warp_img': None
            }
        return result
예제 #2
0
    def post_process(self, image, mask):
        w, h = image.size
        img = cv2.cvtColor(np.asarray(image), cv2.COLOR_RGB2BGR)
        contours, pmask = self.find_contours(image, mask)
        # cv2.imwrite('./pmask.jpg',pmask)
        # embed()
        result = {}
        if len(contours) > 500:
            rect = order_points(contours)
            lt, rt, rb, lb = rect
            if abs(lt[1] - rt[1]) > 5 or abs(rb[1] - lb[1]) > 5:
                xb1, yb1, xb2, yb2 = lb[0], lb[1], rb[0], rb[1]
                xt1, yt1, xt2, yt2 = lt[0], lt[1], rt[0], rt[1]
                center = (w // 2, h // 2)
                if abs(yb1 - yb2) > abs(yt1 - yt2):
                    angle = calAngle(xb1, yb1, xb2, yb2)
                    M = cv2.getRotationMatrix2D(center, angle, 1)
                    rotated_img = cv2.warpAffine(img, M, (w, h))
                else:
                    angle = calAngle(xt1, yt1, xt2, yt2)
                    M = cv2.getRotationMatrix2D(center, angle, 1)
                    rotated_img = cv2.warpAffine(img, M, (w, h))
                result = {
                    'flag': 1,
                    'rote_M': M,
                    'warp_M': None,
                    'keyboard_rect': None,
                    'rotated_img': rotated_img
                }
            else:
                lr, rt, rb, lb = rect
                sx, ex = int(min(lt[0], lb[0])), int(max(rt[0], rb[0]))
                sy, ey = int(min(lt[1], rt[1])), int(max(lb[1], rb[1]))
                flag, keyboard_rect = self.find_rect(pmask, sx, sy, ex, ey)
                result = {
                    'flag': flag,
                    'rote_M': None,
                    'warp_M': None,
                    'keyboard_rect': keyboard_rect,
                    'rotated_img': None
                }

        else:
            result = {
                'flag': 0,
                'rote_M': None,
                'warp_M': None,
                'keyboard_rect': None,
                'rotated_img': None
            }
        return result
예제 #3
0
def process2(mask):
    mask = cv2.cvtColor(mask, cv2.COLOR_BGR2GRAY)
    contours, pmask = find_contours(mask)
    result = {}
    rect = order_points(contours)
    if len(contours) > 500:
        lt, rt, rb, lb = rect
        sx, ex = int(min(lt[0], lb[0])), int(max(rt[0], rb[0]))
        sy, ey = int(min(lt[1], rt[1])), int(max(lb[1], rb[1]))
        flag, keyboard_rect = find_rect(pmask, sx, sy, ex, ey)
        result = {'flag': flag, 'keyboard_rect': keyboard_rect}
    else:
        result = {'flag': 0, 'keyboard_rect': None}
    return result
예제 #4
0
 def post_process2(self, image, mask):
     w, h = image.size
     img = cv2.cvtColor(np.asarray(image), cv2.COLOR_RGB2BGR)
     contours, pmask = self.find_contours(image, mask)
     result = {}
     rect = order_points(contours)
     if len(contours) > 500:
         lt, rt, rb, lb = rect
         sx, ex = int(min(lt[0], lb[0])), int(max(rt[0], rb[0]))
         sy, ey = int(min(lt[1], rt[1])), int(max(lb[1], rb[1]))
         flag, keyboard_rect = self.find_rect(pmask, sx, sy, ex, ey)
         result = {'flag': flag, 'keyboard_rect': keyboard_rect}
     else:
         result = {'flag': 0, 'keyboard_rect': None}
     return result
예제 #5
0
def main(index, image):        
    
    height, width, channels = image.shape;
    
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

    gray = cv2.GaussianBlur(gray, (7, 7), 0)
    image = cv2.copyMakeBorder(image, 5, 5, 5, 5, cv2.BORDER_CONSTANT, value=[0,0,0])
    gray = cv2.copyMakeBorder(gray, 5, 5, 5, 5, cv2.BORDER_CONSTANT, value=[0,0,0])

    edged = cv2.Canny(gray, 50, 100)
    
    cv2.imshow("test", edged)
    cv2.waitKey(0);

    contours, hierarchy = cv2.findContours(edged.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
    height = edged.shape[0]
    width = edged.shape[1]

    MAX_COUNTOUR_AREA = (width - 10) * (height - 10)
    maxAreaFound = MAX_COUNTOUR_AREA * 0.3 #object 크기는 이미지 전체의 30% 이상이어야 함.

    #colors = ((0, 0, 255), (240, 0, 159), (255, 0, 0), (255, 255, 0))

    rectList = [];
    for cnt in contours:
        if cv2.contourArea(cnt) <= maxAreaFound:
            continue

        box = cv2.minAreaRect(cnt)
        box = cv2.boxPoints(box)
        box = np.array(box, dtype="int")

        rect = order_points(box)

        if len(rectList) == 0:
            rectList.append(rect)
        else:
            count = 0;
            listLength = len(rectList)
            isNotSameArea = True;

            for oneRectInList in rectList:
                if 0.99 * cv2.contourArea(rect) <= cv2.contourArea(oneRectInList) <= 1.01 * cv2.contourArea(rect):
                    isNotSameArea = False;
                    break;
            
            if(isNotSameArea):
                rectList.append(rect)

    sortedRect = sorted(rectList, key=lambda oneRect: cv2.contourArea(oneRect), reverse=True)
    #print(sortedRect)

    roi = None

    if(len(sortedRect) == 1):
        roi = sortedRect[0]
    else:
        roi = sortedRect[1]

    pts = np.array(roi, dtype = "float32")
    warped = four_point_transform(image, pts)
    if index != 0:
        warped = random_noise(warped, 100)
        warped = image_rotation(warped)
    
    cv2.imshow("Warped", warped)
    cv2.waitKey(0)
    print(dhash(Image.fromarray(warped.astype('uint8'), 'RGB')))
예제 #6
0
def main(index, image):
    height, width, channels = image.shape
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

    gray = cv2.GaussianBlur(gray, (7, 7), 0)
    gray = cv2.GaussianBlur(gray, (7, 7), 0)

    edged = cv2.Canny(gray, 30, 80)  # 50 ,100

    kernel = np.array([[0, 1, 0], [1, 1, 1], [0, 1, 0]], np.uint8)
    edged = cv2.dilate(edged, kernel, iterations=5)
    edged = cv2.erode(edged, kernel, iterations=5)

    image = cv2.copyMakeBorder(image,
                               1,
                               1,
                               1,
                               1,
                               cv2.BORDER_CONSTANT,
                               value=[0, 0, 0])
    edged = cv2.copyMakeBorder(edged,
                               1,
                               1,
                               1,
                               1,
                               cv2.BORDER_CONSTANT,
                               value=[1, 1, 1])

    contours, hierarchy = cv2.findContours(edged.copy(), cv2.RETR_TREE,
                                           cv2.CHAIN_APPROX_SIMPLE)
    height = edged.shape[0]
    width = edged.shape[1]

    MAX_COUNTOUR_AREA = (width - 2) * (height - 2)
    maxAreaFound = MAX_COUNTOUR_AREA * 0.35  #object 크기는 이미지 전체의 30% 이상이어야 함.

    rectList = []
    for cnt in contours:

        box = cv2.minAreaRect(cnt)
        box = cv2.boxPoints(box)
        box = np.array(box, dtype="int")

        if cv2.contourArea(box) <= maxAreaFound:
            continue

        rect = order_points(box)

        if len(rectList) == 0:
            rectList.append(rect)
        else:
            count = 0
            listLength = len(rectList)
            isNotSameArea = True

            for oneRectInList in rectList:
                if 0.99 * cv2.contourArea(rect) <= cv2.contourArea(
                        oneRectInList) <= 1.01 * cv2.contourArea(rect):
                    isNotSameArea = False
                    break

            if (isNotSameArea):
                rectList.append(rect)

    sortedRect = sorted(rectList,
                        key=lambda oneRect: cv2.contourArea(oneRect),
                        reverse=True)

    roi = None

    if (len(sortedRect) == 1):
        roi = sortedRect[0]
    else:
        roi = sortedRect[1]

    pts = np.array(roi, dtype="float32")
    warped = four_point_transform(image, pts)

    if index != 0:
        warped = random_noise(warped, 200)
        warped = image_rotation(warped)

    print(dhash(Image.fromarray(warped.astype('uint8'), 'RGB')))
예제 #7
0
def main():
    filePath = sys.argv[1]
    image = cv2.imread(filePath)
    
    #image = cv2.imread("sample4.jpg")
    height, width, channels = image.shape;
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

    gray = cv2.GaussianBlur(gray, (7, 7), 0)
    gray = cv2.GaussianBlur(gray, (7, 7), 0)

    edged = cv2.Canny(gray, 30, 80) # 30, 80
  
    kernel = np.array([[0,1,0], [1,1,1], [0,1,0]], np.uint8)
    edged = cv2.dilate(edged, kernel, iterations=5)
    edged = cv2.erode(edged, kernel, iterations=5)
        
    image = cv2.copyMakeBorder(image, 1, 1, 1, 1, cv2.BORDER_CONSTANT, value=[0,0,0])
    edged = cv2.copyMakeBorder(edged, 1, 1, 1, 1, cv2.BORDER_CONSTANT, value=[1,1,1])
    
    #cv2.imshow("test", edged)
    #cv2.waitKey(0);

    contours, hierarchy = cv2.findContours(edged.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
    height = edged.shape[0]
    width = edged.shape[1]
    
    #print(image.shape)
    
    MAX_COUNTOUR_AREA = (width - 2) * (height - 2)
    maxAreaFound = MAX_COUNTOUR_AREA * 0.35  # 오브젝트 크기는 이미지 전체의 35% 이상이어야 함.
    #testContourArea = MAX_COUNTOUR_AREA * 0.01; # 0.01# 0.05

    #print(MAX_COUNTOUR_AREA)

    colors = ((0, 0, 255), (240, 0, 159), (255, 0, 0), (255, 255, 0))

    rectList = [];
    for cnt in contours:
        box = cv2.minAreaRect(cnt)
        box = cv2.boxPoints(box)
        box = np.array(box, dtype="int")
        
        #if cv2.contourArea(box) <= maxAreaFound or cv2.contourArea(cnt) <= testContourArea :
        #   continue
        #print(cv2.contourArea(box), " <-> ",  (MAX_COUNTOUR_AREA-cv2.contourArea(box)))
        #if cv2.contourArea(box) <= maxAreaFound and cv2.contourArea(cnt) <= (MAX_COUNTOUR_AREA-cv2.contourArea(box))*0.5 :
        #   continue

        #print(cv2.contourArea(box), " <-> ",  cv2.contourArea(cnt))
        if cv2.contourArea(box) <= maxAreaFound: 
           continue

        #box = cv2.minAreaRect(cnt)
        #box = cv2.boxPoints(box)
        #box = np.array(box, dtype="int")

        rect = order_points(box)

        if len(rectList) == 0:
            rectList.append(rect)
        else:
            count = 0;
            listLength = len(rectList)
            isNotSameArea = True;

            for oneRectInList in rectList:
                if 0.99 * cv2.contourArea(rect) <= cv2.contourArea(oneRectInList) <= 1.01 * cv2.contourArea(rect):
                    isNotSameArea = False;
                    break;
            
            if(isNotSameArea):
                #print(cv2.contourArea(rect), "," ,(cv2.contourArea(rect)/MAX_COUNTOUR_AREA) * 100)
                rectList.append(rect)

    sortedRect = sorted(rectList, key=lambda oneRect: cv2.contourArea(oneRect), reverse=True)
    #print(sortedRect)

    roi = None
    
    #사각형이 아예 안나오는 경우 버그 생길 수 있음.
    if(len(sortedRect) == 1):
        roi = sortedRect[0]
    else:
        roi = sortedRect[1]

    pts = np.array(roi, dtype = "float32")
    warped = four_point_transform(image, pts)
    
    #print(warped.shape)

    #cv2.imshow("Warped", warped)
    #cv2.waitKey(0)
    print(dhash(Image.fromarray(warped.astype('uint8'), 'RGB')))