图像处理-运动目标检测

1.背景简介

之前听李永乐老师讲课youtube,个人觉得讲得非常好!想截图保存讲课内容,奈何李老师没给任何机会,于是有了这个小demo!图像处理-运动目标检测_第1张图片

2.Matlab代码

clc
clear
close all;
%%
%读图rgb2gray
img_s1 = imread('11.png');
img_s2 = imread('22.png');
if ndims(img_s1)==3
    img1 = rgb2gray(img_s1);
else
    img1 = img_s1;
end
if ndims(img_s2)==3
    img2 = rgb2gray(img_s2);
else
    img2 = img_s2;
end

%二值化提ROI-经验值
diff  = abs(double(img1) - double(img2));
figure, imshow(diff),imcontrast,impixelinfo;
BW = imbinarize(diff,30);
%形态学处理
se1=strel('disk',3);
se2=strel('disk',15);
bw_e = imerode(BW,se1);
bw_d = imdilate(bw_e,se2);
bw_d = imdilate(bw_d,se2);
figure, imshow(bw_d);
[L,num]= bwlabel(bw_d);% 对连通区域进行标记
%初始化新图像
dst = img_s1;
for idx = 1:num
    g1 = rgba_img(img_s1,L==idx);
    g2 = rgba_img(img_s2,L==idx);
    if g1<g2&&abs(g1-g2)>0.2
        disp(g1);
        disp(g2);
        mask = repmat(L==idx, [1,1,3]);
        dst(mask) = img_s2(mask);
    end
end
figure, imshow(dst);
imwrite(dst,'dst.png');
function gv = rgba_img(img,mask)
%统计mask区域内RGB颜色分量
rc = img(:,:,1);
gc = img(:,:,2);
bc = img(:,:,3);
green_bigger = (gc>rc).*(gc>bc);
gv = sum(double(green_bigger(mask)))/sum(sum(mask));
end

3.结果

图像处理-运动目标检测_第2张图片

你可能感兴趣的:(图像处理)