声振论坛

 找回密码
 我要加入

QQ登录

只需一步,快速开始

123
返回列表 发新帖
楼主: 程序255

[编程技巧] 中值滤波的问题

[复制链接]
发表于 2007-4-25 20:17 | 显示全部楼层
回复 支持 反对
分享到:

使用道具 举报

发表于 2007-4-25 20:18 | 显示全部楼层
原帖由 程序255 于 2007-4-25 20:05 发表
Q(1:8)=(p2,p3,p4,p5,p6,p7,p8,p9);
Q为一个一唯矩阵 后面是它的元素的值  


Q(1:8)=[p2,p3,p4,p5,p6,p7,p8,p9];
 楼主| 发表于 2007-4-25 23:47 | 显示全部楼层

eight 看看我的这个表示对吗

比较R 和P 的元素是否完全一样     
和 把P用R代替


while R~=P
   P=R;
发表于 2007-4-26 10:02 | 显示全部楼层
原帖由 程序255 于 2007-4-25 23:47 发表
比较R 和P 的元素是否完全一样     
和 把P用R代替


while R~=P
   P=R;



help isequal

代替问题要两者维数相同才行
 楼主| 发表于 2007-4-26 10:42 | 显示全部楼层

eight 急死了

[m,n]=size(P);
W=zeros(m,n);

for i=2:m-1
    for j=2:n-1
count2=0;count3=0;count4=0;count5=0;count6=0;count7=0;count8=0;count9=0;
p1=P(i,j);
p2=P(i-1,j);
p3=P(i-1,j+1);
p4=P(i,j+1);
p5=P(i+1,j+1);
p6=P(i+1,j);
p7=P(i+1,j-1);
p8=P(i,j-1);
p9=P(i-1,j-1);
      if p1==255
         
      
      if p2==255
          count2=1;
      else count2=0;
      end;
      if p3==255
          count3=1;
      else count3=0;
      end;
      if p4==255
          count4=1;
      else count4=0;
      end;
      if p5==255
          count5=1;
      else count5=0;
      end;
      if p6==255
          count6=1;
      else count6=0;
      end;
      if p7==255
          count7=1;
      else count7=0;
      end;
      if p8==255
          count8=1;
      else count8=0;
      end;
      if p9==255
          count9=1;
      else count9=0;
      end;
      else W(i,j)=P(i,j);
   end;  
count=count2+count3+count4+count5+count6+count7+count8+count9;
if count<2|count>6
    W(i,j)=P(i,j);
else
   Q(1:8)=[p2,p3,p4,p5,p6,p7,p8,p9];
   a=0;
   for k=1:7
       if Q(k)<Q(k+1)
           a=a+1;
       end;
   end;
   if a~=1
        W(i,j)=P(i,j);
   else
       if p2*p4*p6~=0|p4*p6*p8~=0
           
     W(i,j)=P(i,j);
       else W(i,j)=0;
       end;
   end;
end;
    end;
end;
[m,n]=size(W);
   R=zeros(m,n);
   
for i=2:m-1
    for j=2:n-1
p1=W(i,j);
p2=W(i-1,j);
p3=W(i-1,j+1);
p4=W(i,j+1);
p5=W(i+1,j+1);
p6=W(i+1,j);
p7=W(i+1,j-1);
p8=W(i,j-1);
p9=W(i-1,j-1);
if p1==255
         
      
      if p2==255
          count2=1;
      else count2=0;
      end;
      if p3==255
          count3=1;
      else count3=0;
      end;
      if p4==255
          count4=1;
      else count4=0;
      end;
      if p5==255
          count5=1;
      else count5=0;
      end;
      if p6==255
          count6=1;
      else count6=0;
      end;
      if p7==255
          count7=1;
      else count7=0;
      end;
      if p8==255
          count8=1;
      else count8=0;
      end;
      if p9==255
          count9=1;
      else count9=0;
      end;
else R(i,j)=W(i,j);
end;

count=count2+count3+count4+count5+count6+count7+count8+count9;
if count<2|count>6
    R(i,j)=W(i,j);
else
   Q(1:8)=[p2,p3,p4,p5,p6,p7,p8,p9];
   a=0;
   for k=1:7
       if Q(k)<Q(k+1)
           a=a+1;
       end;
   end;
   if a~=1
        R(i,j)=W(i,j);
   else
       if p2*p4*p8~=0|p2*p6*p8~=0
           
     R(i,j)=W(i,j);
       else R(i,j)=0;
       end;
   end;
