How to fill an object with the same color as surrounding pixels?
3 views (last 30 days)
Show older comments
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 Comments
Accepted Answer
Jeff E
on 24 Sep 2015
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 Comment
Image Analyst
on 25 Sep 2015
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).
More Answers (0)
See Also
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!