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

3 次查看(过去 30 天)
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.

采纳的回答

yanqi liu
yanqi liu 2021-12-8
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, []);

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matched Filter and Ambiguity Function 的更多信息

产品


版本

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by