how can i Find a midline using two lines

조회 수: 18 (최근 30일)
yoram ifraimov
yoram ifraimov 2022년 5월 2일
답변: Image Analyst 2022년 5월 15일
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
yoram ifraimov
yoram ifraimov 2022년 5월 2일
my matlab code:
% Initialization steps.
close all;
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);
% העברה לבינארי
if ndims(frame) == 3
% It's color. Take the red channel.
grayImage = frame(:, :, 1);
imshow(frame, []);
axis('on', 'image')
mask = logical(frame > 140 & frame < 255);
mask = bwareafilt(mask, 2); % Make sure we have only two lines.
mask = bwskel(mask);
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;
% 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);

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

답변 (2개)

yanqi liu
yanqi liu 2022년 5월 7일
yes,sir,which is the center line,is it like this?
may be check

Image Analyst
Image Analyst 2022년 5월 15일
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.


Help CenterFile Exchange에서 Musical Instrument Digital Interface (MIDI)에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by