Borrar filtros
Borrar filtros

find 0 crossing points trough all the image rows

1 visualización (últimos 30 días)
mikel lasa
mikel lasa el 11 de Mzo. de 2021
Comentada: Satwik el 7 de Jun. de 2024
Hello all,
I have a vertical laser stripe image and my goal is to find the 0 crossing point of every row of the image in order to plot them as points afterwards.
This is my code, I have done the process for one row of the image. Now I have problems to do this same process for all the rows of the image. I'm trying to do that with a for loop but no way.
Resuming, is to to apply this same code for all the image rows. Thanks in advance!
clc; clear; close all;
%% calibracion de laser real con blur
%cargar la imagen que se va a medir
img=imread(fullfile('LaserTestPatternBlurred.bmp'));
img=imrotate(img,90);
% imshow(img)
% title('Imagen original')
%% procesado de la imagen
img_o=rgb2gray(img);
%mask = (I(:, :, 1) > 100) & (I(:, :, 2) > 100);
mask=(img_o(:,:) > 50);
img = bsxfun(@times, img_o, cast(mask, 'like', img_o));
% figure()
% imshow(img)
% title('Binarizada')
%% seleccionar una linea de la imagen y buscar su 0 crossing
[rows, columns, numberOfColorChannels] = size(img);
%seleccionamos la linea 900 y se le aplica un filtro para suavizar
linea=img(900,:);
sm=sgolayfilt(double(linea),2,25);
% figure()
% plot(img_o(900,:),'b')
% hold on
% plot(linea,'r')
% plot(sm,'g')
% grid on
% ylim([0 255])
% title('Linea')
% legend('imagen','linea','filtrado')
% hold off
%buscamos los picos maximos de la gradiente
Lder=gradient(sm);
[picosp,locsp]=findpeaks(Lder);
[picosn,locsn]=findpeaks(-Lder);
% figure()
% plot(Lder)
% hold on
% scatter(locsn(1,2),-picosn(1,2),'g')
% scatter(locsp(1,1),picosp(1,1),'g')
% plot([locsn(1,2) locsp(1,1)],[-picosn(1,2) picosp(1,1)])
% axis([450 550 -15 15])
% grid on
%obtener la ecuacion de la recta entre los maximos y el paso por 0
recta = polyfit([locsp, locsn], [picosp, -picosn], 1);
a = recta (1);
b = recta (2);
subpixel= -b/(a);
scatter(subpixel,0,'r')
hold off

Respuestas (1)

Satwik
Satwik el 29 de Mayo de 2024
Hi
To apply the process of finding 0 crossing point of one row across all rows of the image, you can encapsulate your logic into a loop that iterates over each row. To visualize the 0 crossing points for all rows, you can collect their coordinates and plot them all at once after processing all rows.
Here is how you can modify your code to achieve your goal.
%% Load and prepare the image
img = imread(fullfile('LaserTestPatternBlurred.bmp'));
img = imrotate(img, 90);
%% Convert to grayscale and apply a threshold
img_o = rgb2gray(img);
mask = (img_o(:,:) > 50);
img = bsxfun(@times, img_o, cast(mask, 'like', img_o));
%% Initialize variables for plotting
[rows, columns, ~] = size(img);
zeroCrossingsX = zeros(rows, 1); % Pre-allocate for speed
zeroCrossingsY = (1:rows)'; % Y-coordinates for each row
%% Process each row to find the 0 crossing
for rowIndex = 1:rows
% Select a row and apply a filter to smooth it
linea = img(rowIndex,:);
sm = sgolayfilt(double(linea), 2, 25);
% Find the peaks in the gradient of the smoothed line
Lder = gradient(sm);
[picosp, locsp] = findpeaks(Lder);
[picosn, locsn] = findpeaks(-Lder);
% If peaks are found, calculate the zero crossing
if ~isempty(locsp) && ~isempty(locsn)
% Fit a line between the peaks and find the zero crossing
recta = polyfit([locsp(1), locsn(1)], [picosp(1), -picosn(1)], 1);
a = recta(1);
b = recta(2);
subpixel = -b/a;
% Store the zero crossing x-coordinate
zeroCrossingsX(rowIndex) = subpixel;
else
% If no peaks are found, use NaN or another placeholder value
zeroCrossingsX(rowIndex) = NaN;
end
end
%% Plot the zero crossings for all rows
figure;
scatter(zeroCrossingsX, zeroCrossingsY, 'r.');
title('Zero Crossings for All Rows');
xlabel('Column Index');
ylabel('Row Index');
axis ij; % To match the image coordinates
Hope this helps!
  2 comentarios
Image Analyst
Image Analyst el 29 de Mayo de 2024
@Satwik where did you get the test image? I don't see it attached to either your Answer or the original poster's 3 year old message. Can you attach it? Also show some screenshots of how well your algorithm worked, otherwise we're just blind and have no idea.
Satwik
Satwik el 7 de Jun. de 2024
Hi @Image Analyst, here the algorithm used for finding the zero crossing is not mine. I am just extending the code given in the question to cover all rows. I used a random test image which I am attaching here, and got the following output.

Iniciar sesión para comentar.

Productos


Versión

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by