Reputation: 2445
I have an image like this:
img
and I wanted to fill the bottom part using a mask like this as the location of seed points:
mask
This is what I tried but didn't work:
img = imread('img.jpg'); maskImg = imread('mask.png');
[Gmag, Gdir] = imgradient(img,'sobel');
mask = imbinarize(maskImg);
[rows,columns] = find(mask);
bw = imfill(Gmag, [rows(:) columns(:)]);
figure, imshow(bw);
The error message is:
Function IMFILL expected input number 2, CONN, to be a valid connectivity specifier. A nonscalar connectivity specifier must be 3-by-3-by- ... -by-3.
How can I fill the road part of the image above?
Upvotes: 0
Views: 380
Reputation: 4757
Not great and not highly reproducible for a variety of images but maybe something interesting can be pulled from this mess of rudimentary image processing techniques.
Image = imread("img.jpg");
Mask = imread("mask.png");
Contrast_Stretched_Image = imadjust(Image,stretchlim(Image),[]);
Max_Filtered_Image = ordfilt2(Contrast_Stretched_Image,1,ones(9,9));
Filtered_Image = medfilt2(Max_Filtered_Image, [15 15]);
Filtered_Image = Filtered_Image > 200;
[Boundaries,Boundary_Image] = bwboundaries(Filtered_Image, 'noholes');
Largest_Boundary = Boundaries(1);
Largest_Boundary = Largest_Boundary{1,1};
imshow(Boundary_Image)
[Image_Height,Image_Width] = size(Image);
Binary_Image = zeros(Image_Height,Image_Width);
for Boundary_Index = 1: length(Largest_Boundary)
X = Largest_Boundary(Boundary_Index,1);
Y = Largest_Boundary(Boundary_Index,2);
Binary_Image(X,Y) = 255;
end
Filled_Binary_Image = logical(imfill(Binary_Image,'holes'));
imshow(Filled_Binary_Image);
Image_Fill = uint8(~Filled_Binary_Image).*Image;
imshow(Image_Fill);
Mask_Fill = uint8(Filled_Binary_Image).*Mask;
imshow(Image_Fill+Mask_Fill);
The colour of the black unmasked portions can be changed prior to or afterwards. Complementary statements can be used to change the white and black regions.
Image = imread("img.jpg");
Mask = imread("Mask.png");
Mask = (Mask ~= 0);
Image(Mask) = 255;
imshow(Image);
Image = imread("img.jpg");
Mask = imread("Mask.png");
Mask = uint8(Mask > 0);
Masked_Image = Image.*Mask;
imshow(Masked_Image);
Image = imread("img.jpg");
Mask = imread("Mask.png");
Mask = uint8(Mask == 0);
Masked_Image_2 = Image.*Mask;
imshow(Masked_Image_2);
Image = imread("img.jpg");
Mask = imread("Mask.png");
Mask = (Mask == 0);
Image(Mask) = 255;
imshow(Image);
Using MATLAB version: R2019b
Upvotes: 1