我从校园里的网络摄像头拍摄了多张截图,我已经拍摄了300张截图的平均值,它给了我一张有很多鬼影的人的照片。我试图弄清楚如何获得图像之间的差异图像,以便我可以将差异更改为红色以更清楚地显示。我只是需要一些帮助来了解从哪里开始。我知道我需要在第二个图像中减去第一个图像,然后在第三个图像中减去该图像,但我不确定如何在Python中执行此操作。
import os, os.path, time
import matplotlib.pyplot as mplot
from PIL import Image
import numpy as np
files=os.listdir('./images')
print files
image=[]
for file in files:
img=Image.open('./images/'+file)
img=np.float32(img)
image.append(img)
avg_img=[]
for img in image:
try:
avg_img+=img
except:
avg_img=img
avg_img/=len(image)
avg_img=np.clip(avg_img, 0, 255)
avg_img=np.uint8(avg_img)
mplot.imshow(avg_img)
mplot.show()
答案 0 :(得分:1)
有很多方法可以做到这一点,但我会首先保持最接近你现有的方式。然后我将展示一种更紧凑的方式。
import {
customElement,
TaskQueue,
bindable,
ViewCompiler,
ViewSlot,
View,
ViewResources,
Container,
ViewFactory,
inlineView,
inject,
DOM
} from "aurelia-framework";
@customElement("dynamic-html")
@inlineView("<template><div></div></template>")
@inject(DOM.Element, TaskQueue, Container, ViewCompiler)
export class DynamicHtml {
@bindable()
public html: string;
public element: HTMLElement;
private tq: TaskQueue;
private container: Container;
private viewCompiler: ViewCompiler;
private runtimeView: View;
private runtimeViewSlot: ViewSlot;
private runtimeViewFactory: ViewFactory;
private runtimeViewAnchor: HTMLDivElement;
constructor(element, tq, container, viewCompiler) {
this.element = <HTMLElement>element;
this.tq = tq;
this.container = container;
this.viewCompiler = viewCompiler;
}
public bindingContext: any;
public overrideContext: any;
public bind(bindingContext: any, overrideContext: any): void {
this.bindingContext = bindingContext;
this.overrideContext = overrideContext;
if (this.html) {
this.htmlChanged(this.html, undefined);
}
}
public unbind(): void {
this.disposeView();
this.bindingContext = null;
this.overrideContext = null;
}
public needsApply: boolean = false;
public isAttached: boolean = false;
public attached(): void {
this.runtimeViewAnchor = <HTMLDivElement>this.element.firstElementChild;
this.isAttached = true;
if (this.needsApply) {
this.needsApply = false;
this.apply();
}
}
public detached(): void {
this.isAttached = false;
this.runtimeViewAnchor = null;
}
private htmlChanged(newValue: string, oldValue: void): void {
if (newValue) {
if (this.isAttached) {
this.tq.queueMicroTask(() => {
this.apply();
});
} else {
this.needsApply = true;
}
} else {
if (this.isApplied) {
this.disposeView();
}
}
}
private isApplied: boolean = false;
private apply(): void {
if (this.isApplied) {
this.disposeView();
}
this.compileView();
}
private disposeView(): void {
if (this.runtimeViewSlot) {
this.runtimeViewSlot.unbind();
this.runtimeViewSlot.detached();
this.runtimeViewSlot.removeAll();
this.runtimeViewSlot = null;
}
if (this.runtimeViewFactory) {
this.runtimeViewFactory = null;
}
if (this.runtimeView) {
this.runtimeView = null;
}
this.isApplied = false;
}
private compileView(): void {
this.runtimeViewFactory = createViewFactory(this.viewCompiler, this.container, this.html);
this.runtimeView = createView(this.runtimeViewFactory, this.container);
this.runtimeViewSlot = createViewSlot(this.runtimeViewAnchor);
this.runtimeViewSlot.add(this.runtimeView);
this.runtimeViewSlot.bind(this.bindingContext, this.overrideContext);
this.runtimeViewSlot.attached();
this.isApplied = true;
}
}
function createViewFactory(viewCompiler: ViewCompiler, container: Container, html: string): ViewFactory {
if (!html.startsWith("<template>")) {
html = `<template>${html}</template>`;
}
let viewResources: ViewResources = container.get(ViewResources);
let viewFactory = viewCompiler.compile(html, viewResources);
return viewFactory;
}
function createView(viewFactory: ViewFactory, container: Container): View {
let childContainer = container.createChild();
let view = viewFactory.create(childContainer);
return view;
}
function createViewSlot(containerElement: Element): ViewSlot {
let viewSlot = new ViewSlot(containerElement, true);
return viewSlot;
}
使用numpy这是一种更简单的方法。
import os, os.path, time
import matplotlib.pyplot as mplot
from PIL import Image
import numpy as np
files=os.listdir('./images')
print files
image=[]
for file in files:
img=Image.open('./images/'+file)
img=np.float32(img)
image.append(img)
avg_img=np.zeros_like(image[0])
for img in image:
avg_img += img
avg_img/=len(image)
avg_img=np.clip(avg_img, 0, 255)
avg_img=np.uint8(avg_img)
mplot.imshow(avg_img)
mplot.show()
# get series of differences
differences = []
for img0, img1 in zip(image[:-1], image[1:]):
differences.append(img1 - img0)
我怀疑你的网络摄像头图像存储浮动,但也许就是这种情况。