[Image Processing] Detect horizontal lines in image

22 visualizaciones (últimos 30 días)
TP
TP el 25 de Dic. de 2023
Comentada: TP el 25 de Dic. de 2023
I have a photo that has some black horizontal lines as below. They show all over the whole picture (from the top to bottom).
To eliminate those lines, the process I am thinking about is detecting the positions (in term of row) of the lines first, and then do simple linearly interpolations between the right-above row, and the right-after row of that line.
Is there an effective way to detect the positions (in term of row) of those black horizontal lines.
Any ideas would be appreciated.
Thanks.
  2 comentarios
Matt J
Matt J el 25 de Dic. de 2023
Isn't this the same problem you described here,
Why doesn't the answer you accepted there apply?
Image Analyst
Image Analyst el 25 de Dic. de 2023
The noise is different. Here it's thin, single-line, much darker lines whereas in the other question the dark bands were much wider and not so distinct. In my answer below I replace only the thin noise lines and the rest of the image is unaffected.

Iniciar sesión para comentar.

Respuesta aceptada

Image Analyst
Image Analyst el 25 de Dic. de 2023
Try a masked median filter, where you median filter the image, then find the bad lines, then replace only the bad lines with the median filtered lines. It will look better once you magnify the window on your screen.
% Demo by Image Analyst.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format short g;
format compact;
fontSize = 12;
markerSize = 6;
%--------------------------------------------------------------------------------------------------------
% READ IN TEST IMAGE
folder = pwd;
baseFileName = "pic.jpg";
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
% Read in image file.
grayImage = imread(fullFileName);
% Get the vertical profile of the image. Untested code:
[rows, columns, numberOfColorChannels] = size(grayImage)
rows = 1216
columns = 480
numberOfColorChannels = 3
if numberOfColorChannels == 3
grayImage = rgb2gray(grayImage);
end
% Display the image.
subplot(2, 3, 1);
imshow(grayImage, []);
axis('on', 'image');
impixelinfo;
title('Original Image', 'FontSize', fontSize, 'Interpreter', 'None');
verticalProfile = mean(grayImage, 2);
subplot(2, 3, 2);
plot(verticalProfile, 'b-');
xlabel('Row');
ylabel('Mean Gray Level');
grid on;
title('Vertical Profile', 'FontSize', fontSize, 'Interpreter', 'None');
% Then go down that profile getting the average difference between a row and it's two neighbors. Most lines should have a average difference that is low, but rows with a dark line in them should have a higher average difference.
badRows = false(1, rows);
theDifference = zeros(1, rows);
threshold = -10; % Or whatever.
for row = 2 : rows-1
theDifference(row) = mean(2 * single(grayImage(row, :)) - single(grayImage(row-1, :)) - single(grayImage(row+1, :)));
badRows(row) = theDifference(row) < threshold;
end
% Display plot.
subplot(2, 3, 3);
plot(theDifference, 'b-');
xlabel('Row');
ylabel('Mean Gray Level');
title('Average difference of row to its neighbors', 'FontSize', fontSize, 'Interpreter', 'None');
grid on;
yline(threshold, 'Color', 'r', 'LineWidth',2)
% Then median filter the image and replace the bad rows with the median, or with the average of the two rows on either side.
windowWidth = 7;
filteredImage = medfilt2(grayImage, [windowWidth, 1]);
% Display the image.
subplot(2, 3, 4);
imshow(filteredImage, []);
axis('on', 'image');
impixelinfo;
title('Median Filtered Image', 'FontSize', fontSize, 'Interpreter', 'None');
repairedImage = grayImage;
repairedImage(badRows, :) = filteredImage(badRows, :);
% Display the image.
subplot(2, 3, 5);
imshow(repairedImage, []);
axis('on', 'image');
impixelinfo;
title('Repaired Image', 'FontSize', fontSize, 'Interpreter', 'None');
  1 comentario
TP
TP el 25 de Dic. de 2023
The idea of the vertical profile of the image is brilliant.
Thank you so much for your help!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Image Processing Toolbox en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by