Reputation: 473
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
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.
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.
Upvotes: 1