Exemple #1
0
    def collectCheckboardPoints(self):
        
        self.pointsArray1 = np.zeros((nimages, num_pts, 2)) 
        self.pointsArray2 = np.zeros((nimages, num_pts, 2)) 

        cv.NamedWindow("camera")
        cv.NamedWindow("camera2")
        
      
        i = 0
        while True :
            frame = cv.QueryFrame(self.video1)
        #     print type(frame)
        #     [rows1, cols] = cv.GetSize(frame)
            image = cv.CreateImage(cv.GetSize(frame), cv.IPL_DEPTH_8U, frame.nChannels)        
            cv.Copy(frame, image)
            cv.ShowImage("camera", frame)
            grayScaleFullImage = cv.CreateImage((image.width, image.height), 8, 1)
            cv.CvtColor(image, grayScaleFullImage, cv.CV_BGR2GRAY)
            
            frame2 = cv.QueryFrame(self.video2)
            image2 = cv.CreateImage(cv.GetSize(frame2), cv.IPL_DEPTH_8U, frame2.nChannels)        
            cv.Copy(frame2, image2)
            cv.ShowImage("camera2", frame2)
            grayScaleFullImage2 = cv.CreateImage((image2.width, image2.height), 8, 1)
            cv.CvtColor(image2, grayScaleFullImage2, cv.CV_BGR2GRAY)
             
             
            found, points = cv.FindChessboardCorners(grayScaleFullImage, dims, cv.CV_CALIB_CB_ADAPTIVE_THRESH)
            if found != 0:
                print "found chess board " + str(np.shape(points))
                cv.DrawChessboardCorners(image, dims, points, found)
                cv.ShowImage("win2", image)
                cv.WaitKey(2)
        #     else:
        #         print "no chess"
            found2, points2 = cv.FindChessboardCorners(grayScaleFullImage2, dims, cv.CV_CALIB_CB_ADAPTIVE_THRESH)
            if found2 != 0:
                print "found chess board2"
                cv.DrawChessboardCorners(image2, dims, points2, found2)
                cv.ShowImage("win3", image2)
                cv.WaitKey(2)
        
            if found and found2:
                
                print "entered here!!!!!"
               
                self.pointsArray1[i, :] = points
                self.pointsArray2[i, :] = points2
                i = i + 1
                
            if i == nimages:
                self.size = cv.GetSize(image)
                break
            
            if cv.WaitKey(10) == 27:
                break 
        
        cv.DestroyWindow("Camera 1")
        cv.DestroyWindow("Camera 2")
    def handle_msg(self, msg):
        """
        Detects the calibration target and, if found and provides enough new information,
        adds it to the sample database.

        Returns a MonoDrawable message with the display image and progress info.
        """
        rgb = self.mkgray(msg)
        linear_error = -1

        # Get display-image-to-be (scrib) and detection of the calibration target
        scrib, corners, downsampled_corners, board, (
            x_scale, y_scale) = self.downsample_and_detect(rgb)

        if self.calibrated:
            # Show rectified image
            # TODO Pull out downsampling code into function
            if x_scale != 1.0 or y_scale != 1.0:
                rgb_rect = self.remap(rgb)
                cv.Resize(rgb_rect, scrib)
            else:
                scrib = self.remap(rgb)

            if corners:
                # Report linear error
                src = self.mk_image_points([(corners, board)])
                undistorted = list(cvmat_iterator(self.undistort_points(src)))
                linear_error = self.linear_error(undistorted, board)

                # Draw rectified corners
                scrib_src = [(x / x_scale, y / y_scale)
                             for (x, y) in undistorted]
                cv.DrawChessboardCorners(scrib, (board.n_cols, board.n_rows),
                                         scrib_src, True)

        elif corners:
            # Draw (potentially downsampled) corners onto display image
            src = self.mk_image_points([(downsampled_corners, board)])
            cv.DrawChessboardCorners(scrib, (board.n_cols, board.n_rows),
                                     cvmat_iterator(src), True)

            # Add sample to database only if it's sufficiently different from any previous sample.
            params = self.get_parameters(corners, board, cv.GetSize(rgb))
            if self.is_good_sample(params):
                self.db.append((params, rgb))
                self.good_corners.append((corners, board))
                print "*** Added sample %d, p_x = %.3f, p_y = %.3f, p_size = %.3f, skew = %.3f" % tuple(
                    [len(self.db)] + params)

        rv = MonoDrawable()
        rv.scrib = scrib
        rv.params = self.compute_goodenough()
        rv.linear_error = linear_error
        return rv
