Removing Noise in Image
Show older comments
I'm trying to detect a semicircular region from an image to create a mask. Tried edge detection followed by filtering. Original image on the left and processed image on the right. How can I fill the dark region on the right and remove the extra features on left and right leaving me with just the semicircular region.
Answers (2)
VINAYAK LUHA
on 1 Oct 2023
Edited: VINAYAK LUHA
on 1 Oct 2023
Hi Kartikeya,
I understand that you want to postprocess the obtained mask to remove noise and accurately detect the semicircular object in your image.
Follow the below steps to achieve that -
- Mirror your mask about the horizontal axis.
- Stitch the original and mirrored image horizontally to obtain a complete circle.
- Preprocess the image obtained from step 2.
- Use the "Image Segmenter" app and detect and save the mask for the complete circle.
- Horizontally crop the mask obtained from top till mid.
After step 3 -
After step 4 -

After step 5 -

Here is the code snippet for your reference.
image = imread('pathToMask');
%1 Mirror the mask about the horizontal axis.
flipped_image = flipud(image);
%2 Stitch the original and mirrored image horizontally to obtain a complete circle.
joined_image = [image;flipped_image];
%3 Preprocess the image obtained from step 2
gray_image = rgb2gray(joined_image);
blurred_image = imgaussfilt(gray_image, 5);
%4 Use the "Image Segmenter" app and detect and save the mask for the complete circle.
[fullmask,~] = segmentImage(blurred_image);
%Horizontally crop the mask obtained from top till mid.
halfmask = fullmask(1:int32(size(image,2)/2),:, :);
figure;
imshow(halfmask)
hold off;
%Function exported from Image Segmenter app to detect circle and obtain
%mask
function [BW,maskedImage] = segmentImage(X)
%segmentImage Segment image using auto-generated code from Image Segmenter app
% [BW,MASKEDIMAGE] = segmentImage(X) segments image X using auto-generated
% code from the Image Segmenter app. The final segmentation is returned in
% BW, and a masked image is returned in MASKEDIMAGE.
% Auto-generated by imageSegmenter app on 01-Oct-2023
%----------------------------------------------------
% Adjust data to span data range.
X = imadjust(X);
% Find circles
[centers,radii,~] = imfindcircles(X,[400 600],'ObjectPolarity','bright','Sensitivity',0.90);
BW = false(size(X,1),size(X,2));
[Xgrid,Ygrid] = meshgrid(1:size(BW,2),1:size(BW,1));
BW = BW | (hypot(Xgrid-centers(1,1),Ygrid-centers(1,2)) <= radii(1));
% Create masked image.
maskedImage = X;
maskedImage(~BW) = 0;
end
Explore these links to the documentation of "flipud" and "imgaussfilt" for more details -
- https://in.mathworks.com/help/matlab/ref/flipud.html
- https://in.mathworks.com/help/images/ref/imgaussfilt.html
Hope this helps !
Regards,
Vinayak Luha
Image Analyst
on 1 Oct 2023
0 votes
What do we know about the circular/elliptical shape? Is it always partially outside the image? Do the streaks always emanate from the right and left side and never from the middle?
For this particular image, I'd probably find the top edge of your segmented image and fit those (x,y) to a circle. Then create a new mask based on the circular fit.
Categories
Find more on Region and Image Properties in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!