clc;
clear;
close all;
warning off;
addpath 'func\'
path = [cd '/Save_Temp/RGB/'];
files = dir([path '*.' 'jpg']);
files = sort({files.name});
T = size(files, 2); % Number of frames
[HEIGHT, WIDTH, C] = size(imresize(imread([path files{1}]),[128,100])); % Get the dimension of one picture
D = HEIGHT * WIDTH;
image_sequence = zeros(HEIGHT, WIDTH, C, T, 'uint8'); % Used for image dispaly
data = zeros(HEIGHT, WIDTH, C, T, 'uint8');
% load data from the image sequence
for tt = 1:T
im = imresize(imread([path files{tt}]),[128,100]);
data(:, :, :, tt) = im;
image_sequence(:, :, :, tt) = im;
end
% Turn each picture into a column vector of color triplets
data = reshape(data, size(data, 1) * size(data, 2), size(data, 3), ...
size(data, 4));
%--------------------------------------------------------------------------
%-------------------Initialise mix_gaussian parameters---------------------
K = 3; % how many components does the mixture have
ALPHA = 0.01; % How fast to adapt the component weights
RHO = 0.01; % How fast to adapt the component means and covariances
DEVIATION_SQ_THRESH = 7^2; % Threshold used to find matching versus unmatching components.
INIT_VARIANCE = 3; % Initial variance for newly placed components
INIT_MIXPROP = 0.00001; % Initial prior for newly placed components
BACKGROUND_THRESH = 0.8; % Percentage of weight that must be accounted for by background models.
COMPONENT_THRESH = 10; % Filter out connected components of smaller size
%--------------------------------------------------------------------------
%----------------------------model initialise------------------------------
% Place the mean of first Gaussian on the value of first observed pixel.
% Place all other Gaussians mean randomly.
Mus = zeros(D, K, C);
Mus(:, 1, :) = double(data(:, :, 1));
Mus(:, 2:K, :) = rand([D, K-1, C]) * 255;
% Genenrally, R��G��B component are independent and have the same variance
% We just initialise the variance of the Gaussian with a relatively high value.
Sigmas = ones(D, K, C) * INIT_VARIANCE;
% Give each Gaussian except the first one zero weight. This ensures that uninitialised
% Gaussians (i.e. everything but the first) will be replaced first time we have the chance.
Weights = [ones(D, 1), zeros(D, K-1)];
%--------------------------------------------------------------------------
%---------------------------------- iteration -----------------------------
deviations_squared = zeros(D, K); % the deviation from the mean of the Gaussian
matched_gaussian = zeros(D, K); % All gaussians that matched a pixel
accumulated_weights = zeros(D, K);
background = zeros(HEIGHT, WIDTH, T);
shadow_mark = zeros(HEIGHT, WIDTH);
% Used for image dispaly
foreground_map_sequence = zeros(HEIGHT, WIDTH, T);
foreground_with_map_sequence = zeros(HEIGHT, WIDTH, T);
foreground_map_adjust_sequence = zeros(HEIGHT, WIDTH, T);
background_sequence = zeros(HEIGHT, WIDTH, C, T);
for tt = 1:T
tt
image = data(:, :, tt);
%----------------------Light and shadow correction---------------------
% Gama correction
% ratio = sum(sum(background_hsv(:,:,3)))/(sum(sum(background_hsv(:,:,3)))...
% + sum(sum(background_hsv(:,:,3))));
% image_hsv(:,:,3) = image_hsv(:,:,3).^ratio;
% image_adjust = hsv2rgb(image_hsv) * 255;
%----------------------------------------------------------------------
%---------------------------match definition---------------------------
% Compute the squared Mahalanobis distance. This only works if
% Sigmas represents a diagonal covariance matrix in the form
% of a vector of diagonal entries.
for kk = 1:K
% Center the image around the means of the kk-th Gaussian
% data_centered = double(reshape(image_adjust, D, C)- reshape(Mus(:, kk, :), D, C));
data_centered = double(data(:, :, tt)) - reshape(Mus(:, kk, :), D, C);
deviations_squared(:, kk) = sum((data_centered .^ 2) ./ reshape(Sigmas(:, kk, :), D, C), 2);
end
% Now see if any of the Gaussians matches to within DEVIATION_SQ_THRESH deviations.
% The best match for a pixel has min deviation.
[junk, index] = min(deviations_squared, [], 2);
% From index, whose elements are indeces of the minimum element of each
% row, compute an indicator matrix that has a 1 in the corresponding
% position of the min element and a 0 otherwise.
matched_gaussian = zeros(size(deviations_squared));
matched_gaussian(sub2ind(size(deviations_squared), 1:length(index), index')) = ones(D, 1);
matched_gaussian = matched_gaussian & (deviations_squared < DEVIATION_SQ_THRESH);
%----------------------------------------------------------------------
%---------------------------Parameters Update--------------------------
% Now update the weights. Increment weight for the selected Gaussian (if any),
% and decrement weights for all other Gaussians.
Weights = (1 - ALPHA) .* Weights + ALPHA .* matched_gaussian;
% Adjust Mus and Sigmas for matching distributions.
for kk = 1:K
pixel_matched = repmat(matched_gaussian(:, kk), 1, C);
pixel_unmatched = abs(pixel_matched - 1); % Inverted and mutually exclusive
Mu_kk = reshape(Mus(:, kk, :), D, C);
Sigma_kk = reshape(Sigmas(:, kk, :), D, C);
Mus(:, kk, :) = pixel_unmatched .* Mu_kk + ...
pixel_matched .* (((1 - RHO) .* Mu_kk) + ...
(RHO .* double(image)));
% Get updated Mus; Sigmas is still unchanged
Mu_kk = reshape(Mus(:, kk, :), D, C);
Sigmas(:, kk, :) = pixel_unmatched .* Sigma_kk + ...
pixel_matched .* (((1 - RHO) .* Sigma_kk) + ...
repmat((RHO .* sum((double(image) - Mu_kk) .^ 2, 2)), 1, C));
end
% Maintain an indicator matrix of those components that were replaced because no component matched.
replaced_gaussian = zeros(D, K);
% Find those pixels which have no Gaussian that matches
mismatched = find(sum(matched_gaussian, 2) == 0);
% A method that works well: Replace the component we
% are least confident in. This includes weight in the choice of
% component.
for ii = 1:length(mismatched)
[junk, index] = min(Weights(mismatched(ii), :) ./ sqrt(Sigmas(mismatched(ii), :, 1)));
% Mark that this Gaussian will be replaced
replaced_gaussian(mismatched(ii), index) = 1;
% With a distribution that has the current pixel as mean
Mus(mismatched(ii), index, :) = image(mismatched(ii), :);
% And a relatively wide variance
Sigmas(mismatched(ii), index, :) = ones(1, C) * INIT_VARIANCE;
% Also set the weight to be relatively small
Weights(mismatched(ii), index) = INIT_MIXPROP;
end
% Now renormalise the weights so they still sum to 1
Weights = Weights ./ repmat(sum(Weights, 2), 1, K);
active_gaussian = matched_gaussian + replaced_gaussian;
%----------------------------------------------------------------------
%--------------------------background Segment--------------------------
% Find maximum weight/sigma per row.
[junk, index] = sort(Weights ./ sqrt(Sigmas(:, :, 1)), 2, 'descend');
% Record indeces of those each pixel's component we are most confident
% in, so that we can display a single background estimate later. While
% our model allows for a multi-modal background, this is a useful
% visualisa


fpga和matlab
- 粉丝: 18w+
- 资源: 2657
最新资源
- 基于多智能体事件触发的一致性控制:状态轨迹、控制输入与事件触发图详解(附参考图形),基于多智能体事件触发的一致性控制:状态轨迹、控制输入与事件触发机制详解及图形展示,多智能体事件触发、一致性控制 状态
- 基于BP神经网络结合高阶累积量的信号识别技术:实现BPSK、QPSK、8PSK、32QAM信号的准确识别与Matlab实现详解,BP神经网络结合高阶累积量信号识别技术:实现BPSK、QPSK、8PSK
- 大学生校园社团小程序源码云开发前后端完整代码.zip
- 高性能直流无刷电机参数详解:外径41mm,转速高达6000rpm,功率达200W,槽满率67.5%,效率80.7%,最大输出功率达320W,高性能直流无刷电机参数详解:转速高达6000rpm,功率达2
- 小麦种子图像分类数据集【已标注,约2,000张数据】
- 校园帮-WeChat-基于微信小程序的“校园帮”系统设计与实现(毕业论文)
- 多功能工具箱微信小程序源码.zip
- 狼人杀微信娱乐游戏小程序源码.zip
- 基于BP神经网络的回归预测:多个输出数据的分析与MATLAB代码实现,基于BP神经网络的多元输出数据回归预测技术研究与实践-以MATLAB代码为例,基于BP神经网络的多个输出数据的回归预测 matl
- 云开发谁是卧底线下小游戏发牌助手微信小程序源码.zip
- 最新版去水印小程序源码.zip
- 探索各类光波场的仿真与可视化:MATLAB的应用与实践,深入探索:各类光波场在MATLAB环境下的仿真研究与应用,各类光波场的MATLAB仿真 ,光波场仿真; MATLAB仿真; 仿真分析; 模拟;
- https://upload.csdn.net/creation/uploadResources?spm=1011.2124.3001.5646
- 应用商店-安卓-基于Android的应用商店设计与实现(毕业论文)
- 基于高斯过程回归算法的精准时间序列区间预测技术,高斯过程回归在时序区间预测中的应用与研究,基于高斯过程回归(GPR)的时间序列区间预测 ,基于高斯过程回归(GPR); 时间序列; 区间预测,基于高斯过
- 多相交错图腾柱PFC的C代码实现:优化算法与电流纹波控制技术,深度探究多相交错图腾柱PFC的C代码实现:优化算法以减小电流纹波,多相交错图腾柱PFC,C代码实现,电流纹波小 ,多相交错图腾柱PFC
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈


