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

조회 수: 11 (최근 30일)
Alberto Acri
Alberto Acri 2023년 7월 12일
댓글: Alberto Acri 2023년 7월 12일
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'.

채택된 답변

Vishnu
Vishnu 2023년 7월 12일
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
Alberto Acri
Alberto Acri 2023년 7월 12일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Anavi Somani
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.

카테고리

Help CenterFile Exchange에서 Get Started with Image Processing Toolbox에 대해 자세히 알아보기

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by