Azzera filtri
Azzera filtri

Extend line until the image borders.

15 visualizzazioni (ultimi 30 giorni)
THULYO NEDER
THULYO NEDER il 30 Set 2020
Commentato: Rik il 26 Dic 2020
Hey,
I have plotted few lines in an image and I want to extend these lines until the image borders (end).
I tried to call ''aLine'' command and also use some basics trigonometry, but it did not work for all the lines.
Any idea?
Thanks.
  2 Commenti
Image Analyst
Image Analyst il 26 Dic 2020
Not sure what your edit was. But do you want lines in the overlay with the line() function? Or do you want lines burned into the image?
Rik
Rik il 26 Dic 2020
The original question was still on the Google cache.
@Thulyo, why did you edit away most of your question?

Accedi per commentare.

Risposta accettata

Image Analyst
Image Analyst il 30 Set 2020
Do you have the equation of each line? If so, simply put in 1 and the max x value to get the two y values. Then call plot. Any part of the line outside the axes simply won't draw so it will have the effect of crossing the entire image.

Più risposte (1)

Image Analyst
Image Analyst il 26 Dic 2020
Thulyo, I'm not sure if you want the lines in the overlay or burned into the image, so I did it both ways:
% Demo by Image Analyst, December, 2020.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clearvars;
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 16;
fprintf('Beginning to run %s.m ...\n', mfilename);
%-----------------------------------------------------------------------------------------------------------------------------------
% Read in image.
folder = [];
baseFileName = 'peppers.png';
fullFileName = fullfile(folder, baseFileName);
% Check if file exists.
if ~exist(fullFileName, 'file')
% The file doesn't exist -- didn't find it there in that folder.
% Check the entire search path (other folders) for the file by stripping off the folder.
fullFileNameOnSearchPath = baseFileName; % No path this time.
if ~exist(fullFileNameOnSearchPath, 'file')
% Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist in the search path folders.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
rgbImage = imread(fullFileName);
[rows, columns, numberOfColorChannels] = size(rgbImage)
% Display the test image full size.
subplot(2, 2, 1);
imshow(rgbImage, []);
axis('on', 'image');
caption = sprintf('Original Image : "%s"', baseFileName);
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
% Set up figure properties:
% Enlarge figure to full screen.
hFig1 = gcf;
hFig1.Units = 'Normalized';
hFig1.WindowState = 'maximized';
% Get rid of tool bar and pulldown menus that are along top of figure.
% set(gcf, 'Toolbar', 'none', 'Menu', 'none');
% Give a name to the title bar.
hFig1.Name = 'Demo by Image Analyst';
%--------------------------------------------------------------------------------------------------------
% Get randomly located pairs of points.
numLines = 30;
lineColors = jet(numLines);
x = randi(columns, numLines, 2);
y = randi(rows, numLines, 2);
for k = 1 : numLines
% Display in overlay above the image.
line(x(k, :), y(k, :), 'Color', lineColors(k, :), 'LineWidth', 2);
end
% Display image again.
subplot(2, 2, 2);
imshow(rgbImage, []);
axis('on', 'image');
caption = sprintf('%d Lines in Overlay, Extended', numLines);
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
hold on;
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
%==============================================================================================
% Now extend these lines in the overlay until the image edge.
for k = 1 : numLines
coefficients = polyfit(x(k, :), y(k, :), 1);
% Define x values to be the whole width of the image.
xFit = 1 : columns;
% Get y values. Note: some may be less than 1 or more than rows but we will adjust.
yFit = polyval(coefficients, xFit);
goodIndexes = yFit >= 1 & yFit <= rows;
xFit = xFit(goodIndexes);
yFit = yFit(goodIndexes);
% Display in overlay above the image.
line([xFit(1), xFit(end)], [yFit(1), yFit(end)], 'Color', lineColors(k, :), 'LineWidth', 2);
end
%==============================================================================================
% Now extend these lines BURNED INTO the image until the image edge.
[redChannel, greenChannel, blueChannel] = imsplit(rgbImage);
for k = 1 : numLines
coefficients = polyfit(x(k, :), y(k, :), 1);
% Define x values to be the whole width of the image.
xFit = 1 : columns;
% Get y values. Note: some may be less than 1 or more than rows but we will adjust.
yFit = polyval(coefficients, xFit);
% Round to integer coordinates.
yFit = round(yFit);
goodIndexes = yFit >= 1 & yFit <= rows;
xFit = xFit(goodIndexes);
yFit = yFit(goodIndexes);
% Burn into each color channel
for k2 = 1 : length(yFit)
row = yFit(k2);
col = xFit(k2);
redChannel(row, col) = round(255 * lineColors(k, 1)); % Assign the red channel the red color of our line.
greenChannel(row, col) = round(255 * lineColors(k, 2)); % Assign the green channel the green color of our line.
blueChannel(row, col) = round(255 * lineColors(k, 3)); % Assign the blue channel the blue color of our line.
end
line([xFit(1), xFit(end)], [yFit(1), yFit(end)], 'Color', lineColors(k, :), 'LineWidth', 2);
end
% Build the output image from the individual color channels.
rgbImage2 = cat(3, redChannel, greenChannel, blueChannel);
% Display new image with burned in colors.
subplot(2, 2, 3);
imshow(rgbImage2, []);
axis('on', 'image');
caption = sprintf('%d Single Pixel Wide Lines Burned In to the Image', numLines);
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
hold on;
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
Note that some of the burned in lines may appear broken in the display because they are only one pixel wide and may have gotten subsampled away when the image was shrunk down for display.
But note how the partial lines showing in the upper left image have in both cases been extended until they hit the edge of the image.

Community Treasure Hunt

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

Start Hunting!

Translated by