## How can I fill boundary by white pixel?

### bamini thavarajah (view profile)

on 18 Apr 2019
Latest activity Answered by Image Analyst

### Image Analyst (view profile)

on 20 Apr 2019
I have boundary pixel values in mat file and image in jpg format (colour image).
I need to fill within boundary by white pixel, others by black pixel.
final output will be binary image.
I need matlab code for it.
I have attached image and boundary pixel in mat file

### Andreas Bernatzky (view profile)

on 18 Apr 2019

Hey Bamini,
I am not quiet sure if I have correctly understood your questions. But you have a given image with for example 100x100 pixels. you want to fill up the four border (left,right,upper,lower) of the image with white pixels - right?
I would suggest this:
%% Filling Up Borders with white Pixels
YourImage=rand(100,100);%matrix 100x100 representing gray values
boundaryLeft=1;
boundaryUpper=1;
boundaryRight=1;
boundaryLower=1;
imgSize=size(YourImage);
% 0 = white pixel
YourImage(:,1:boundaryLeft)=0;%left Border
YourImage(1:boundaryUpper,:)=0;%upper Border
YourImage(:,(imgSize(2)-boundaryRight+1):end)=0;%right Border
YourImage((imgSize(1)-boundaryLower+1):end,:)=0;%lower Border;

bamini thavarajah

### bamini thavarajah (view profile)

on 19 Apr 2019
boundary not like squre or any geomatrical shape. It can be any object outline for example a bird.
Andreas Bernatzky

### Andreas Bernatzky (view profile)

on 20 Apr 2019
Hey,
this sounds like a segmentation problem to me. I just learned the theory about Image Processing but never did something with it in MATLAB or have really practical knowledge. So I can just give you some quick advices.
I can imagine to write an "easy-rough" algorithm like the one you would need. But I am pretty sure there is some in-built MATLAB function for this.

### Image Analyst (view profile)

on 20 Apr 2019

Use poly2mask(), as long as your x and y are in order. It will turn your (x,y) vertex coordinate list into a filled mask (a logical image). Try this code:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 20;
% Get the dimensions of the image.
[rows, columns, numberOfColorChannels] = size(rgbImage)
subplot(2, 2, 1);
imshow(rgbImage);
axis('on', 'image');
title('Original Image', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0, 0.04, 1, 0.96]);
x = s.obj_contour(1, :);
y = s.obj_contour(2, :);
subplot(2, 2, 2);
plot(x, y, 'b*-', 'MarkerSize', 10, 'LineWidth', 2)
grid on;
title('Boundary Points', 'FontSize', fontSize);
xlabel('x', 'FontSize', fontSize);
ylabel('y', 'FontSize', fontSize);
% Show them over the image.
subplot(2, 2, 3);
imshow(rgbImage);
axis('on', 'image');
title('Original Image with Boundary', 'FontSize', fontSize);
hold on;
plot(x, y, 'b*-', 'MarkerSize', 9, 'LineWidth', 2)
% Create a mask from the coordinates.