# How to define the surface line of a wave? Hought Transform on not straight lines and interpolation for mising parts.

2 views (last 30 days)
Hannah on 3 Jun 2024
Answered: Kothuri on 21 Jun 2024
Hi Matlab Friends,
I have following problem that I hope to get help with:
My goal is to film flowing water through a window and detect the water surface line. I attached the original image and the segmented image of the water: As you can see, there is some serious reflection happening, which is segmented as well. Additionally, one party of the sidewall is not see-through resulting in missing parts of the wave.
Ho do I define only the surface as a not-straight line and maybe interpolate the missing part?
• Is there a method for Hough transformation to fit a curved line?
• A second idea would be to get the coordinates of the edges by bwboundaries() and use polynomial curvefitting. However, the whole water body gets detected creating a round-ish object and I don't know to extract only the values of the surface line. The data I have so far is the cell array of coordinates of the red line, surrounding the wave:
wave_coordinates = bwboundaries(BW);
xy = cell2mat(wave_coordinates);
waveX = xy(:,1); %20287x1 double
waveY = xy(:,2); %20287x1 double
I am curious for your solution approaches!
##### 1 CommentShow -1 older commentsHide -1 older comments
Hannah on 3 Jun 2024
A first attempt of mine would be
wave_coordinates = bwboundaries(BW);
xy = cell2mat(wave_coordinates);
%create array with y values
waveY = xy(:,1);
%create array with y values
waveX = xy(:,2);
%find edge points (where x is min and max)
[edge1 idx1] = min(waveX)
[edge2 idx2] = max(waveX)
%find all points that are above the right edge point
idx_upperLine = waveY >= waveY(idx1);
%create array with x and y coordinates of all point above the edge point (which is hopefully the upper Line)
surfaceX = nonzeros(waveX(idx_upperLine));
surfaceY = nonzeros(waveY(idx_upperLine));

Sign in to comment.

### Answers (1)

Kothuri on 21 Jun 2024
Hi Hannah,
Traditional line detection methods like the Hough transform are typically used for straight line detection, may not be directly applied for curved line detection. Since the water surface is not a straight line, polynomial curve fitting, or spline fitting could be more appropriate.
You can use MATLAB's polyfit function to fit a polynomial to fit the water surface line is within a certain region of the image.
For e.g:
% Assuming waveX and waveY are the coordinates and you have filtered them to focus on the water surface
p = polyfit(waveX, waveY, degree); % Choose an appropriate degree
fitY = polyval(p, waveX);
plot(waveX, fitY, 'r-', 'LineWidth', 2); % This plots the fitted curve
You can refer the below link for more info on polyfit
You can also try Spline fitting using MATLAB fit function for a more flexible fit that can better adapt to the curvature of the water surface.
For e.g:
fitType = 'smoothingspline'; % This type of spline allows for smoothing which might be necessary for noisy data
fitOptions = fitoptions('Method', 'SmoothingSpline', 'SmoothingParam', 0.5); % Adjust the smoothing parameter as needed
curveFit = fit(waveX, waveY, fitType, fitOptions);
plot(curveFit, waveX, waveY); % This plots the fitted curve along with the original points
##### 0 CommentsShow -2 older commentsHide -2 older comments

Sign in to comment.

### Categories

Find more on Get Started with Curve Fitting Toolbox 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!