class avm:
    def __init__(self):
        self.__frontCamera = UndistortFisheye("Front_Camera")
        self.__backCamera = UndistortFisheye("Back_Camera")

        self.__frontEagle = EagleView()
        self.__backEagle = EagleView()
        # self.__frontEagle.setDimensions((149, 195), (439, 207), (528, 380), (37, 374))
        # self.__backEagle.setDimensions((164, 229), (469, 229), (588, 430), (45, 435))
        self.__frontEagle.setDimensions((186, 195), (484, 207), (588, 402),
                                        (97, 363))
        self.__backEagle.setDimensions((171, 240), (469, 240), (603, 452),
                                       (52, 441))

        self.__middleView = None
        self.__counter = 0

        self.stitcher = stitchTwoImages("Bottom2Upper")
        self.upper = None
        self.bottom = None

    def runAVM(self, frontFrame, backFrame):
        frontView = self.__frontCamera.undistort(frontFrame)
        topDown_Front = self.__frontEagle.transfrom(frontView)
        backView = self.__backCamera.undistort(backFrame)
        topDown_Back = self.__backEagle.transfrom(backView)
        topDown_Back = cv2.flip(topDown_Back, 1)

        topDown_Front, topDown_Back = self.__reScale(topDown_Front,
                                                     topDown_Back)
        # stitchingResult = self.__startStitching(topDown_Front)
        middleView = self.__getMiddleView(topDown_Front)
        birdView = np.vstack((topDown_Front, middleView, topDown_Back))
        return birdView

    def __reScale(self, topDown_Front, topDown_Back):
        width_FrontView = topDown_Front.shape[1]
        width_BackView = topDown_Back.shape[1]
        height_FrontView = topDown_Front.shape[0]
        height_BackView = topDown_Back.shape[0]

        if width_FrontView > width_BackView:
            newWidth = width_BackView
            ratio = width_BackView / width_FrontView
            newHeight = int(ratio * height_FrontView)
            topDown_Front = cv2.resize(topDown_Front, (newWidth, newHeight))
        else:
            newWidth = width_FrontView
            ratio = width_FrontView / width_BackView
            newHeight = int(ratio * height_BackView)
            topDown_Back = cv2.resize(topDown_Back, (newWidth, newHeight))

        return topDown_Front, topDown_Back

    def __getMiddleView(self, topDown_Front):
        # the length of the image represents the distance in front or back of the car
        height_FrontView = topDown_Front.shape[0]
        if self.__middleView is None:
            realHeight_FrontView = 13  # unit is cm
            realHeight_MiddleView = 29.5  # unit is cm
            ratio = int(height_FrontView / realHeight_FrontView)
            height_MiddleView = int(realHeight_MiddleView * ratio)
            width_MiddleView = int(topDown_Front.shape[1])
            self.__middleView = np.zeros(
                (height_MiddleView, width_MiddleView, 3), np.uint8)
            # print(ratio)
        # else:
        #     # self.__middleView[0:stitchingResult.shape[0], :]

        return self.__middleView

    def __startStitching(self, accView):
        if self.bottom is None:
            self.bottom = accView
            return None
        else:
            # time.sleep(0.5)
            self.upper = accView
            self.bottom = self.stitcher.stitch(self.bottom, self.upper)
            cv2.imshow("Result", self.bottom)
            height = accView.shape[0]
            return self.bottom[height:self.bottom.shape[0], :]
while True:
    # isGrabbed, frame = video.read()
    frame = cv2.imread("dataset/Front_View.jpg")

    topLeft = (int(topLeft_XBar.getValue()), int(topLeft_YBar.getValue()))
    topRight = (int(topRight_XBar.getValue()), int(topRight_YBar.getValue()))
    bottomRight = (int(bottomRight_XBar.getValue()),
                   int(bottomRight_YBar.getValue()))
    bottomLeft = (int(bottomLeft_XBar.getValue()),
                  int(bottomLeft_YBar.getValue()))

    cameraView = camera.undistort(frame)

    eagleView.setDimensions(topLeft, topRight, bottomRight, bottomLeft)
    topDown = eagleView.transfrom(cameraView)

    cloneFrame = cameraView.copy()
    # visualize corners
    cv2.line(cloneFrame, topLeft, topRight, (0, 0, 255), 1)
    cv2.line(cloneFrame, bottomLeft, bottomRight, (0, 0, 255), 1)
    cv2.line(cloneFrame, topLeft, bottomLeft, (0, 0, 255), 1)
    cv2.line(cloneFrame, bottomRight, topRight, (0, 0, 255), 1)

    cv2.imshow("Bird's Eye View", topDown)
    cv2.imshow("Original Image", cloneFrame)
    key = cv2.waitKey(1) & 0xFF
    if key == ord("q"):
        break
    elif key == ord("d"):  # print tuned values when "d" button is pressed
        print("width: " + str(cloneFrame.shape[1]))
frontEagle.setDimensions((149, 195), (439, 207), (528, 380), (37, 374))

