FlowCalculator.py 文件源码

python
阅读 42 收藏 0 点赞 0 评论 0

项目:VerySharp 作者: wilecoyote2015 项目源码 文件源码
def calculateOpticalFlowsForDataset(self, image_reference, dataset):
        # optical flows will be stored here        
        list_optical_flows = []

        # add zero optical flow for the reference image which is at first position
        shape_image_reference = image_reference.shape
        shape_optical_flow = [shape_image_reference[0],
                              shape_image_reference[1],
                              2] 
        zero_optical_flow = np.zeros(shape_optical_flow, np.float32)
        list_optical_flows.append(zero_optical_flow)

        # iterate through the dataset and calculate the optical flow for each
        # except the first one
        num_images = dataset.getImageCount()
        for index in range(1, num_images):

            print ("calculating optical flow for image ", index)      

            # Get the image at the index
            data = dataset.getData(index)
            hdu_image = data["hdu_list"][self.extension].data
            image = CommonFunctions.preprocessHduImage(hdu_image, 
                                                       self.scale_factor)

            # @todo: here, do not use config but simply check if matrix is None
            # apply the transformation to the input image
            if self.config["Processing_Options"]["align_images"] == "True":
                # get the image dimension
                image_shape = image.shape

                # Transform the Image
                image = cv2.warpAffine(image,
                                       data["transform_matrix"],
                                       (image_shape[1],image_shape[0]),
                                       flags=cv2.INTER_CUBIC + cv2.WARP_INVERSE_MAP)       

            # calculate the optical flow (backwards for warping!)
            optical_flow = cv2.calcOpticalFlowFarneback(image_reference,
                                                        image,
                                                        None,
                                                        self.pyr_scale,
                                                        self.levels,
                                                        self.winsize,
                                                        self.iterations,
                                                        self.poly_n,
                                                        self.poly_sigma,
                                                        cv2.OPTFLOW_FARNEBACK_GAUSSIAN)

            # Write out optical flow images for user evaluation
            self.writeOpticalFlowImage(index, optical_flow)

            list_optical_flows.append(optical_flow)

        return list_optical_flows


    ## Average a list of optical flows
    #  @param list_optical_flows list object containing optical flows as numpy arrays
    #  @return averaged optical flow as numpy array
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号