How to fill an object with the same color as surrounding pixels?
3 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
采纳的回答
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
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 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Image Processing Toolbox 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!