Exemple #3
0
if __name__ == "__main__":
    cv.NamedWindow("win")
    if len(sys.argv) > 1:
        filename = sys.argv[1]
        im = cv.LoadImage(filename, cv.CV_LOAD_IMAGE_GRAYSCALE)
        im3 = cv.LoadImage(filename, cv.CV_LOAD_IMAGE_COLOR)
    else:
        try:  # try opening local copy of image
            fileName = '../cpp/left01.jpg'
            im = cv.LoadImageM(fileName, False)
            im3 = cv.LoadImageM(fileName, True)
        except:  # if local copy cannot be opened, try downloading it
            url = 'https://raw.github.com/opencv/opencv/master/samples/cpp/left01.jpg'
            filedata = urllib2.urlopen(url).read()
            imagefiledata = cv.CreateMatHeader(1, len(filedata), cv.CV_8UC1)
            cv.SetData(imagefiledata, filedata, len(filedata))
            im = cv.DecodeImageM(imagefiledata, cv.CV_LOAD_IMAGE_GRAYSCALE)
            im3 = cv.DecodeImageM(imagefiledata, cv.CV_LOAD_IMAGE_COLOR)

    chessboard_dim = (9, 6)

    found_all, corners = cv.FindChessboardCorners(im, chessboard_dim)
    print found_all, len(corners)

    cv.DrawChessboardCorners(im3, chessboard_dim, corners, found_all)

    cv.ShowImage("win", im3)
    cv.WaitKey()
    cv.DestroyAllWindows()
    def handle_msg(self, msg):
        # TODO Various asserts that images have same dimension, same board detected...
        (lmsg, rmsg) = msg
        lrgb = self.mkgray(lmsg)
        rrgb = self.mkgray(rmsg)
        epierror = -1

        # Get display-images-to-be and detections of the calibration target
        lscrib, lcorners, ldownsampled_corners, lboard, (
            x_scale, y_scale) = self.downsample_and_detect(lrgb)
        rscrib, rcorners, rdownsampled_corners, rboard, _ = self.downsample_and_detect(
            rrgb)

        if self.calibrated:
            # Show rectified images
            if x_scale != 1.0 or y_scale != 1.0:
                rgb_rect = self.l.remap(lrgb)
                cv.Resize(rgb_rect, lscrib)
                rgb_rect = self.r.remap(rrgb)
                cv.Resize(rgb_rect, rscrib)
            else:
                lscrib = self.l.remap(lrgb)
                rscrib = self.r.remap(rrgb)

            # Draw rectified corners
            if lcorners:
                src = self.mk_image_points([(lcorners, lboard)])
                lundistorted = list(
                    cvmat_iterator(self.l.undistort_points(src)))
                scrib_src = [(x / x_scale, y / y_scale)
                             for (x, y) in lundistorted]
                cv.DrawChessboardCorners(lscrib,
                                         (lboard.n_cols, lboard.n_rows),
                                         scrib_src, True)

            if rcorners:
                src = self.mk_image_points([(rcorners, rboard)])
                rundistorted = list(
                    cvmat_iterator(self.r.undistort_points(src)))
                scrib_src = [(x / x_scale, y / y_scale)
                             for (x, y) in rundistorted]
                cv.DrawChessboardCorners(rscrib,
                                         (rboard.n_cols, rboard.n_rows),
                                         scrib_src, True)

            # Report epipolar error
            if lcorners and rcorners:
                epierror = self.epipolar_error(lundistorted, rundistorted,
                                               lboard)

        else:
            # Draw any detected chessboards onto display (downsampled) images
            if lcorners:
                src = self.mk_image_points([(ldownsampled_corners, lboard)])
                cv.DrawChessboardCorners(lscrib,
                                         (lboard.n_cols, lboard.n_rows),
                                         cvmat_iterator(src), True)
            if rcorners:
                src = self.mk_image_points([(rdownsampled_corners, rboard)])
                cv.DrawChessboardCorners(rscrib,
                                         (rboard.n_cols, rboard.n_rows),
                                         cvmat_iterator(src), True)

            # Add sample to database only if it's sufficiently different from any previous sample
            if lcorners and rcorners:
                params = self.get_parameters(lcorners, lboard,
                                             cv.GetSize(lrgb))
                if self.is_good_sample(params):
                    self.db.append((params, lrgb, rrgb))
                    self.good_corners.append((lcorners, rcorners, lboard))
                    print "*** Added sample %d, p_x = %.3f, p_y = %.3f, p_size = %.3f, skew = %.3f" % tuple(
                        [len(self.db)] + params)

        rv = StereoDrawable()
        rv.lscrib = lscrib
        rv.rscrib = rscrib
        rv.params = self.compute_goodenough()
        rv.epierror = epierror
        return rv
Exemple #5
0
    x0 = [0.]
    #print silly(x0, goodcorners)
    print "initial error", silly(x0, goodcorners)
    xopt = fmin(silly, x0, args=(goodcorners, ))
    print "xopt", xopt
    print "final error", silly(xopt, goodcorners)

    d = 1.0  # - sum(xopt)
    poly = numpy.poly1d(list(xopt) + [d, 0.])
    print "final polynomial"
    print poly

    for co in goodcorners:
        scrib = cv.CreateMat(480, 640, cv.CV_8UC3)
        cv.SetZero(scrib)
        cv.DrawChessboardCorners(scrib, (num_x_ints, num_y_ints),
                                 [xf(pt, poly) for pt in co], True)
        cv.ShowImage("snap", scrib)
        cv.WaitKey()

    sys.exit(0)

for (i, (img, (ok, co))) in enumerate(zip(images, corners)):
    scrib = cv.CreateMat(img.rows, img.cols, cv.CV_8UC3)
    cv.CvtColor(img, scrib, cv.CV_GRAY2BGR)
    if ok:
        cv.DrawChessboardCorners(scrib, (num_x_ints, num_y_ints), co, True)
    cv.ShowImage("snap", scrib)
    cv.WaitKey()

print len(goodcorners)
ipts = mk_image_points(goodcorners)