count = 0
totalTime = 0
mergedImage = 0
video = cv2.VideoCapture("dataset/front_camera.avi")
while True:
    count += 1
    isGrabbed, frame = video.read()

    if (not isGrabbed):
        break
    if count % 5 != 0:
        continue
    frontView = frontCamera.undistort(frame)
    topDown_Front = frontEagle.transfrom(frontView)
    # frame = imutils.resize(frame, width=600)
    if count == 5:
        bottom = topDown_Front
    else:
        # time.sleep(1)
        upper = topDown_Front
        cv2.imshow("upper", upper)
        cv2.imshow("bottom", bottom)
        cv2.waitKey()
        # key = cv2.waitKey(1) & 0xFF
        # if key == ord("q"):
        #     break
        stitcher = stitchTwoImages("Bottom2Upper")
        mergedImage = stitcher.stitch(bottom, upper)
        if mergedImage is None:
# frontStream = cv2.VideoCapture("dataset/front_camera.avi")
# backStream = cv2.VideoCapture("dataset/back_camera.avi")

frontCamera = UndistortFisheye("Front_Camera")
backCamera = UndistortFisheye("Back_Camera")

frontEagle = EagleView()
backEagle = EagleView()
frontEagle.setDimensions((186, 195), (484, 207), (588, 402), (97, 363))
backEagle.setDimensions((171, 240), (469, 240), (603, 452), (52, 441))

while True:
    # _, frontFrame = frontStream.read()
    # _, backFrame = backStream.read()
    frontFrame = cv2.imread("dataset/Front_View.jpg")
    backFrame = cv2.imread("dataset/Rear_View.jpg")

    frontView = frontCamera.undistort(frontFrame)
    topDown_Front = frontEagle.transfrom(frontView)
    backView = backCamera.undistort(backFrame)
    topDown_Back = backEagle.transfrom(backView)

    cv2.imshow("Front Bird's Eye View", topDown_Front)
    cv2.imshow("Back Bird's Eye View", topDown_Back)

    key = cv2.waitKey(1) & 0xFF
    if key == ord("q"):
        break

cv2.destroyAllWindows()
Esempio n. 5
0
class avm:
    def __init__(self):
        self.__leftCamera = UndistortFisheye("left_Camera")
        self.__rightCamera = UndistortFisheye("right_Camera")

        self.__leftEagle = EagleView()
        self.__rightEagle = EagleView()
        # self.__frontEagle.setDimensions((149, 195), (439, 207), (528, 380), (37, 374))
        # self.__backEagle.setDimensions((164, 229), (469, 229), (588, 430), (45, 435))

        #reset left/right setDimensions
        self.__leftEagle.setDimensions(
            (186, 195), (484, 207), (588, 402), (97, 363)
        )  #fisheyecalibration을 진행한 후인 Undistorted_Front_View.jpg의 4 개의 꼭짓점
        self.__rightEagle.setDimensions((171, 240), (469, 240), (603, 452),
                                        (52, 441))
        # self.__leftEagle.setDimensions((186, 195), (484, 207), (588, 402), (97, 363)) #fisheyecalibration을 진행한 후인 Undistorted_Front_View.jpg의 4 개의 꼭짓점
        # self.__rightEagle.setDimensions((171, 240), (469, 240), (603, 452), (52, 441))

        self.__middleView = None
        self.__counter = 0

        # self.stitcher = stitchTwoImages("Bottom2Upper") #어떻게 활용해야할까???
        # self.upper = None
        # self.bottom = None

    def runAVM(self, leftFrame, rightFrame):
        leftView = self.__leftCamera.undistort(leftFrame)
        topDown_left = self.__leftEagle.transfrom(leftView)
        rightView = self.__rightCamera.undistort(rightFrame)
        topDown_right = self.__rightEagle.transfrom(rightView)
        # topDown_Back = cv2.flip(topDown_Back, 1) #flip left/right

        topDown_left, topDown_right = self.__reScale(topDown_left,
                                                     topDown_right)
        # stitchingResult = self.__startStitching(topDown_Front)
        middleView = self.__getMiddleView(topDown_left)
        birdView = np.hstack((topDown_left, middleView, topDown_right))
        return birdView

    def __reScale(self, topDown_left, topDown_right):
        width_leftView = topDown_left.shape[1]
        width_rightView = topDown_right.shape[1]
        height_leftView = topDown_left.shape[0]
        height_rightView = topDown_right.shape[0]
        if height_leftView > height_rightView:
            newHeight = height_rightView
            ratio = height_rightView / height_leftView
            newWidth = int(ratio * width_leftView)
            topDown_left = cv2.resize(topDown_left, (newWidth, newHeight))
        else:
            newHeight = height_leftView
            ratio = height_leftView / height_rightView
            newWidth = int(ratio * width_rightView)
            topDown_right = cv2.resize(topDown_right, (newWidth, newHeight))

        return topDown_left, topDown_right

    def __getMiddleView(self, topDown_left):
        # the length of the image represents the distance in front or back of the car
        width_leftView = topDown_left.shape[1]
        if self.__middleView is None:
            realWidth_leftView = 13  # unit is cm
            realWidth_MiddleView = 29.5  # unit is cm
            ratio = int(width_leftView / realWidth_leftView)
            width_MiddleView = int(realWidth_MiddleView * ratio)
            height_MiddleView = int(topDown_left.shape[0])
            self.__middleView = np.zeros(
                (height_MiddleView, width_MiddleView // 2, 3), np.uint8)
            # print(ratio)
        # else:
        #     #  self.__middleView[0:stitchingResult.shape[0], :]

        return self.__middleView