Clear Filters
Clear Filters

Apply the color white (255) to some black pixels (0)

5 views (last 30 days)
I need to apply the color white (255) to some black pixels.
For example, in my case I have the geometry of 'circles' (but I can also have other similar geometries, such as ellipses).
Is there any way to easily fill the circle(s) (or similar geometries) with white color?
The starting images are 'test', 'test2', 'test3', 'test4'; the images I want to get are 'test_out', 'test2_out', 'test3_out', 'test4_out'.

Accepted Answer

Vishnu
Vishnu on 12 Jul 2023
Hi Alberto,
You can easily fill the circle with white color using MATLAB. Here's the code to guide on how to do it:
This assumes that the circles are well-defined and have distinct boundaries. If the circles are partially obscured or have irregular boundaries, additional image processing techniques may be required to accurately fill them.
image = imread('test.png'); % read the image file into MATLAB
grayImage = rgb2gray(image);% convert it to grayscale using the rgb2gray function.
% Use the imbinarize function to convert the grayscale image to a binary image
% using a suitable threshold value.
% Adjust the threshold value until the circles are well-segmented.
thresholdValue = graythresh(grayImage);
binaryImage = imbinarize(grayImage, thresholdValue);
% Use the imfill function to fill the circles in the binary image with white color.
filledImage = imfill(binaryImage, 'holes');
outputFilename = 'test_out.png'; % Specify the desired filename
imwrite(filledImage, outputFilename);
% Use the imshow function to display the filled image.
imshow(filledImage);
  1 Comment
Alberto Acri
Alberto Acri on 12 Jul 2023
Hi, thank you for your response. I have edited the post. I don't always have circles as input image. They can also be partially overlapping. At the moment your code works for me. I will update you in case I have problems with your code.

Sign in to comment.

More Answers (1)

Anavi Somani
Anavi Somani on 12 Jul 2023
Hi Alberto,
You can try running these commands in MATLAB to fill in a circle imported from a file.
% Read the input images
test = imread('test.png');
test2 = imread('test2.png');
% Convert the images to grayscale
test_gray = rgb2gray(test);
test2_gray = rgb2gray(test2);
% Apply edge detection to find the boundaries of the circle(s)
test_edges = edge(test_gray, 'Canny');
test2_edges = edge(test2_gray, 'Canny');
% Perform a morphological closing operation to close any gaps in the edges
se = strel('disk', 5);
test_closed = imclose(test_edges, se);
test2_closed = imclose(test2_edges, se);
% Fill the circle(s) with white color
test_out = test;
test_out(test_closed) = 255;
test2_out = test2;
test2_out(test2_closed) = 255;
% Display the output images
figure;
subplot(2, 2, 1), imshow(test), title('Input Image (test)');
subplot(2, 2, 2), imshow(test_out), title('Output Image (test_out)');
subplot(2, 2, 3), imshow(test2), title('Input Image (test2)');
subplot(2, 2, 4), imshow(test2_out), title('Output Image (test2_out)');
Make sure to adjust the file paths or image names according to your specific setup.

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!