how can i Find a midline using two lines

11 views (last 30 days)
yoram ifraimov
yoram ifraimov on 2 May 2022
Answered: Image Analyst on 15 May 2022
I have a code that is supposed to find the middle line in a track built from two lines when you can see this in the following pictures:
  1 Comment
yoram ifraimov
yoram ifraimov on 2 May 2022
my matlab code:
% Initialization steps.
clc;
clearvars;
close all;
workspace;
fontSize = 16;
objects = imaqfind; %find video input objects in memory % מחפש את כל קלטי הוידאו שבזכרון
delete(objects) %delete a video input object from memory %מאפס את כל קלטי הוידאו
load('cameraParams1.mat')% נתונים מהקליברציה
focalLength = [cameraParams.FocalLength(1), cameraParams.FocalLength(2)]; % [fx, fy] in pixel units
principalPoint = [cameraParams.PrincipalPoint(1), cameraParams.PrincipalPoint(2)]; % [cx, cy] optical center in pixel coordinates
imageSize = cameraParams.ImageSize; % [nrows, mcols] 480x640
camIntrinsics = cameraIntrinsics(focalLength, principalPoint, imageSize); %מאחסן את הפרמטרים של המצלמה
height = 0.15; % mounting height in meters from the ground -גובה מיקום המצלמה
pitch = 30; % pitch of the camera in degrees -זווית המצלמה
yaw = 0; % yaw of the camera in degrees
roll = 0; % roll of the camera in degrees
sensor = monoCamera(camIntrinsics, height, 'Pitch', pitch,'Yaw',yaw,'Roll',roll); % חיישן המצלמה- גובה וזווית המצלמה
videoName = 'test3.mp4';
videoReader = VideoReader(videoName);
timeStamp = 0.06667; % time from the beginning of the video
videoReader.CurrentTime = timeStamp; % point to the chosen frame
frame = readFrame(videoReader); % read frame at timeStamp seconds
imshow(frame) % display frame
% Using vehicle coordinates, define area to transform
distAheadOfSensor = 1.; % in meters, as previously specified in monoCamera height input-
spaceToOneSide = 0.5; % all other distance quantities are also in meters-
bottomOffset = 0.15;
outView = [bottomOffset, distAheadOfSensor, -spaceToOneSide, spaceToOneSide]; % [xmin, xmax, ymin, ymax]
imageSize = [NaN, 1000]; % output image width in pixels; height is chosen automatically to preserve units per pixel ratio
birdsEyeConfig = birdsEyeView(sensor, outView, imageSize);
% birdsEyeImage = transformImage(birdsEyeConfig, frame);
% figure
% imshow(birdsEyeImage)
% % העברה מצבעוני לאפור
frame = rgb2gray(frame);
figure
imshow(frame)
% העברה לבינארי
frame(frame<170)=0;
figure
imshow(frame)
if ndims(frame) == 3
% It's color. Take the red channel.
grayImage = frame(:, :, 1);
end
figure
imshow(frame, []);
impixelinfo;
axis('on', 'image')
mask = logical(frame > 140 & frame < 255);
mask = bwareafilt(mask, 2); % Make sure we have only two lines.
mask = bwskel(mask);
figure
imshow(mask);
impixelinfo;
axis('on', 'image')
labeledImage = bwlabel(mask);
line1 = ismember(labeledImage, 1);
line2 = ismember(labeledImage, 2);
% Get rows and columns of each line.
[r1, c1] = find(line1);
[r2, c2] = find(line2);
for k = 1 : length(r1)
distances = sqrt(((r1(k) - r2) .^ 2) + ((c1(k) - c2) .^ 2));
[minDistance, index] = min(distances);
% Find the midPoint
midX(k) = mean([c1(k), c2(index)]);
midY(k) = mean([r1(k), r2(index)]);
% Burn into mask
mask(round(midY(k)), round(midX(k))) = true;
end
% Need to add a small amount of noise to x to make the values unique.
midX = midX + 0.001 * rand(size(midX));
midY = midY + 0.001 * rand(size(midY));
% Interpolate x and y to make sure there are no gaps.
kVec = 1 : length(midX);
kFit = linspace(1, kVec(end), 10000);
xFit = interp1(kVec, midX, kFit, 'linear');
yFit = interp1(kVec, midY, kFit, 'linear');
% Remove duplicate values
xy = unique(round([xFit(:), yFit(:)]), "rows");
% Extract individual x and y.
midX = xy(:, 1);
midY = xy(:, 2);
hold on;
plot(midX, midY, 'g.', 'MarkerSize', 10);

Sign in to comment.

Answers (2)

yanqi liu
yanqi liu on 7 May 2022
yes,sir,which is the center line,is it like this?
may be check
https://ww2.mathworks.cn/matlabcentral/answers/1712640-how-to-create-centerline-between-to-lines-in-image#answer_958935

Image Analyst
Image Analyst on 15 May 2022
I know for a fact that I solved this. Here it is:
Was this yours and @Dekel Mashiach's homework that I did for you? Otherwise why do two people have the very same image? Or are you and @Dekel Mashiach the same person, just with two different accounts.

Community Treasure Hunt

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

Start Hunting!

Translated by