示例#1
0
def get_mean(vid_file):
    """Return the average frame across the whole video."""
    vid = cv2.VideoCapture(vid_file)
    num_frames = vid.get(cv2.cv.CV_CAP_PROP_FRAME_COUNT)
    
    retval, im = vid.read()
    mean = np.zeros((len(im), len(im[0]), 3))
    num = 1.0
    cv2.accumulate(im,mean)
    frame = 0
    
    while retval:
        frame += 1
        sys.stdout.write('\rgetting mean: ' + `(frame * 100) / int(num_frames)` + '%')
        sys.stdout.flush()

        cv2.accumulate(im, mean)
        num += 1
        retval, im = vid.read()

    print '\ndone getting mean'
        
    mean = mean / num
    mean = mean.astype("uint8")
    vid.release()

    return mean
示例#2
0
def callback(image_wv1, image_wv2, image_wv3, image_wv4, image_wv5):
    stamp = image_wv1.header.stamp

    cv_img_wv1 = bridge.imgmsg_to_cv2(image_wv1, desired_encoding="bgr8")
    cv_img_wv1 = cv.cvtColor(cv_img_wv1, cv.COLOR_BGR2GRAY)
    cv_img_wv2 = bridge.imgmsg_to_cv2(image_wv2, desired_encoding="bgr8")
    cv_img_wv2 = cv.cvtColor(cv_img_wv2, cv.COLOR_BGR2GRAY)
    cv_img_wv3 = bridge.imgmsg_to_cv2(image_wv3, desired_encoding="bgr8")
    cv_img_wv3 = cv.cvtColor(cv_img_wv3, cv.COLOR_BGR2GRAY)
    cv_img_wv4 = bridge.imgmsg_to_cv2(image_wv4, desired_encoding="bgr8")
    cv_img_wv4 = cv.cvtColor(cv_img_wv4, cv.COLOR_BGR2GRAY)
    cv_img_wv5 = bridge.imgmsg_to_cv2(image_wv5, desired_encoding="bgr8")
    cv_img_wv5 = cv.cvtColor(cv_img_wv5, cv.COLOR_BGR2GRAY)

    #Fusion
    cv_img_merged = np.zeros(cv_img_wv1.shape)
    cv.accumulate(cv_img_wv1, cv_img_merged);
    cv.accumulate(cv_img_wv2, cv_img_merged);
    cv.accumulate(cv_img_wv3, cv_img_merged);
    cv.accumulate(cv_img_wv4, cv_img_merged);
    cv.accumulate(cv_img_wv5, cv_img_merged);
    cv_img_merged = cv_img_merged/5
    cv_img_merged = cv_img_merged.astype(np.uint8)

    #Publish
    message = bridge.cv2_to_imgmsg(cv_img_merged, encoding="mono8")
    message.header.stamp = stamp

    pub.publish(message)
示例#3
0
    def plot(self, colorPatches):
        print self
        # print "Cluster %i has %i components" % (self.clusterID, self.count)
        enlargeFactor = 4
        bigN = 15 * enlargeFactor
        bigImage = np.zeros((bigN, bigN * self.count, 3), np.uint8)
        accumu = np.zeros(colorPatches[0].shape, np.float)
        for i in range(self.count):
            cv2.accumulate(colorPatches[self.components[i]], accumu)
            bigImage[:, i * bigN:(i + 1) * bigN] = cv2.resize(
                colorPatches[self.components[i]],
                None,
                fx=enlargeFactor,
                fy=enlargeFactor,
                interpolation=cv2.INTER_CUBIC)

        self.avgImg = (accumu / self.count).astype(np.uint8)
        cv2.imshow('Cluster' + str(self.clusterID), bigImage)
        cv2.imshow(
            'Rescaled Average image' + str(self.clusterID),
            cv2.resize(self.avgImg,
                       None,
                       fx=enlargeFactor,
                       fy=enlargeFactor,
                       interpolation=cv2.INTER_CUBIC))
        WaitKey(0)
示例#4
0
def accumulate_frames(camera, averaged_image, method='accumulate', alpha='1'):
    """
        Obtain an averaged frame from a webcam, with
    :param camera: cv2.VideoCapture()
        Camera object created with cv2.VideoCapture('camera_index')
    :param alpha: float [0:1]
        defines persistence of background. In other words, how long before a frame is forgotten (1 = never)
    :param color : string
        defines output pixel data
        possible values: 'RGB', 'BGR', 'avg', 'r', 'g', 'b',
            'BGR' - no transformation, image given as BGR color coding
            'RGB' - transforms color coding into RGB
            'sum' - averages all colors and returns single value per pixel
            'r' - returns only red channel
            'g' - returns only green channel
            'b' - returns only blue channel
    :return: np.array
        array of pixels with data as defined by color method
    """

    _, frame = camera.read()
    # avg_img = np.float32(frame)
    if method == 'accumulate':
        cv2.accumulate(frame, averaged_image)
    elif method == 'accumulateWeighted':
        cv2.accumulateWeighted(frame, averaged_image, alpha)
    elif method == 'accumulateProduct':
        cv2.accumulateProduct(frame, averaged_image, alpha)
    elif method == 'accumulateSquare':
        cv2.accumulateSquare(frame, averaged_image, alpha)
    else:
        raise ValueError(
            'Unknown accumulation method, please use one of: \n\taccumulate \n\taccumulateSquare \n\taccumulateProduct, \n\taccumulateWeighted'
        )
    return averaged_image
 def get_simple_diff_mask(self, image):
     """
     Calculate a mask for the image based on the difference with previous ones.
     TODO: Move to Hand class
     :param image: image to get the mask
     :return: mask based on the difference with previous frames
     """
     mask = None
     if len(self.last_frames) < self.discarded_frames:
         self.last_frames.append(image)
         mean_image = np.zeros(image.shape, dtype=np.float32)
         for old_image in self.last_frames:
             cv2.accumulate(old_image, mean_image)
         self.first_frame = mean_image / len(self.last_frames)
     else:
         blur_radius = 5
         blurred_image = cv2.GaussianBlur(image, (blur_radius, blur_radius),
                                          0)
         blurred_background = cv2.GaussianBlur(self.first_frame,
                                               (blur_radius, blur_radius),
                                               0)
         diff = cv2.absdiff(blurred_image,
                            blurred_background.astype(np.uint8))
         # print "diff"
         gray_diff = cv2.cvtColor(diff, cv2.COLOR_BGR2GRAY)
         if self.debug:
             cv2.imshow("DEBUG: HandDetection_lib: diff", gray_diff)
         # TODO: ENV_DEPENDENCE: it could depend on the lighting and environment
         _, mask = cv2.threshold(gray_diff, 40, 255, cv2.THRESH_BINARY)
     return mask