end;
    end;
end;
while R~=P
   P=R;
    [m,n]=size(P);


for i=2:m-1
    for j=2:n-1
count2=0;count3=0;count4=0;count5=0;count6=0;count7=0;count8=0;count9=0;
p1=P(i,j);
p2=P(i-1,j);
p3=P(i-1,j+1);
p4=P(i,j+1);
p5=P(i+1,j+1);
p6=P(i+1,j);
p7=P(i+1,j-1);
p8=P(i,j-1);
p9=P(i-1,j-1);
      if p1==255
         
      
      if p2==255
          count2=1;
      else count2=0;
      end;
      if p3==255
          count3=1;
      else count3=0;
      end;
      if p4==255
          count4=1;
      else count4=0;
      end;
      if p5==255
          count5=1;
      else count5=0;
      end;
      if p6==255
          count6=1;
      else count6=0;
      end;
      if p7==255
          count7=1;
      else count7=0;
      end;
      if p8==255
          count8=1;
      else count8=0;
      end;
      if p9==255
          count9=1;
      else count9=0;
      end;
      else W(i,j)=P(i,j);
   end;  
count=count2+count3+count4+count5+count6+count7+count8+count9;
if count<2|count>6
    W(i,j)=P(i,j);
else
   Q(1:8)=[p2,p3,p4,p5,p6,p7,p8,p9];
   a=0;
   for k=1:7
       if Q(k)<Q(k+1)
           a=a+1;
       end;
   end;
   if a~=1
        W(i,j)=P(i,j);
   else
       if p2*p4*p6~=0|p4*p6*p8~=0
           
     W(i,j)=P(i,j);
       else W(i,j)=0;
       end;
   end;
end;
    end;
end;
[m,n]=size(W);
  
   
for i=2:m-1
    for j=2:n-1
p1=W(i,j);
p2=W(i-1,j);
p3=W(i-1,j+1);
p4=W(i,j+1);
p5=W(i+1,j+1);
p6=W(i+1,j);
p7=W(i+1,j-1);
p8=W(i,j-1);
p9=W(i-1,j-1);
if p1==255
         
      
      if p2==255
          count2=1;
      else count2=0;
      end;
      if p3==255
          count3=1;
      else count3=0;
      end;
      if p4==255
          count4=1;
      else count4=0;
      end;
      if p5==255
          count5=1;
      else count5=0;
      end;
      if p6==255
          count6=1;
      else count6=0;
      end;
      if p7==255
          count7=1;
      else count7=0;
      end;
      if p8==255
          count8=1;
      else count8=0;
      end;
      if p9==255
          count9=1;
      else count9=0;
      end;
else R(i,j)=W(i,j);
end;

count=count2+count3+count4+count5+count6+count7+count8+count9;
if count<2|count>6
    R(i,j)=W(i,j);
else
   Q(1:8)=[p2,p3,p4,p5,p6,p7,p8,p9];
   a=0;
   for k=1:7
       if Q(k)<Q(k+1)
           a=a+1;
       end;
   end;
   if a~=1
        R(i,j)=W(i,j);
   else
       if p2*p4*p8~=0|p2*p6*p8~=0
           
     R(i,j)=W(i,j);
       else R(i,j)=0;
       end;
   end;
end;
    end;
end;
end;
figure,imshow(R);







我的那个循环为什么不执行啊 我估计问题出在 条件的表示上 可是弄不好  上面程序 是对P进行边缘细化 ,需要重复那个循环 直到不再有变化为止  关键给我看看怎么让那个循环执行  :'( 谢了
 楼主| 发表于 2007-4-26 10:50 | 显示全部楼层

我手工又细化了一次是这个效果 可那循环的就没再处理

{3DDCE581-B829-4D91-A2F1-4E12375C700A}0.jpg
{50211318-DA0D-4537-977F-466C5661017A}0.jpg
发表于 2007-4-26 10:53 | 显示全部楼层
原帖由 程序255 于 2007-4-26 10:42 发表
[m,n]=size(P);
W=zeros(m,n);

for i=2:m-1
    for j=2:n-1
count2=0;count3=0;count4=0;count5=0;count6=0;count7=0;count8=0;count9=0;
p1=P(i,j);
p2=P(i-1,j);
p3=P(i-1,j+1);
p4=P(i,j+1);
p5 ...


你至少得说明是哪个循环吧,我可不是天天有空坐在这里等你们来提问的
 楼主| 发表于 2007-4-26 11:09 | 显示全部楼层

