2014-09-19 99 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