示例#6
0
def get_tempo(vid_file):
    """Return the tempo property which is dependent on difference frames."""
    vid = cv2.VideoCapture(vid_file)
    num_frames = vid.get(cv2.cv.CV_CAP_PROP_FRAME_COUNT)
    width = vid.get(cv2.cv.CV_CAP_PROP_FRAME_WIDTH)
    height = vid.get(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT)
    num_pixels = width * height * 3

    retval, im1 = vid.read()
    retval, im2 = vid.read()

    total = np.zeros((height, width, 3))
    cv2.accumulate(im1, total)
    frame = 0
    
    while retval:
        frame += 1
        sys.stdout.write('\rgetting tempo: ' + `(frame * 100) / int(num_frames)` + '%')
        sys.stdout.flush()
        cv2.accumulate(cv2.subtract(im2,im1), total)
        im1 = im2
        retval, im2 = vid.read()

    print '\ndone getting tempo'
    vid.release()
    tempo = np.sum(total) / (num_pixels * num_frames * 255.0)
    
    return tempo
示例#7
0
    def avgBackground(self, I):
        # read background pic of I,and then calculate frame difference of current frame and pre frame: I and IprevF
        # accumulate every frame difference Iscratch2 to sum of differences :IdiffF
        # meanwhile,accumulate every frame I  to sum of frames :IavgF
        """
        Parameters
        --------------
        I: input  Mat type pic stream

        Returns
        -------

        Examples
        --------
        """
        #学习平均背景步骤:
        # 1,使用cv2.accumulate函数累积每一参与学习的帧 ,之后会根据帧数做平均(IavgF)
        # 2,使用cv2.absdiff函数获取当前帧(I)与前一帧(IprevF)的帧差(Iscratch2)
        # 3,再次使用cv2.accumulate函数对帧差(Iscratch2)做累积,之后会根据帧数(Icount)做平均得到(IdiffF)
        cv2.accumulate(I, self.IavgF)
        # cv2.absdiff(I,IprevF, Iscratch2)
        self.Iscratch2 = cv2.absdiff(I, self.IprevF)
        cv2.accumulate(self.Iscratch2, self.IdiffF)

        # print("IavgF[100,100,0]:", self.IavgF[100, 100, 0])
        # print("IdiffF[100,100,0]:", self.IdiffF[100, 100, 0])
        self.Icount += 1.0
        self.IprevF = I.copy()
示例#8
0
 def acc_back(self, frame):
   if self.i_prev_f != None:
     cv2.accumulate(frame, self.i_avg_f)
     diff = cv2.absdiff(frame, self.i_prev_f)
     cv2.accumulate(diff, self.i_diff_f)
     self.i_cnt += 1
   
   self.i_prev_f = frame.copy()
示例#9
0
def run_avg(image,wt):
    global bg

    if bg is None:
        bg=image.copy().astype('float')
        return

    cv2.accumulate(image,bg,wt)
    def get_simple_diff_mask2(self, image):
        """
        TODO: Move to Hand class
        :param image:
        :return:
        """
        mask = None
        # TODO: ENV_DEPENDENCE: it could depend on the lighting and environment
        blur_radius = 5
        if len(self.last_frames) > 0:
            mean_image = np.zeros(image.shape, dtype=np.float32)
            for old_image in self.last_frames:
                cv2.accumulate(old_image, mean_image)
            result_image = mean_image / len(self.last_frames)
            result_image = result_image.astype(np.uint8)
            # cv2.imshow("mean_image", result_image.astype(np.uint8))
            # cv2.imshow("the image", image)
            blurred_image = cv2.GaussianBlur(image, (blur_radius, blur_radius),
                                             0)
            blurred_background = cv2.GaussianBlur(result_image,
                                                  (blur_radius, blur_radius),
                                                  0)
            # cv2.imshow("b_mean_image", blurred_background)
            # cv2.imshow("b_the image", blurred_image)
            diff = cv2.absdiff(blurred_image,
                               blurred_background.astype(np.uint8))
            # cv2.imshow("diff", diff)
            # print "diff"
            gray_diff = cv2.cvtColor(diff, cv2.COLOR_BGR2GRAY)
            # cv2.imshow("gray_diff", gray_diff)
            # TODO: ENV_DEPENDENCE: it could depend on the lighting and environment
            _, mask = cv2.threshold(gray_diff, 40, 255, cv2.THRESH_BINARY)
            non_zero_pixels = cv2.countNonZero(mask)
            non_zero_percent = (non_zero_pixels * 100.) / (image.shape[0] *
                                                           image.shape[1])
            if non_zero_percent < 1:
                self.first_frame = result_image
            elif non_zero_percent > 95:
                self.reset_background()
            # print "Non zero pixel percentage %d" % non_zero_percent

        if self.first_frame is not None:

            blurred_image = cv2.GaussianBlur(image, (blur_radius, blur_radius),
                                             0)
            blurred_background = cv2.GaussianBlur(self.first_frame,
                                                  (blur_radius, blur_radius),
                                                  0)
            diff = cv2.absdiff(blurred_image,
                               blurred_background.astype(np.uint8))
            # print "diff"
            gray_diff = cv2.cvtColor(diff, cv2.COLOR_BGR2GRAY)
            # cv2.imshow("diff", gray_diff)
            # TODO: ENV_DEPENDENCE: it could depend on the lighting and environment
            _, mask = cv2.threshold(gray_diff, 40, 255, cv2.THRESH_BINARY)
        else:
            self.last_frames.append(image)
        return mask
    def summarize(self, colorPatches):
        # get average image
        accumu = np.zeros(colorPatches[0].shape, np.float)
        for i in range(self.count):
            cv2.accumulate(colorPatches[self.components[i]], accumu)
        self.avgImg = (accumu/self.count).astype(np.uint8)

        # get hist
        W,H,_ = colorPatches[0].shape
        self.bigImage = np.zeros((W, H*self.count, 3), np.uint8)
        for i in range(self.count):
           self.bigImage[:, i*H : (i+1)*H, :] = colorPatches[self.components[i]]
        bigImageHSV = cv2.cvtColor(self.bigImage, cv2.COLOR_BGR2HSV)
        self.hist = cv2.calcHist([bigImageHSV], [0,1], None, [180, 256],[0, 180,0,256])
