SSD。图像恢复(帧t-1和t)。蟒

时间:2017-12-11 05:09:50

标签: python opencv image-processing frames

我尝试从当前(t)恢复前一帧(t-1)。此代码正在运行但质量不佳。我找不到错误=( 可能有人有什么想法吗?

帧:

enter image description here

enter image description here

import random
import numpy as np
import cv2


def get_ssd(first_block, second_block):
    return np.sum((first_block[:, :] - second_block[:, :]) ** 2)


def get_sad(first_block, second_block):
    return np.sum(abs((first_block[:, :] - second_block[:, :])))


def sliding_block(image, block_size, step):
    for w in range(0, image.shape[0], step):
        if w + block_size[0] > image.shape[0]:
            w = image.shape[0] - block_size[0]
        for h in range(0, image.shape[1], step):
            if h + block_size[1] > image.shape[1]:
                h = image.shape[1] - block_size[1]
            yield (w, h, image[w:w + block_size[0], h:h + block_size[1]])


def replace_block(a, b, c, position_x, position_y):
    a[position_x:position_x + BLOCK_SIZE[0], position_y:position_y + BLOCK_SIZE[1]] += b[0:0 + BLOCK_SIZE[0],
                                                                                       0:0 + BLOCK_SIZE[1]]
    c[position_x:position_x + BLOCK_SIZE[0], position_y:position_y + BLOCK_SIZE[1]] += 1


def search_window(image, x, y, size):
    if x - size < 0:
        width_s = 0
        width_e = x + size
    elif x + size > image.shape[0]:
        width_s = x - size
        width_e = image.shape[0]
    else:
        width_s = x - size
        width_e = x + size

    if y - size < 0:
        height_s = 0
        height_e = y + size
    elif y + size > image.shape[1]:
        height_s = y - size
        height_e = image.shape[1]
    else:
        height_s = y - size
        height_e = y + size

    return width_s, height_s, image[width_s:width_e, height_s:height_e]


t_1 = cv2.imread("frame20.jpg", 0)
t = cv2.imread("frame21.jpg", 0)

t_rec = t_1.copy()
t_acc = t_1.copy()
t_vec = t_1.copy()
t_rec2 = t_1.copy()
t_rec2.fill(0)
t_vec.fill(255)
t_acc.fill(0)
t_rec.fill(0)

block_memory = []
BLOCK_SIZE = [10, 10]
STEP_SIZE = 10

for (x, y, im_block) in sliding_block(t_1, BLOCK_SIZE, STEP_SIZE):
    sad = 99999
    (x_start, y_start, im_window) = search_window(t, x, y, 30)
    for width in range(0, im_window.shape[0] - BLOCK_SIZE[0]):
        for height in range(0, im_window.shape[1] - BLOCK_SIZE[1]):
            sad_new = get_ssd(im_block, im_window[width:width + BLOCK_SIZE[0], height:height + BLOCK_SIZE[1]])
            if sad > sad_new:
                sad = sad_new
                to_x = x_start + width
                to_y = y_start + height
                im_w = im_window[width: width + BLOCK_SIZE[0], height: height + BLOCK_SIZE[1]]

    # block_memory.append((x, y, to_x, to_y, sad))
    replace_block(t_rec, im_w, t_acc, x, y)
    cv2.line(t_vec, (to_y, to_x), (y, x), (random.randint(0, 256)), 1, 8, 0)

for i in range(0, len(t_rec)):
    for j in range(0, len(t_rec[0])):
        t_rec2[i][j] = round(t_rec[i][j] / t_acc[i][j])

cv2.imshow("Recover", t_rec2)
cv2.imshow("Vectors", t_vec)
cv2.waitKey(0)

我尝试从当前(t)恢复前一帧(t-1)。此代码正在运行但质量不佳。我找不到错误=( 可能有人有什么想法吗?

结果:

enter image description here

C ++上具有相同参数的相同算法:

enter image description here

0 个答案:

没有答案