How to improve the filtering of a bubble image

2 次查看(过去 30 天)
I need to better filter my image (a bubble) in order to obtain a better filtering than the one I already have (code), in order to get a better approximation of the contour of the bubble.
Would you know of any other filters or what I could change in my code to improve the borders I drew in red and to make the filtered image of the bubble (image 2) better look like the real bubble outline? (it should look like a semi-ellipse)
Thanks in advance
clc
clear
close all
% 1 - Reading the image
image = imread('bubble.png');
figure, imshow(image);
% 2 - Cropping the image
cropped = imcrop(image,[552.9 0 639.2 1080]);
figure, imshow(cropped);
% 3 - Gray scale
gray = rgb2gray(cropped);
figure, imshow(gray);
% 4 - Threshold filter
thresh = im2bw(gray, 0.5);
figure, imshow(thresh);
% 5 - Small pixel cluster removal filter
remove = bwareaopen(thresh, 13200);
figure, imshow(remove);
% 6 - Close connections filter
se = strel('line',300,0);
closing = imclose(remove,se);
figure, imshow(closing);
% 7 - Fill center of objects filter
OriginalLine = closing(1 , :);
OriginalLine2 = closing(end , :);
closing(1, :) = true;
closing(end, :) = true;
fill = imfill(closing, 'holes');
closing(end,:) = false;
closing(1,:) = false;
fill(1, :) = OriginalLine;
fill(end, :) = OriginalLine2;
figure, imshow(fill);
  2 个评论
Rik
Rik 2020-6-11
(Retrieved from Google cache)
Question title:
How to improve the filtering of a bubble image
Question body:
I need to better filter my image (a bubble) in order to obtain a better filtering than the one I already have (code), in order to get a better approximation of the contour of the bubble.
Would you know of any other filters or what I could change in my code to improve the borders I drew in red and to make the filtered image of the bubble (image 2) better look like the real bubble outline? (it should look like a semi-ellipse)
Thanks in advance
clc
clear
close all
% 1 - Reading the image
image = imread('bubble.png');
figure, imshow(image);
% 2 - Cropping the image
cropped = imcrop(image,[552.9 0 639.2 1080]);
figure, imshow(cropped);
% 3 - Gray scale
gray = rgb2gray(cropped);
figure, imshow(gray);
% 4 - Threshold filter
thresh = im2bw(gray, 0.5);
figure, imshow(thresh);
% 5 - Small pixel cluster removal filter
remove = bwareaopen(thresh, 13200);
figure, imshow(remove);
% 6 - Close connections filter
se = strel('line',300,0);
closing = imclose(remove,se);
figure, imshow(closing);
% 7 - Fill center of objects filter
OriginalLine = closing(1 , :);
OriginalLine2 = closing(end , :);
closing(1, :) = true;
closing(end, :) = true;
fill = imfill(closing, 'holes');
closing(end,:) = false;
closing(1,:) = false;
fill(1, :) = OriginalLine;
fill(end, :) = OriginalLine2;
figure, imshow(fill);

请先登录,再进行评论。

采纳的回答

Image Analyst
Image Analyst 2020-4-17
Can you assume the ellipse axes will be vertical and horizontal? If not, see the FAQ: Click here
Otherwise you can use bwferet() to get the caliper dimension in one direction. Call it 2*a. Use sum() or regionprops() to get the area and centroid,
binaryImage = bwareafilt(binaryImage, 1); % Take largest blob only.
props = regionprops(binaryImage, 'Area', 'Centroid');
area = props.Area;
xCenter = props.Centroid(1);
yCenter = props.Centroid(2);
hold on;
plot(xCenter, yCenter, 'r+', 'MarkerSize', 60, 'LineWidth', 2); % Plot crosshairs at the centroid.
then, since the area of an ellipse is pi*a*b,
b = area / (pi * a);
Create an ellipse from the code in the FAQ:
xCenter = 12.5;
yCenter = 10;
xRadius = 2.5;
yRadius = 8;
theta = 0 : 0.01 : 2*pi;
x = xRadius * cos(theta) + xCenter;
y = yRadius * sin(theta) + yCenter;
plot(x, y, 'LineWidth', 3);
grid on;
where xRadius = b and yRadius = a (if vertical is the feret diameter direction you picked).
Or you might try activecontour(). See attached demo.
  2 个评论
Image Analyst
Image Analyst 2020-4-18
You have an old version then. Try using regionprops() and getting the MajorAxisLength and MinorAxisLength, which are the axes of an ellipse fit to your blob.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Image Segmentation and Analysis 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by