示例#12
0
    def learn(self):
        if self.done:
            raise Exception('already done!')

        cv2.accumulate(self.motion_detector.prev_frame, self.__accumulated)
        self.frames_learned = self.frames_learned + 1

        if self.motion_detector.motion_started():
            self.learned = self.__average(self.__accumulated,
                                          self.frames_learned)
            self.done = True
            self.__accumulated = None

        return self.done
示例#13
0
    def learn(self, frame, foregroundMask=None):
        if self.__framesHistoryAccumulator is None:
            self.__framesHistoryAccumulator = np.zeros(frame.shape, np.float32)
            self.learned = np.zeros(frame.shape, np.uint8)
        history = self.__framesHistory
        accumulator = self.__framesHistoryAccumulator

        if len(history) == history.maxlen:  # if frames queue is full
            oldestFrame, bgMask = history.popleft()
            cv2.subtract(accumulator, oldestFrame, dst=accumulator, dtype=cv2.CV_32F,
                         mask=bgMask)  # remove old frame from accum

        bgMask = cv2.bitwise_not(foregroundMask)
        history.append((frame, bgMask))
        cv2.accumulate(frame, accumulator, mask=bgMask)  # accumulate frames sum
        self.learned = np.true_divide(accumulator, len(history), out=self.learned, casting='unsafe')  # average frame
示例#14
0
    def summarize(self, colorPatches):
        # get average image
        accumu = np.zeros(colorPatches[0].shape, np.float)
        for i in range(self.count):
            cv2.accumulate(colorPatches[self.components[i]], accumu)
        self.avgImg = (accumu / self.count).astype(np.uint8)

        # get hist
        W, H, _ = colorPatches[0].shape
        self.bigImage = np.zeros((W, H * self.count, 3), np.uint8)
        for i in range(self.count):
            self.bigImage[:, i * H:(i + 1) *
                          H, :] = colorPatches[self.components[i]]
        bigImageHSV = cv2.cvtColor(self.bigImage, cv2.COLOR_BGR2HSV)
        self.hist = cv2.calcHist([bigImageHSV], [0, 1], None, [180, 256],
                                 [0, 180, 0, 256])
示例#15
0
    def accumulate_static(self, frame):
        """Accumulate frames for averaging for static analysis of the playing field.

        Only calculates the result once every avg frames. If reset is True, will purge old results.

        :arg frame: A frame of type CV_8UC3 and size self.shape
        :arg reset: Reset averaging
        """

        self.counter += 1
        cv2.accumulate(frame, self.accumulator)  # Is faster than uint16/uint8 ops in numpy
        #self.accumulator += frame

        if not self.counter % self.avg:  # if avg samples have been collected
            self._run_process_static()
            self.counter = 0
            self.accumulator = np.zeros(frame.shape, dtype=np.float32)
示例#16
0
def composePicture(pics):
    print(pics[0].shape[0:2])
    ret_mat = np.zeros(pics[0].shape[0:2])
    for pic in pics:
        pic = cv2.cvtColor(pic, cv2.COLOR_BGR2GRAY)
        print(pic.shape)
        ret_mat = cv2.accumulate(pic, ret_mat)
    ret_mat = ret_mat / len(pics)
    return ret_mat
示例#17
0
def main(videofile):
    # Initialize video stream
    cap = cv2.VideoCapture(videofile)

    frame = get_frame(cap)

    avg1 = np.float32(frame)
    avg2 = np.float32(frame)

    accum = np.float32(frame)

    num_bg_samples = 20
    for _ in xrange(num_bg_samples):
        frame = get_frame(cap)
        cv2.accumulate(frame, accum)

    bg = cv2.convertScaleAbs(accum / float(num_bg_samples))
    bgmean = bg.mean()
    bgstd = bg.std()

    cv2.imshow("Background", bg)
    while True:
        frame = get_frame(cap)

        fg = cv2.convertScaleAbs(bg - frame)
        _, fgbin = cv2.threshold(fg, bgmean + bgstd, 255, cv2.THRESH_BINARY)
        #        cv2.accumulateWeighted(frame, avg1, 0.1)
        #        cv2.accumulateWeighted(frame, avg2, 0.01)

        #        res1 = cv2.convertScaleAbs(avg1)
        #        res2 = cv2.convertScaleAbs(avg2)

        cv2.imshow('img', frame)
        cv2.imshow("FG", fg)
        cv2.imshow("FG bin", fgbin)
        #        cv2.imshow('avg1',res1)
        #        cv2.imshow('avg2',res2)

        key = cv2.waitKey(10)

        if key == 27:
            break
        if key == ord(" "):
            cv2.imwrite("screenshot.jpg", frame)
