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
댓글 수: 1
답변 (2개)
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);
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.
댓글 수: 0
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
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.
Have a look here: https://de.mathworks.com/discovery/image-segmentation.html
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 Center 및 File Exchange에서 Image Processing Toolbox에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!