Beispiel #1
0
def testAerialSequence():
    masks = []
    for i in range(video_frames.shape[2] - 1):
        It = video_frames[:, :, i]
        It1 = video_frames[:, :, i + 1]
        print(It.shape)
        mask = SubtractDominantMotion(It, It1)
        masks.append(mask)

        # if i%50==0:
        # 	plt.subplot(211)
        # 	plt.imshow(mask)
        # 	plt.show()

    np.save('invercompose_masks.npy', masks)
    return
Beispiel #2
0
def main():

    frames = np.load('../data/aerialseq.npy')

    fig, ax = plt.subplots(1)
    rect_save = []

    for i in range(frames.shape[-1] - 1):
        p = SubtractDominantMotion.SubtractDominantMotion(
            frames[:, :, i], frames[:, :, i + 1])

        points = np.where(p)
        plt.plot(points[1], points[0], 'g.')
        plt.imshow(frames[:, :, i], cmap='gray')
        plt.pause(0.0001)

        if i in [30, 60, 90, 120]:
            plt.savefig('airseq_' + str(i) + '.png')

        plt.clf()
from SubtractDominantMotion import *
from LucasKanadeAffine import *
import time

frames = np.load('../data/aerialseq.npy')
time_total = 0
seq_len = frames.shape[2]

for i in range(seq_len):
    if i == 0:
        continue
    print("Processing frame %d" % i)

    start = time.time()
    image1 = frames[:, :, i - 1]
    image2 = frames[:, :, i]
    mask = SubtractDominantMotion(image1, image2)
    end = time.time()
    time_total += end - start

    if i == 31 or i == 61 or i == 91 or i == 121:
        plt.figure()
        plt.imshow(image1, cmap='gray')
        for r in range(mask.shape[0] - 1):
            for c in range(mask.shape[1] - 1):
                if mask[r, c]:
                    plt.scatter(c, r, s=1, c='r', alpha=0.5)
        plt.show()

print('Finished, the tracking frequency is %.4f' % (seq_len / time_total))
parser.add_argument('--tolerance', type=float, default=0.2, help='binary threshold of intensity difference when computing the mask')
args = parser.parse_args()
num_iters = args.num_iters
threshold = args.threshold
tolerance = args.tolerance

seq = np.load('../data/aerialseq.npy')
print(seq.shape)

num_frames = seq.shape[2]

fig = plt.figure()

for i in range(0, num_frames-1):
    print('frame: ', i)
    mask = SubtractDominantMotion(seq[:,:,i], seq[:,:,i+1], threshold,num_iters,tolerance)
    img1 = seq[:,:,i-1]
    img2 = seq[:,:,i]
    #masks = mask[:,:,i-1]
    
    highlight = np.where(mask,1.0,img1)
    img_stack = np.dstack((img1,img1))
    superimpose_img = np.dstack((img_stack,highlight))
    
    plt.imshow(superimpose_img)
    #plt.show()
    
    
    if i in [30, 60, 90, 120]:
        plt.savefig("aerialseq"+str(i)+".png")
    plt.pause(1.0)
Beispiel #5
0
from SubtractDominantMotion import *

# write your script here, we recommend the above libraries for making your animation
video_path = '../data/aerialseq.npy'
frame_step = 1

data = np.load(video_path)
frame_num = data.shape[2]

masks = np.zeros(data.shape)
for i in np.arange(1, frame_num, frame_step):
    print("Image:", i)
    # pdb.set_trace()
    img_pre = data[:, :, i - 1]
    img_cur = data[:, :, i]
    mask = SubtractDominantMotion(img_pre, img_cur)
    masks[:, :, i] = mask
    # plt.imshow(data[:,:,i], cmap='gray')
    # plt.imshow(mask, cmap='Reds', alpha=0.4)
    # plt.show()
    # pdb.set_trace()

# visualization
frame = [30, 60, 90, 120]
fig, axs = plt.subplots(1, 4, figsize=(8, 2))
for i, k in enumerate(frame):
    axs[i].imshow(data[:, :, k], cmap='gray')
    axs[i].imshow(masks[:, :, k], cmap='Reds', alpha=0.4)
plt.show()
Beispiel #6
0
num_iters = args.num_iters
threshold = args.threshold
tolerance = args.tolerance

seq = np.load('../data/aerialseq.npy')

imH, imW, frames = np.shape(seq)
# print(imH,imW)
print(frames)
# rect = [101., 61., 155., 107.]
start = time.time()
for i in range(frames - 1):
    #print(i)
    image1 = seq[:, :, i]
    image2 = seq[:, :, i + 1]
    mask = SubtractDominantMotion.SubtractDominantMotion(
        image1, image2, threshold, num_iters, tolerance)

    if (i == 29) or (i == 59) or (i == 89) or (i == 119):
        # since we are plotting framee 30, 60, 90, 120 are plotted
        pic = plt.figure()
        plt.imshow(image2, cmap='gray')
        plt.axis('off')
        plt.title("Frame  %d " % (i + 1))
        # print(mask)
        for w in range(mask.shape[0] - 1):
            for h in range(mask.shape[1] - 1):
                if mask[w, h]:
                    plt.scatter(h, w, s=1, c='r', alpha=0.5)
        plt.show()

stop = time.time()
from matplotlib import animation
import matplotlib.patches as patches
import os
import cv2

import SubtractDominantMotion

# write your script here, we recommend the above libraries for making your animation
if __name__ == '__main__':
    result_dir = '../result/'
    if not os.path.exists(result_dir):
        os.makedirs(result_dir)
    aerial_data = np.load('../data/aerialseq.npy')
    frame = aerial_data[:, :, 0]

    for i in range(1, aerial_data.shape[2]):
        next_frame = aerial_data[:, :, i]
        mask = SubtractDominantMotion.SubtractDominantMotion(frame, next_frame)

        tmp_img = np.zeros((next_frame.shape[0], next_frame.shape[1], 3))
        tmp_img[:, :, 0] = next_frame
        tmp_img[:, :, 1] = next_frame
        tmp_img[:, :, 2] = next_frame
        tmp_img[:, :, 0][mask == 1] = 1

        if i in [30, 60, 90, 120]:
            cv2.imwrite(os.path.join(result_dir, 'q3-3_{}.jpg'.format(i)),
                        tmp_img * 255)

        frame = next_frame
Beispiel #8
0
from matplotlib import animation
import matplotlib.patches as patches

import SubtractDominantMotion

carseq_data = np.load("../data/aerialseq.npy")
frame_num = carseq_data.shape[2]

for i in range(frame_num - 1):

    # Load the template and the input images
    It = carseq_data[:, :, i]
    It1 = carseq_data[:, :, i + 1]

    print(i)

    # find out the mask that shows the main motion
    mask = SubtractDominantMotion.SubtractDominantMotion(It, It1)

    # plot the tracking
    fig = plt.figure()
    plt.imshow(It1)
    locs = np.transpose(np.nonzero(mask))
    plt.plot(locs[:, 1], locs[:, 0], 'b.')
    plt.draw()

    # save the tracking
    file_name = 'md_%s.png' % (str(i + 1))
    plt.savefig(file_name)
    plt.close()