示例#18
0
def main(videofile):
    # Initialize video stream
    cap = cv2.VideoCapture(videofile)

    frame = get_frame(cap)
    
    avg1 = np.float32(frame)
    avg2 = np.float32(frame)
    
    accum = np.float32(frame)
    
    num_bg_samples = 20
    for _ in xrange(num_bg_samples):
        frame = get_frame(cap)
        cv2.accumulate(frame, accum)
        
    bg = cv2.convertScaleAbs(accum / float(num_bg_samples))
    bgmean = bg.mean()
    bgstd = bg.std()
    
    cv2.imshow("Background", bg)
    while True:
        frame = get_frame(cap)
        
        fg = cv2.convertScaleAbs(bg - frame)
        _, fgbin = cv2.threshold(fg, bgmean + bgstd, 255, cv2.THRESH_BINARY)
#        cv2.accumulateWeighted(frame, avg1, 0.1)
#        cv2.accumulateWeighted(frame, avg2, 0.01)
        
#        res1 = cv2.convertScaleAbs(avg1)
#        res2 = cv2.convertScaleAbs(avg2)
        
        cv2.imshow('img',frame)
        cv2.imshow("FG", fg)
        cv2.imshow("FG bin", fgbin)
#        cv2.imshow('avg1',res1)
#        cv2.imshow('avg2',res2)
        
        key = cv2.waitKey(10)
        
        if key == 27:
            break
        if key == ord(" "):
            cv2.imwrite("screenshot.jpg", frame)
def vio_sensor_cb(data):
    global cnt, active, imgs
    num_samples = 100  # number of image samples to take

    if cnt == num_samples and active:
        imgs['l'] /= num_samples
        imgs['r'] /= num_samples
        active = 0
        return

    left = np.float32(CvBridge().imgmsg_to_cv2(data.left_image, "mono8"))
    right = np.float32(CvBridge().imgmsg_to_cv2(data.right_image, "mono8"))

    if cnt == 0:
        imgs['l'] = left
        imgs['r'] = right
    else:
        cv2.accumulate(left, imgs['l'])
        cv2.accumulate(right, imgs['r'])

    cnt += 1
示例#20
0
def calculate_mean_over_interval(vid_frames):
    width, height = get_frame_dimensions(vid_frames[0])
    sum_of_frames = np.zeros((height, width, 3), np.float64)
    mean_of_frames = np.zeros((height, width, 3), np.float)

    i = 0
    for frame in vid_frames:
        sum_of_frames = cv2.accumulate(frame, sum_of_frames)
        i += 1
        # print("Sum: " + str(sum_of_frames[0, 0]))

    mean_of_frames = sum_of_frames / i
    return mean_of_frames
示例#21
0
def main(videofile):
    # Initialize video stream
    cap = cv2.VideoCapture(videofile)

    # Get video stream width and height
    width = cap.get(cv2.cv.CV_CAP_PROP_FRAME_WIDTH)
    height = cap.get(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT)

    edge_counts = np.zeros((height, width))

    num_bg_samples = 50
    for _ in xrange(num_bg_samples):
        cv2.accumulate(get_edge_frame(cap) / 255.0, edge_counts)

    bg_prob = edge_counts / float(num_bg_samples)
    print bg_prob.min()
    print bg_prob.max()

    bg_thresh = 0.5
    bg_pixels = bg_prob > bg_thresh

    while True:
        frame = get_frame(cap)
        cv2.imshow("Original", frame)

        edge_img = cv2.Canny(frame, 40, 100)
        cv2.imshow("Raw edges", edge_img)

        edge_nobg = edge_img.copy()
        edge_nobg[bg_pixels] = 0
        cv2.imshow("Edges no bg", edge_nobg)

        key = cv2.waitKey(10)

        if key == 27:
            break
        if key == ord(" "):
            cv2.imwrite("screenshot.jpg", edge_img)
示例#22
0
    def pile_up(self, new_frame):
        """
        Add a new frame to the current accumulation

        @rtype: integer, number of frames
        @param new_frame:
        @return: number of frames in the current pile
        """

        # Kill black level before the accumulation
        cv2.equalizeHist(new_frame, self.frame_eq)

        # Do the accumulation with motion compensation
        # -- we offset the previous accumulation
        if self.motion_comp and self.n_fused_frames > 0:
            b_success = self.compensate_interframe_motion(
                new_frame, 'shi_tomasi')

            if b_success:
                print "Frames aligned"
            else:
                print "Frames not aligned"

        # Handle a reset of the accumulation (TODO : Make it automatic if the scene changes a lot)
        if self.reset:
            self.frame_acc = np.float32(new_frame)
            self.reset = False

        # Pile up
        cv2.accumulate(new_frame, self.frame_acc)  # Just add pixel values
        cv2.normalize(np.power(self.frame_acc, self.gamma),
                      self.frame_acc_disp, 0., 1., cv2.NORM_MINMAX)

        # Update and return
        self.n_fused_frames += 1
        self.frame_prev = new_frame

        return self.n_fused_frames
