我在篱笆后面有一些鹿的图像,我正在试图移除篱笆,这样我们就可以直视鹿,没有挡板。到目前为止,我已经,以一个不错的质量,分割出(确定了栅栏的坐标),并试图用附近的颜色替换这些坐标的颜色,以模拟移除栅栏。
MATLAB中是否有一些图像处理功能可以帮助我轻松实现目标?我试过下面的代码,我试图找到最近的坐标不是围栏的一部分,但是“~istmember([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
编辑9/20/14: 尝试使用以下代码进行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 :(得分:0)
制作二进制图像,其中1是栅栏,0是剩余。然后使用Matlab的[D,IDX] = bwdist(BW)函数。在IDX中,您拥有最接近的非栅栏索引。然后你只需要在栅栏中的体素中分配最接近的非栅栏值。
[D,IDX] = bwdist(~mask_fence)
I(mask_fence)=I(IDX(mask_fence))