def load_image(self, contour, orig_image): mask = cv.CreateImage( cv.GetSize(orig_image), 8, 1) cv.Set(mask, cv.RGB(0, 0, 0)) cv.DrawContours(mask, contour, cv.RGB(255, 255, 255), cv.RGB(255, 255, 255), 0, -1) img = cv.CreateImage( cv.GetSize(orig_image), 8, 3) cv.Set(img, cv.RGB(255, 0, 0)) cv.Copy(orig_image, img, mask) big_image = common.opencv_img_to_pygame_img(img) p_rect = pygame.rect.Rect(cv.BoundingRect(contour)) image = pygame.Surface((p_rect.width, p_rect.height)) rect = image.blit(big_image, (0, 0), p_rect) image.set_colorkey((255, 0, 0), RLEACCEL) return image, rect
def run(self): calib_img = pygame.image.load(common.CALIBRATION_IMAGE).convert() calib_img = pygame.transform.scale(calib_img, common.RESOLUTION) self.screen.blit(calib_img, (0, 0)) pygame.display.flip() pygame.time.wait(1000) # this is actually the dimensions of the "inner chessboard" (see FindChessboardCorners documentation) chessboard_dim = ( 8, 5 ) # For some reason the first capture doesn't get the chessboard (even though it's there 5 secs!) and the second one sometimes comes out blurry. whole_view = cv.QueryFrame(self.capture) whole_view = cv.QueryFrame(self.capture) # converting our image to grayscale. Might be unnecessary. whole_view_gs = cv.CreateImage(cv.GetSize(whole_view), whole_view.depth, 1) cv.CvtColor(whole_view, whole_view_gs, cv.CV_BGR2GRAY) found_all, corners = cv.FindChessboardCorners( whole_view_gs, chessboard_dim ) if found_all: cv.DrawChessboardCorners( whole_view, chessboard_dim, corners, found_all ) else: print "Only found ", len(corners), " corners" self.screen.blit(common.opencv_img_to_pygame_img(whole_view), (0,0)) pygame.display.flip() pygame.time.wait(1000) exit(1) # these are the bounding 4 points of the inner chessboard. bounding_rect = (corners[39], corners[32], corners[7], corners[0]) transform_mat = cv.CreateMat(3, 3, cv.CV_32F) cv.GetPerspectiveTransform(bounding_rect, common.SCREEN_RECT, transform_mat) return transform_mat