这个循环

while R~=P
   P=R;
发表于 2007-4-26 11:17 | 显示全部楼层
原帖由 程序255 于 2007-4-26 11:09 发表
while R~=P
   P=R;



自己设置断点调试一下吧,你的程序太复杂,我没有那么多时间细看

你试试把 R~=P 改为 any(R~=P)

[ 本帖最后由 eight 于 2007-4-26 11:20 编辑 ]
 楼主| 发表于 2007-4-26 11:38 | 显示全部楼层

好的 

 楼主| 发表于 2007-4-26 13:11 | 显示全部楼层

大虾 看看我的循环为什么只执行一次

K=1;
while K>0
   
    [m,n]=size(R);
  for i=1:m
    for j=1:n
        P(i,j)=R(i,j);
    end;
end;
   


for i=2:m-1
    for j=2:n-1
count2=0;count3=0;count4=0;count5=0;count6=0;count7=0;count8=0;count9=0;
p1=P(i,j);
p2=P(i-1,j);
p3=P(i-1,j+1);
p4=P(i,j+1);
p5=P(i+1,j+1);
p6=P(i+1,j);
p7=P(i+1,j-1);
p8=P(i,j-1);
p9=P(i-1,j-1);
      if p1==255
         
      
      if p2==255
          count2=1;
      else count2=0;
      end;
      if p3==255
          count3=1;
      else count3=0;
      end;
      if p4==255
          count4=1;
      else count4=0;
      end;
      if p5==255
          count5=1;
      else count5=0;
      end;
      if p6==255
          count6=1;
      else count6=0;
      end;
      if p7==255
          count7=1;
      else count7=0;
      end;
      if p8==255
          count8=1;
      else count8=0;
      end;
      if p9==255
          count9=1;
      else count9=0;
      end;
      else W(i,j)=P(i,j);
   end;  
count=count2+count3+count4+count5+count6+count7+count8+count9;
if count<2|count>6
    W(i,j)=P(i,j);
else
   Q(1:8)=[p2,p3,p4,p5,p6,p7,p8,p9];
   a=0;
   for k=1:7
       if Q(k)<Q(k+1)
           a=a+1;
       end;
   end;
   if a~=1
        W(i,j)=P(i,j);
   else
       if p2*p4*p6~=0|p4*p6*p8~=0
           
     W(i,j)=P(i,j);
       else W(i,j)=0;
       end;
   end;
end;
    end;
end;
[m,n]=size(W);
  
   
for i=2:m-1
    for j=2:n-1
p1=W(i,j);
p2=W(i-1,j);
p3=W(i-1,j+1);
p4=W(i,j+1);
p5=W(i+1,j+1);
p6=W(i+1,j);
p7=W(i+1,j-1);
p8=W(i,j-1);
p9=W(i-1,j-1);
if p1==255
         
      
      if p2==255
          count2=1;
      else count2=0;
      end;
      if p3==255
          count3=1;
      else count3=0;
      end;
      if p4==255
          count4=1;
      else count4=0;
      end;
      if p5==255
          count5=1;
      else count5=0;
      end;
      if p6==255
          count6=1;
      else count6=0;
      end;
      if p7==255
          count7=1;
      else count7=0;
      end;
      if p8==255
          count8=1;
      else count8=0;
      end;
      if p9==255
          count9=1;
      else count9=0;
      end;
else R(i,j)=W(i,j);
end;

count=count2+count3+count4+count5+count6+count7+count8+count9;
if count<2|count>6
    R(i,j)=W(i,j);
else
   Q(1:8)=[p2,p3,p4,p5,p6,p7,p8,p9];
   a=0;
   for k=1:7
       if Q(k)<Q(k+1)
           a=a+1;
       end;
   end;
   if a~=1
        R(i,j)=W(i,j);
   else
       if p2*p4*p8~=0|p2*p6*p8~=0
           
     R(i,j)=W(i,j);
       else R(i,j)=0;
       end;
   end;
end;
    end;
end;
for i=1:m
    for j=1:n
        K=0;
        if R(i,j)~=P(i,j)
            K=K+1;
        else K=0;
        end;
    end;
end;
            
end;
figure,imshow(R);
您需要登录后才可以回帖 登录 | 我要加入

本版积分规则

QQ|小黑屋|Archiver|手机版|联系我们|声振论坛

GMT+8, 2024-5-20 06:21 , Processed in 0.086545 second(s), 19 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表