2014-09-19 43 views
0

我在栅栏后面有一些鹿的图像,我试图去除栅栏,以便我们可以直接看到没有栅栏的鹿。到目前为止,我已经对一个体面的质量进行了细分(确定了栅栏的坐标),并试图用附近的颜色替换这些坐标的颜色来模拟移除栅栏。MATLAB:过滤出图像的一部分

MATLAB中有一些图像处理函数可以帮助我轻松完成目标吗?我尝试了下面的代码,我试图找到最近的坐标不是围栏的一部分,但“〜ismember([i_temp,j],[ii,jj])”不起作用,因为我是试图比较坐标,但相反,它似乎将i_temp和j作为单独变量进行比较,即i_temp不在ii或jj中,并且在ii或jj中是j。

%% 5. Locate pixel locations of the segmented image 
% Now that the fence has been segmented. Locate the pixel locations of the 
% segmented image 

[ii,jj] = find(Img_threshold2==1); 

n = length(ii); % Get a count of the number of coordinates 

Rout = Rin; 
Gout = Gin; 
Bout = Bin; 

%% 6. Recolor the areas of the fence with the nearest color available 
for k=1:n 
    i = ii(k); 
    j = jj(k); 

    keepLooping = true; 
    i_add = 0; 
    j_add = 0; 
    i_coord = 0; 
    j_coord = 0; 

    % Find the nearest coordinate that is not a part of the fence 
    while keepLooping 
     i_add = i_add + 1; 
     j_add = j_add + 1; 

     % Check right 
     i_temp = i + i_add; 
     if ~ismember([i_temp,j],[ii,jj]) 
      i_coord = i_temp; 
      j_coord = j; 
      break 
     end 

     % Check left 
     i_temp = i - i_add; 
     if ~ismember([i_temp,j],[ii,jj]) 
      i_coord = i_temp; 
      j_coord = j; 
      break 
     end 

     % I would do check up and down, but the left/right doesn't even work 
     % since ismember doesn't work as I expected 
    end 

    % Replace the color of the fence coordinate to the nearest non-fence 
    % coordinate determined above 
    Rout(i,j) = Rin(i_coord,j_coord); 
    Gout(i,j) = Rin(i_coord,j_coord); 
    Bout(i,j) = Rin(i_coord,j_coord); 
end 

EDIT 14年9月20日: 试过bwdist与下面的代码,其中IIN是输入图像和image_threshold2是分段的围栏。篱笆不是变成最接近的颜色,而是变成绿松石,这是没有意义的,因为图像中没有绿松石。这是一个展示发生了什么的截图。我裁剪了图像,以便在图像的一小部分区域进行测试,实际的图像要大得多,并且有实际的鹿和什么。截图:http://gyazo.com/32ab37b8d2d9e137103d330a39d4ecfa

[D,IDX] = bwdist(~Img_threshold2); 

% Replace the color of the fence coordinate to the nearest non-fence 
% coordinate determined above 
Iout = Iin; 
Iout(Img_threshold2)=Iout(IDX(Img_threshold2)); 
+0

你能提供一个你想要处理的图像的链接吗? – rayryeng 2014-09-19 15:21:40

+0

@Julio Revka,如果提供的答案解决了您的问题,请在左侧接受它。 – ASantosRibeiro 2014-09-20 18:37:20

回答

0

使一个二进制图像,其中图1是围栏和0的保留。然后使用Matlab的[D,IDX] = bwdist(BW)函数。在IDX中,您有最接近的非围栏索引。然后,您只需将栅栏中的体素分配给最接近的非栅栏值。

[D,IDX] = bwdist(~mask_fence) 
I(mask_fence)=I(IDX(mask_fence)) 
+0

我试过这个,而不是得到最接近的篱笆颜色,篱笆变成了绿松石,这是一种在图像中无处存在的颜色。请参阅我在原始帖子中所做的修改。 – 2014-09-20 19:28:41

+0

如果您使用3个通道(RGB),则通过3个独立通道执行此操作。基本上Ired(mask_fence)= Ired(IDX(mask_fence)),Igreen(mask_fence)= Igreen(IDX(mask_fence)),Iblue(mask_fence)= Iblue(IDX(mask_fence)) – ASantosRibeiro 2014-09-20 19:40:22

+0

是的,我刚刚想到并实现它。然后回头看看这里,看到我的想法符合你的建议:)。似乎工作表示感谢。 – 2014-09-20 19:46:54