Matlab影像處理(二)背景相減
%%影像相減
close all; clear; clc;
img = double(imread('img.jpg'));
bgimg = double(imread('backGround.jpg'));
[row, col] = size(img(:, :, 1));
outputimg = zeros(row, col);
binaryimg = zeros(row, col);
T=10;
for i=1:row
for j=1:col
outputimg(i, j) = (( bgimg(i, j, 1)-img(i, j, 1) ).^2 + ( bgimg(i, j, 2)-img(i, j, 2) ).^2 + ( bgimg(i, j, 2)-img(i, j, 2) ).^2).^0.5;
if outputimg(i, j) > T
binaryimg(i, j) = 255;
end
end
end
subplot(2, 2, 1), imshow(uint8(img)), title('原圖')
subplot(2, 2, 2), imshow(uint8(bgimg)), title('BG圖')
subplot(2, 2, 3), imshow(uint8(outputimg)), title('相減後')
subplot(2, 2, 4), imshow(uint8(binaryimg)), title('二值化')
留言
張貼留言