示例#23
0
    def pile_up(self, new_frame):
        """
        Add a new frame to the current accumulation

        @rtype: integer, number of frames
        @param new_frame:
        @return: number of frames in the current pile
        """

        # Kill black level before the accumulation
        cv2.equalizeHist(new_frame, self.frame_eq)

        # Do the accumulation with motion compensation
        # -- we offset the previous accumulation
        if self.motion_comp and self.n_fused_frames > 0:
            b_success = self.compensate_interframe_motion(new_frame, self.motion_compensation_method)

            if b_success:
                print "Frames aligned"
            else:
                print "Frames not aligned"

        # Handle a reset of the accumulation
        # TODO: Make it automatic if the scene changes a lot
        if self.reset:
            self.frame_acc = np.float32(new_frame)
            self.reset = False

        # Pile up
        cv2.accumulate(new_frame, self.frame_acc)  # Just add pixel values
        cv2.normalize(np.power(self.frame_acc, self.gamma), self.frame_acc_disp, 0., 1., cv2.NORM_MINMAX)

        # Update and return
        self.n_fused_frames += 1
        self.frame_prev = new_frame

        return self.n_fused_frames
示例#24
0
def meanBackground(capture):
    # 获取视频长度
    frameNum = capture.get(cv2.CAP_PROP_FRAME_COUNT)
    success, frame = capture.read()
    # 初始化平均背景图像,初始化图像为视频首帧图像
    meanFrame = frame
    # 在后续处理中为了防止数值溢出,先进行数据类型转化,转为float32型,在处理完成后在转化为unint8格式进行保存
    meanFrame = meanFrame.astype(np.float32)
    cv2.imshow('original image', meanFrame)
    while True:
        # Capture frame-by-frame
        ret, frame = capture.read()
        if ret == True:
            tempframe = frame
            tempframe = tempframe.astype(np.float32)
            # 将所有帧的图像进行叠加
            cv2.accumulate(tempframe, meanFrame)

            cv2.imshow('original video', frame)
            cv2.imshow('temp frame', tempframe)
            cv2.imshow('mean video', meanFrame)

            # Press Q on keyboard to  exit
            if cv2.waitKey(33) & 0xFF == ord('q'):
                break
        # Break the loop
        else:
            break

    # cv2.imshow('accumulate image', meanFrame)
    # cv2.waitKey(0)
    meanFrame = meanFrame / frameNum  # 对叠加后的图像进行求平均
    meanFrame = meanFrame.astype(np.uint8)  # 从float32转为uint8格式
    cv2.imshow('result image', meanFrame)
    cv2.waitKey(300)
    cv2.imwrite('resultImage.jpg', meanFrame)
示例#25
0
def mean_image(img_array, img_shape=(28, 28, 1)):
    """Accept images as numpy array with images separated by rows
    and columns indicating pixel values"""
    assert (type(img_array) is type(pd.DataFrame())), TypeError

    # pre-allocate mean_img
    mean_img = np.zeros(img_shape, dtype=np.float32)

    # process files
    print(f"Computing mean image...")
    for file_count, idx in enumerate(range(img_array.shape[0])):
        temp_img = np.reshape(img_array.iloc[idx].to_numpy(), img_shape)
        mean_img = cv2.accumulate(temp_img.astype(dtype=np.float32), mean_img)

        if file_count % 10000 == 0:
            print(f"\tProcessed {file_count:0d} images.")

    # divide by n_images
    mean_img = np.divide(mean_img, file_count + 1)

    return mean_img
示例#26
0
def ComputeMean(dir_path, mean_file_write_to):
    #resize 尺寸
    protosize = (48, 48)

    #可以限定生成均值图像使用的图像数量
    mean_count = 100
    i = 0
    totalMean = np.zeros((protosize[0], protosize[1], 3), dtype=np.float)
    accedImage = np.zeros((protosize[0], protosize[1], 3), dtype=np.float)
    with open(dir_path, "r") as f:
        reader = csv.reader(f)
        for row in reader:
            if (i < mean_count):
                img_path = row[0]
                img_data = cv2.imread(img_path)
                img_resized = cv2.resize(img_data,
                                         protosize,
                                         interpolation=cv2.INTER_LINEAR)
                cv2.accumulate(img_resized, accedImage)

                #累计1000次计算一次均值速度会快一些,如果图像太多汇总起来再计算可能会溢出。
                if (i % 10 == 0 and i > 0):
                    accedImage = accedImage / float(mean_count)
                    cv2.accumulate(accedImage, totalMean)
                    accedImage = np.zeros((protosize[0], protosize[1], 3),
                                          dtype=np.float)
                print("processed: " + str(i))
                if i == mean_count:
                    break
                i = i + 1
        accedImage = accedImage / float(mean_count)
        cv2.accumulate(accedImage, totalMean)

    #for RGB image
    # totalMean=totalMean.transpose((2, 0, 1))

    # 存储为binaryproto
    blob = caffe_pb2.BlobProto()
    blob.channels = 3
    blob.height = protosize[0]
    blob.width = protosize[1]
    blob.num = 1
    blob.data.extend(totalMean.astype(float).flat)
    binaryproto_file = open(mean_file_write_to, 'wb')
    binaryproto_file.write(blob.SerializeToString())
    binaryproto_file.close()
示例#27
0
文件: box.py 项目: malonezi/meleedb
from __future__ import division
import cv2
import numpy as np
from sys import argv

vid = cv2.VideoCapture(argv[1])

total_frames = vid.get(7)
avg = np.zeros([vid.get(4), vid.get(3), 3])

while vid.grab():
    flag, frame = vid.retrieve()
    cv2.accumulate(frame, avg)

    # every 30 seconds
    if vid.get(1) % (60 * 30) == 0:
        print vid.get(1), "frames"
        hsv = cv2.convertScaleAbs(avg / vid.get(1))

        # extract saturation channel
        hsv = cv2.cvtColor(hsv, cv2.COLOR_RGB2HSV)[:, :, 1]

        cv2.imshow("b", hsv)
        cv2.waitKey(0)
        cv2.destroyAllWindows()

        # threshold for greys
        _, bin = cv2.threshold(hsv, 114,  0xFF, cv2.THRESH_TOZERO)
        _, bin = cv2.threshold(bin, 142, 0xFF, cv2.THRESH_BINARY_INV)

        cv2.imshow("b", bin)
示例#28
0
文件: pystab.py 项目: mompiou/PyStab
	ct=0
	
##################################################
#
# Read raw frame
#
##################################################
	print "Read raw video..."
	while(1):
		ret ,frame = vidcap.read()
		if ret == True :
			image[count] =frame
			image_rect[count] =frame
			if root.findall('spot')!=[]:
				if count>istart and count<iend:
					average_spot=cv2.accumulate(image[count],average_spot)
			count+=1
			if root.findall('preview')!=[]:
				if prevraw=='yes':
					cv2.imshow('Raw video',frame)
					k = cv2.waitKey(1) & 0xff
		else:
			break
	print "Done"
##################################################
#
# Spot option (camera imperfection removal)
#
##################################################			
	
	
示例#29
0
    # Start webcam
    webcam = cv2.VideoCapture()
    webcam.open(1)

    # Init image
    dummy, final_image = webcam.read()
    final_image = np.zeros( final_image.shape, dtype = np.float64)

    while True:
        dummy, new_image = webcam.read()
        dummy, mask = cv2.threshold(new_image, 125, 255, cv2.THRESH_BINARY)
        mask = mask[:, :, 0] | mask[:,:,1] | mask[:, :, 2]

        #cv2.accumulateWeighted(new_image, final_image, 0.5)
        cv2.accumulate(new_image, final_image, mask)

        cv2.imshow("Result", final_image.astype(np.uint8))
        k = cv2.waitKey(30) & 0xFF
        if k == ord('q'):
            break
        elif k == ord('r'):
            # Reset image:
            dummy, final_image = webcam.read()
            final_image = np.zeros( final_image.shape, dtype = np.float64)

    cv2.destroyAllWindows()
    cv2.imwrite(output_file, final_image)


示例#30
0
def blur_Radial(source, size, xm, ym, low_high=(-5, 5), path=None, plot=None):
    filter_V = np.zeros((size, size))
    filter_V[:, int((size - 1) / 2)] = np.ones(size)
    filter_V = filter_V / size

    high_ = low_high[1]
    low_ = low_high[0]
    sm_dims = high_ - low_ + 1
    sm = np.zeros((sm_dims, sm_dims))
    image_RGB = cv2.cvtColor(source, cv2.COLOR_BGR2RGB)

    sum = sumsq = None
    cnt = 0
    for dx in range(low_, high_ + 1):
        for dy in range(low_, high_ + 1):
            polar_image = linear_2_Polar(source, xm + dx, ym + dy)
            polar_rad = cv2.filter2D(polar_image, -1, filter_V)
            if sum is None:
                sum = np.zeros(polar_rad.shape, np.float32)
            if sumsq is None:
                sumsq = np.zeros(polar_rad.shape, np.float32)
            dx2 = cv2.accumulate(polar_rad, sum)
            dy2 = cv2.accumulateSquare(polar_rad, sumsq)
            cnt = cnt + 1

            xx = dx + high_
            yy = dy + high_

            if not (path is None) and Path(path).exists():
                fname = path + 'polar_rad_%s_%s.png' % (xx, yy)
                cv2.imwrite(fname, polar_rad)

            polar_rad = polar_2_Linear(source, polar_rad, xm + dx, ym + dy)
            if dx == 0 and dy == 0:
                polar_rad_0_0 = polar_rad
            sm[dx + high_, dy + high_] = similarity(source, polar_rad)

    sumsq = cv2.multiply(sumsq, cnt)
    var_image = cv2.multiply(sum, sum)
    var_image = cv2.subtract(sumsq, var_image)
    var_image = cv2.divide(var_image, cnt * (cnt - 1))
    c1, c2, c3 = cv2.split(var_image)
    cc = (c1 + c2 + c3) / 3
    ydata = cv2.reduce(cc, 0, cv2.REDUCE_SUM, dtype=-1).flatten()
    idx = 0
    for yd in ydata:
        print('%d %d' % (idx, yd))
        idx = idx + 1

    xdata = np.arange(len(ydata))
    resd = find_inflections(xdata, ydata)
    result = resd['inflection']
    first_valley = resd['xvalleyes']
    pupil_radii = (first_valley[0] + first_valley[1]) / 2.0

    if plot:
        f1, ax1 = plt.subplots(1)
        ax1.set_aspect('equal')
        ax1.imshow(image_RGB)
        ax1.set_title("Pupil Results")
        c = patches.Circle((xm, ym),
                           pupil_radii,
                           color='red',
                           linewidth=2,
                           fill=False)
        ax1.add_patch(c)
        plt.show()

    print(resd)
示例#31
0
mode = cv2.RETR_EXTERNAL
method = cv2.CHAIN_APPROX_SIMPLE

#2
t = 0
while True:
    ret, frame = cap.read()
    if not ret:
        break
    t += 1
    print("t = ", t)
    blur = cv2.GaussianBlur(frame, (5, 5), 0.0)
    #2-1
    if t < 50:
        cv2.accumulate(blur, acc_bgr)
        continue
    elif t == 50:
        bkg_bgr = acc_bgr / t
