Apply the color white (255) to some black pixels (0)
이전 댓글 표시
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
Dyuman Joshi
2023년 7월 12일
Note - Requires Image Processing Toolbox
채택된 답변
추가 답변 (1개)
Anavi Somani
2023년 7월 12일
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.
카테고리
도움말 센터 및 File Exchange에서 Images에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!