How to fill an object with the same color as surrounding pixels?

조회 수: 3 (최근 30일)
Ivan Shorokhov
Ivan Shorokhov 2015년 9월 24일
댓글: Image Analyst 2015년 9월 25일
Hello,
I have following image.
And I would like to fill the white whole, with the same color as the gray circle outside, so that would like as the following image:
Here what I have done just know:
close all; clear all;
Im1=imread('Im1.bmp');
Im2 = rgb2gray(Im1);
figure; imshow(Im1);
bw=im2bw(Im1,0.9);
bw_dilated = imdilate(bw,strel('disk',3));
%[I,J,V] = find(Im2, B{1,1}(1:end,1));
avgPrecisionSize = 16; % smaller is better, but takes longer
G = fspecial('gaussian',[1 1]*100,50);
H = fspecial('average', [1,1]*avgPrecisionSize);
%# User a big filter to get started:
newImage = imfilter(Im2,G,'same');
newImage(~bw_dilated) = Im1(~bw_dilated);
numIterations = 30;
for count = 1:numIterations
newImage = imfilter(newImage, H, 'same');
newImage(~bw_dilated) = Im1(~bw_dilated);
end
%%Plot the results
figure(123);
clf;
% Display the mask:
subplot(1,2,1);
imagesc(Im1);
axis image
title('Region Of the Bad Pixels');
% Display the result:
subplot(1,2,2);
imagesc(newImage);
axis image
set(gca,'clim', [0 255])
title('Infilled Image');
colormap gray
And here is what I got:
[MATLAB] version 2015b

채택된 답변

Jeff E
Jeff E 2015년 9월 24일
If you're always dealing with pixels regions that have the same values, then the below should work:
Im1=imread('Im1.bmp');
Im2 = rgb2gray(Im1);
gray_value = 195 ; %empirically derived value of gray pixels in ring
white_mask = Im2 > 250 ; %empirically derived threshold to make binary mask of white pixels
Im2(white_mask) = gray_value ; %assign all pixels in white_mask the intensity (gray_value)
imshow(Im2);
If the values of the pixels changes, this problem becomes more complex.
  댓글 수: 1
Image Analyst
Image Analyst 2015년 9월 25일
And if the outer region is not a uniform intensity, then you can use regionfill(), which basically smears the surrounding pixels in towards the middle of the region (a weighted interpolation).

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

Community Treasure Hunt

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

Start Hunting!

Translated by