elyraz
elyraz

Reputation: 473

Issue with imfill - Matlab

I try to fill the object in the BW2 but I am getting a completely white image. Why is this happening?

clc;
clear;
grayImage =  rgb2gray(imread('https://i.sstatic.net/afCDL.jpg'));

gaussian1 = fspecial('Gaussian', 15, 10);
gaussian2 = fspecial('Gaussian', 15, 200);
dog = gaussian1 - gaussian2;
dogFilterImage = conv2(double(grayImage), dog, 'same');

I=im2uint8(dogFilterImage); 
T = adaptthresh(I, 0.4);
BW = imbinarize(I,T);

imshowpair(I, BW, 'montage')

BW2 = bwareaopen(BW, 3000);
BW2 = imfill(BW2,'holes');
imshow(BW2)
title('Filled Image')

Upvotes: 1

Views: 195

Answers (1)

Suever
Suever

Reputation: 65430

The issue is that conv2 uses zero-padding on the image so when you perform the convolution you're going to have a "border" around your image since you've used the 'same' parameter.

enter image description here

This border is all 1's in your thresholded image causing imfill to completely fill in your image.

One option would be to use the 'valid' input to conv2 instead which will omit any pixels that are affected by the image being zero-padded prior to convolution

dogFilterImage = conv2(double(grayImage), dog, 'valid');

The other option is to explicitly pad your image with values other than zeros. You can use padarray to do this.

padded = padarray(double(grayImage), [8 8], 'replicate', 'both');
dogFilterImage = conv2(double(grayImage), dog, 'valid');

Using either of these methods your output will look like what I assume you expect.

enter image description here

Upvotes: 1

Related Questions