[Image Processing] Detect horizontal lines in image
12 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentare
Matt J
am 25 Dez. 2023
Isn't this the same problem you described here,
Why doesn't the answer you accepted there apply?
Image Analyst
am 25 Dez. 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.
Akzeptierte Antwort
Image Analyst
am 25 Dez. 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)
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');
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Image Processing Toolbox finden Sie in Help Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!