#2-2: t >= 50
##    diff_bgr = cv2.absdiff(np.float32(blur), bkg_bgr).astype(np.uint8)
    diff_bgr = np.uint8(cv2.absdiff(np.float32(blur), bkg_bgr))
    db, dg, dr = cv2.split(diff_bgr)
    ret, bb = cv2.threshold(db, TH, 255, cv2.THRESH_BINARY)
    ret, bg = cv2.threshold(dg, TH, 255, cv2.THRESH_BINARY)
    ret, br = cv2.threshold(dr, TH, 255, cv2.THRESH_BINARY)
    bImage = cv2.bitwise_or(bb, bg)
    bImage = cv2.bitwise_or(br, bImage)
    bImage = cv2.erode(bImage, None, 5)
    bImage = cv2.dilate(bImage, None, 5)
    bImage = cv2.erode(bImage, None, 7)
示例#32
0
        print e
####

while True:
    #camera.start_preview()
    raw=PiRGBArray(camera,size=(640,480))
    stream = camera.capture_continuous(raw,format="bgr",use_video_port=True)
    #print("[INFO]sampling frames from picamera module")
    #fps=FPS().start()
    for(i,f) in enumerate(stream):
        frame = f.array
        if ac_count==0:
            ac=frame
            continue
        elif ac_count < 5:
            ac=cv2.accumulate(ac,frame)
            continue
        elif ac_count == 5:
            ac_count =0
            
        img=frame.copy()
        frame = frame[crop_y:crop_y+crop_h,crop_x:crop_x+crop_w]
        # Step 1 : grayscale
        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        # Step 2 : medianBlur
        median = cv2.medianBlur(gray,7)
		# Step 3 : find lastframe
        if counter == 0:
            lastframe = median
            counter = 1
            break
 cv2.namedWindow("input")
 cv2.namedWindow("sig2")
 cv2.namedWindow("detect")
 BGsample = 20 # number of frames to gather BG samples from at start of capture
 success, img = cap.read()
 width = cap.get(3)
 height = cap.get(4)
 if success:
     acc = np.zeros((height, width), np.float32) # 32 bit accumulator
     sqacc = np.zeros((height, width), np.float32) # 32 bit accumulator
     for i in range(20): a = cap.read() # dummy to warm up sensor
     # gather BG samples
     for i in range(BGsample):
         success, img = cap.read()
         frame = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
         cv2.accumulate(frame, acc)
         cv2.accumulateSquare(frame, sqacc)
     #
     M = acc/float(BGsample)
     sqaccM = sqacc/float(BGsample)
     M2 = M*M
     sig2 = sqaccM-M2
     # have BG samples now
     # calculate upper and lower bounds of detection window around mean.
     # coerce into 8bit image space for cv2.inRange compare
     detectmin = cv2.convertScaleAbs(M-sig2)
     detectmax = cv2.convertScaleAbs(M+sig2)
     # start FG detection
     key = -1
     while(key < 0):
         success, img = cap.read()
示例#34
0
                 int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)))

acc_gray = np.zeros(shape=(height, width), dtype=np.float32)
acc_bgr = np.zeros(shape=(height, width, 3), dtype=np.float32)
t = 0

#2
while True:
    ret, frame = cap.read()
    if not ret:
        break
    t += 1
    print('t =', t)
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    cv2.accumulate(gray, acc_gray)
    avg_gray = acc_gray / t
    dst_gray = cv2.convertScaleAbs(avg_gray)

    cv2.accumulate(frame, acc_bgr)
    avg_bgr = acc_bgr / t
    dst_bgr = cv2.convertScaleAbs(avg_bgr)

    cv2.imshow('frame', frame)
    cv2.imshow('dst_gray', dst_gray)
    cv2.imshow('dst_bgr', dst_bgr)
    key = cv2.waitKey(20)
    if key == 27:
        break
#3
if cap.isOpened(): cap.release()
示例#35
0
ref = ref[0:sig_n - 1]
#ref = np.array([-1.0, 1.0, -6.0, 6.0, -1.0, 1.0, -6.0, 6.0, -1.0, 1.0, -5.0, 4.0, 0.0,1.0, -6.0, 6.0, -1.0, 1.0, -6.0, 6.0, -1.0, 1.0, -5.0, 4.0, 0.0,1.0, -6.0, 6.0, -1.0, 1.0, -6.0, 6.0, -1.0, 1.0, -5.0, 4.0, 0.0])
#ref = ref * 3
f = np.fft.fft(ref)
fshift = np.fft.fftshift(f)
y1 = (np.abs(fshift))
Ar = -y1.conjugate()
while (d <= sig_n):
    ret, frame = cap.read()
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    grad = np.int8(gray - pgray)  # change the intensity type to iny8
    if (d != 0):
        arr.append(grad)

        accn = np.float64(grad)
        cv2.accumulate(accn, acc)
        cv2.convertScaleAbs(acc, accu)
    pgray = gray
    d += 1

    if (d == sig_n):
        for k in range(d - 1):
            sr[k, :, :] = arr[k]
    if (d == sig_n):
        for v in range(rows):
            for u in range(cols):
                if (accu.item(v, u)) != 0:
                    for k in range(d - 1):
                        sg[k] = sr[k][v][u]
                    # if u==347 and v==615:
                    #	  print sg
示例#36
0
def bg_average(average, images,count):
    cv2.accumulate(images, average)
    average = average / count
    average = np.uint8(average)
    return average
