Apply the color white (255) to some black pixels (0)
Show older comments
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'.
1 Comment
Dyuman Joshi
on 12 Jul 2023
Note - Requires Image Processing Toolbox
Accepted Answer
More Answers (1)
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.
Categories
Find more on Images in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!