How can I fill boundary by white pixel?

6 次查看(过去 30 天)
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

回答(2 个)

Image Analyst
Image Analyst 2019-4-20
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;
% Load in image.
rgbImage = imread('image_0001.jpg');
% 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]);
% Load Coordinates
s = load('annotation_0001.mat')
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.
mask = poly2mask(x, y, rows, columns);
subplot(2, 2, 4);
imshow(mask);
axis('on', 'image');
title('Boundary made into a Mask', 'FontSize', fontSize);
0000 Screenshot.png
Note that your image has white padding on the right and left while your coordinates do not seem to take that into account so that is why the boundary does not overlap perfectly with your image.

Andreas Bernatzky
Andreas Bernatzky 2019-4-18
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;
  2 个评论
bamini thavarajah
bamini thavarajah 2019-4-19
boundary not like squre or any geomatrical shape. It can be any object outline for example a bird.
Andreas Bernatzky
Andreas Bernatzky 2019-4-20
编辑:Andreas Bernatzky 2019-4-20
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Image Processing Toolbox 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by