示例#37
0
def detector(v_name, n, th):
    cap = cv2.VideoCapture(v_name)
    ret, frame = cap.read()
    rows, cols = frame.shape[:2]

    # ref_image = frame[:,:,1] # reference is the first frame
    ref_image = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    gray = np.zeros((rows, cols), np.uint8)
    pgray = np.zeros((rows, cols), np.uint8)
    grad = np.zeros((rows, cols), np.int8)
    acc = np.zeros((rows, cols), np.float64)
    accu = np.zeros((rows, cols), np.uint8)
    dmax = n  # number of processed frames
    dst = np.zeros((rows, cols, 3), np.uint8)
    sr = np.zeros((dmax, rows, cols))

    # dst[:] = 255
    d = 0
    arr = []
    sg = [None] * dmax
    # reference signal
    ref = np.array(
        [
            5.0, 0.0, 0.0, -5.0, 5.0, 0.0, 0.0, -5.0, 5.0, 0.0, 0.0, -5.0, 5.0,
            0.0, 0.0, -5.0, 5.0, 0.0, 0.0, -5.0, 5.0, 0.0, 0.0, -5.0, 5.0, 0.0,
            0.0, -5.0, 5.0, 0.0, 0.0, -5.0, 5.0, 0.0, 0.0, -5.0, 5.0, 0.0, 0.0,
            -5.0, 5.0, 0.0, 0.0, -5.0, 5.0, 0.0, 0.0, -5.0, 5.0, 0.0, 0.0,
            -5.0, 5.0, 0.0, 0.0, -5.0, 5.0, 0.0, 0.0, -5.0
        ]
    )  # 2 or 3 is mainly good for straight v and h lines and still good for slopes
    ref = ref[0:dmax]
    # ref=np.array([ 0.0, 3.0, 0.0, -3, 0.0, 3.0,0, -3.0,0.0,3.0, 0, -3.0, 0.0, 3.0, 0, -3.0, 0.0, 3.0, 0,-3.0 ])#4 corner model good for corner and sloped lines or curves
    f = np.fft.fft(ref)
    fshift = np.fft.fftshift(f)
    y1 = (np.abs(fshift))
    Ar = -y1.conjugate()
    af = scipy.fft(ref)
    pgray = ref_image
    f = open("signal4p.txt", "w")
    ###############
    # start to process a set of oscillation frames

    while (d <= dmax):
        ret, frame = cap.read()
        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        # gray = frame[:,:,1]
        grad = np.int8(gray - pgray)
        # align
        # shift, error, diffphase = register_translation(gray, ref_image, 100) #measure shift
        # if (d!=0) :
        # shift=(-shift[0],-shift[1])
        # offset_image = fourier_shift(np.fft.fft2(grad), shift)
        # grad1 = np.fft.ifft2(offset_image)
        # grad2 = np.real(grad1) #np.abs(grad1)
        arr.append(grad)  # append(np.real(grad1))
        # accn= np.float64(grad2)
        cv2.accumulate(np.uint8(grad),
                       acc)  # (grad2,acc) # accumulated gradients
        cv2.convertScaleAbs(acc, accu)  # get the unsigned acc
        pgray = gray
        '''newimg = cv2.resize(accu, (int(cols / 2), int(rows / 2)))
        cv2.imshow('Image', newimg)
        if cv2.waitKey(10) & 0xFF == ord('q'):
            break'''
        d += 1
    ###########
    ### load sr as 3d array
    for k in range(dmax):
        sr[k, :, :] = arr[k]

    for v in range(rows):
        for u in range(cols):
            if (accu.item(v,
                          u)) != 0:  # if accumulated pixel value is non zero
                for k in range(dmax):
                    sg[k] = sr[k][v][u]

                if np.std(sg) > 1.5:
                    f.write('%d %d \n' % (u, v))
                    for l in range(dmax):
                        f.write('%d\n' % sg[l])
                    f1 = np.fft.fft(sg)
                    fshift1 = np.fft.fftshift(f1)
                    y2 = (np.abs(fshift1))
                    cc = np.abs(np.corrcoef(y2, y1))
                    bf = scipy.fft(sg)
                    c = scipy.ifft(af * scipy.conj(bf))
                    ps = np.argmax(abs(c))
                    # get orientation of line and color code it
                    ps = ps % 4  # phase shift over integer no of cycles
                    if cc[1][0] > th:
                        if ps == 0:
                            dst.itemset((v, u, 0), 255)
                            dst.itemset((v, u, 1), 255)
                            dst.itemset((v, u, 2), 255)
                        if ps == 1:
                            dst.itemset((v, u, 0), 255)
                            dst.itemset((v, u, 1), 255)
                            dst.itemset((v, u, 2), 255)
                        if ps == 2:
                            dst.itemset((v, u, 0), 255)
                            dst.itemset((v, u, 1), 255)
                            dst.itemset((v, u, 2), 255)
                        if ps == 3:
                            dst.itemset((v, u, 0), 255)
                            dst.itemset((v, u, 1), 255)
                            dst.itemset((v, u, 2), 255)

    cv2.imwrite("res_m.jpg", dst)
    t2 = time()
    print
    'time is %f' % (t2 - t0)
    cap.release()
    f.close()
    cv2.destroyAllWindows()
    return dst
numberOfIterations = 1
print("Number of loop iterations: " + str(numberOfIterations))

dstSW = np.zeros((height, width), np.float)

xFimgY1 = mem_manager.cma_array(
    (height, width), np.uint8)  #allocated physically contiguous numpy array
xFimgY1[:] = imgY1[:]  # copy source data

xFdst = mem_manager.cma_array(
    (height, width), np.uint16)  #allocated physically contiguous numpy array

print("Start SW loop")
startSW = time.time()
for i in range(numberOfIterations):
    cv2.accumulate(imgY1, dst=dstSW)  #accumulate on ARM
stopSW = time.time()
print("SW loop finished")

print("Start HW loop")
startPL = time.time()
for i in range(numberOfIterations):
    xv2.accumulate(
        xFimgY1, dst=xFdst
    )  #accumulate offloaded to PL, working on physically continuous numpy arrays
stopPL = time.time()
print("HW loop finished")

print("SW frames per second: ", ((numberOfIterations) / (stopSW - startSW)))
print("PL frames per second: ", ((numberOfIterations) / (stopPL - startPL)))