How to add filter just foreground and another filter to background?

2 views (last 30 days)
Hello, i am trying to add filter to foreground and another filter to background:
I had an gray scale image which background is blur and foreground has an object. And i separated my image with iterative optimal threshold algorithm. Here is my code:
I=imread('a.png');
A=stdfilt(I);
T=1;
%% G1 is foreground, G2 is background %%
for i=1:16
G1=T>A;
G2=T<=A;
m1=mean(A(G1));
m2=mean(A(G2));
Tnew=(m1+m2)/2;
T=Tnew;
end
Here i am trying to add L filter to just foreground but it doesn't work.
L=[-1 -1 -1;-1 8 -1;-1 -1 -1];
P=imfilter(G1,L);
R=I+P;
And here i am trying to add Gfilter to just background but it also doesn't work.
G=1/16*[1 2 1;2 4 2;1 2 1];
J=imfilter(I(G2),G);
Result=J+R;
I know i am wrong somewhere but i also don't know how to fix it.

Accepted Answer

yanqi liu
yanqi liu on 8 Dec 2021
clc; clear all; close all;
I=imread('rice.png');
A=stdfilt(I);
T=1;
% G1 is foreground, G2 is background %%
for i=1:16
G1=T>A;
G2=T<=A;
m1=mean(A(G1));
m2=mean(A(G2));
Tnew=(m1+m2)/2;
T=Tnew;
end
figure; imshow(G1, []);
figure; imshow(G2, []);
G1 = I.*uint8(G1);
G2 = I.*uint8(G2);
figure; imshow(G1, []);
figure; imshow(G2, []);
% add L filter to just foreground
L=[-1 -1 -1;-1 8 -1;-1 -1 -1];
P=imfilter(G1,L);
R=I+P;
% add Gfilter to just background
G=1/16*[1 2 1;2 4 2;1 2 1];
J=imfilter(G2,G);
Result=J+R;
% imshow
figure; imshow(R, []);
figure; imshow(Result, []);

More Answers (0)

Categories

Find more on Images in Help Center and File Exchange

Products


Release

R2021a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!