【学习日记】图像网格条纹去除算法总结

参考资料:

opencv线检测
https://docs.opencv.org/4.x/dd/dd7/tutorial_morph_lines_detection.html

霍夫变换线检测
https://docs.opencv.org/4.x/d9/db0/tutorial_hough_lines.html

fft
https://blog.csdn.net/crystal_remember/article/details/117330680
https://blog.csdn.net/weixin_39450742/article/details/118354529
https://blog.csdn.net/stq054188/article/details/113133533

陷波滤波器
https://blog.csdn.net/weixin_40267472/article/details/81385751
https://blog.csdn.net/weixin_43048780/article/details/121090685

isect_segments-bentley_ottmann 算法
https://github.com/ideasman42/isect_segments-bentley_ottmann
https://stackoverflow.com/questions/45322630/how-to-detect-lines-in-opencv


fft方法

需要手动标注,并做阈值分割等后处理

% https://blog.csdn.net/Crystal_remember/article/details/117330680
close all;
clear all;
clc;

%%
I = imread('test.bmp'); 
I = rgb2gray(I);
totMask = zeros(size(I)); % accumulate all single object masks to this one 
f = figure('CurrentCharacter','a'); 
imshow(I) 
 
FFT = fft2(I);
myangle = angle(FFT);             %相位谱(没有进行移位的)
FS = abs(fftshift(FFT));          % 移位,使低频成分集中到图像中心,并得到幅度谱
S = log(1+abs(FS));
figure,imshow(S,[]);
 
small=min(min(FS));
h = imfreehand(gca); setColor(h,'green'); 
position = wait(h); 
BW = createMask(h); 
totMask = totMask | BW; % add mask to global mask 
while double(get(f,'CurrentCharacter'))~=27 
    % ask user for another mask 
    h = imfreehand(gca); 
    if isempty(h) 
     % User pressed ESC, or something else went wrong 
     continue 
    end 
    setColor(h,'green'); 
    position = wait(h); 
    BW = createMask(h); 
    totMask = totMask | BW; % add mask to global mask 
    pause(.1) 
end 
% show the resulting mask 
% figure; imshow(totMask); title('multi-object mask'); 
 
cost3 = FS;
cost3(totMask == 1) = small ;
FS = cost3; 
 
resul_s  = ifftshift(FS);               % 将处理后的幅度图反移位,恢复到正常状态
result_f = resul_s.*cos(myangle) + resul_s.*sin(myangle).*1i;      % 幅度值和相位值重新进行结合,得到复数
result_image = abs(ifft2(result_f ));               % 进行傅里叶反变换,得到处理后的时域图像
result = im2uint8(mat2gray(result_image));       
figure,imshow(result);       %去除网纹成分后的图像

%------
BW = imbinarize(result,0.8); % 二值化分割
figure,imshow(BW);

效果如下,原图略。
【学习日记】图像网格条纹去除算法总结_第1张图片

概率霍夫线检测算法

# https://docs.opencv.org/4.x/d9/db0/tutorial_hough_lines.html

import sys
import math
import cv2 as cv
import numpy as np

def main(argv):
    default_file = 'test.bmp'
    filename = argv[0] if len(argv) > 0 else default_file

    src = cv.imread(cv.samples.findFile(filename), cv.IMREAD_GRAYSCALE)

    if src is None:
        print ('Error opening image!')
        print ('Usage: hough_lines.py [image_name -- default ' + default_file + '] \n')
        return -1

    # Edge detection
    dst = cv.Canny(src, 0, 150, None, 3)

    # Copy edges to the images that will display the results in BGR
    cdst = cv.cvtColor(dst, cv.COLOR_GRAY2BGR)
    cdstP = np.copy(cdst)

    #  Standard Hough Line Transform
    # rho: 参数的分辨率r以像素为单位
    # 返回值lines为结构体数组,包含端点1、2, 角度, rho
    # lines = cv.HoughLines(dst, 10, np.pi / 180,
    #                       150, None, 0, 0)

    # # Draw the lines
    # if lines is not None:
    #     for i in range(0, len(lines)):
    #         rho = lines[i][0][0]
    #         theta = lines[i][0][1]
    #         a = math.cos(theta)
    #         b = math.sin(theta)
    #         x0 = a * rho
    #         y0 = b * rho
    #         pt1 = (int(x0 + 1000*(-b)), int(y0 + 1000*(a)))
    #         pt2 = (int(x0 - 1000*(-b)), int(y0 - 1000*(a)))
    #         cv.line(cdst, pt1, pt2, (0,0,255), 3, cv.LINE_AA)

    # Probabilistic Line Transform
    # 累计概率霍夫变换
    # 用法:(image, rho, theta, threshold,
    #           lines=None, minLineLength=None, maxLineGap=None)
    linesP = cv.HoughLinesP(dst, 10, np.pi / 180, 150,
                            None, 500, 20)

    # Draw the lines
    if linesP is not None:
        for i in range(0, len(linesP)):
            l = linesP[i][0]
            # (img, pt1, pt2, color, thickness=None, lineType=None, shift=None)
            cv.line(cdstP, (l[0], l[1]), (l[2], l[3]), (0,0,255), 5, cv.LINE_AA)

    # 阈值分割 mask
    # 150以上的设为全黑
    ret, mask = cv.threshold(src, 50, 255, 1) #简单二值化
    cv.imwrite("1-mask.png",mask)

    # 对霍夫P变换检测到的线作为二次判断条件
    # print(cdstP[0,0,0])
    # print(cdstP.shape)
    # print(cdstP[1256,1257])
    # print(cdstP[1256,1257][2])
    # for i in range(cdstP.shape[0]):
    #     for j in range(cdstP.shape[1]):
    #         flag = (cdstP[i, j][0] == 0) & (cdstP[i, j][1] == 0) & (cdstP[i, j][2] == 255)
    #         if flag:
    #             cdstP[i, j] = (255,255,255)
    #         else:
    #             cdstP[i, j] = (0,0,0)
    for i in range(cdstP.shape[0]):
        for j in range(cdstP.shape[1]):
            # 对于阈值分割结果mask:如果认为是网格,像素值设置为0(全黑);否则不处理
            flag = (cdstP[i, j][0] == 0) & (cdstP[i, j][1] == 0) & (cdstP[i, j][2] == 255)
            if flag:
                mask[i, j] = 0
            else:
                pass

    # Show results
    cv.imshow("Source", src)
    # cv.imshow("Standard Hough Line Transform", cdst)
    cv.imshow("Probabilistic Line Transform", cdstP)
    cv.imshow("threshold", mask)

    cv.imwrite("2-HoughLinesP.png",cdstP)
    cv.imwrite("3-result.png",mask)

    cv.waitKey()
    return 0

if __name__ == "__main__":
    main(sys.argv[1:])

效果如下,原图略。
【学习日记】图像网格条纹去除算法总结_第2张图片

bentley-otmann算法

# https://stackoverflow.com/questions/45322630/how-to-detect-lines-in-opencv

import cv2
import numpy as np
import isect_segments_bentley_ottmann.poly_point_isect as bot

img = cv2.imread('test.bmp')
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

kernel_size = 5
blur_gray = cv2.GaussianBlur(gray,(kernel_size, kernel_size),0)

low_threshold = 0
high_threshold = 150
edges = cv2.Canny(blur_gray, low_threshold, high_threshold)

rho = 1  # distance resolution in pixels of the Hough grid
theta = np.pi / 180  # angular resolution in radians of the Hough grid
threshold = 15  # minimum number of votes (intersections in Hough grid cell)
min_line_length = 50  # minimum number of pixels making up a line
max_line_gap = 20  # maximum gap in pixels between connectable line segments
line_image = np.copy(img) * 0  # creating a blank to draw lines on

# Run Hough on edge detected image
# Output "lines" is an array containing endpoints of detected line segments
lines = cv2.HoughLinesP(edges, rho, theta, threshold, np.array([]),
                    min_line_length, max_line_gap)
print(lines)
points = []
for line in lines:
    for x1, y1, x2, y2 in line:
        points.append(((x1 + 0.0, y1 + 0.0), (x2 + 0.0, y2 + 0.0)))
        cv2.line(line_image, (x1, y1), (x2, y2), (255, 0, 0), 5)

lines_edges = cv2.addWeighted(img, 0.8, line_image, 1, 0)
print(lines_edges.shape)
#cv2.imwrite('line_parking.png', lines_edges)

print(points)
intersections = bot.isect_segments(points)
print(intersections)

for inter in intersections:
    a, b = inter
    for i in range(3):
        for j in range(3):
            lines_edges[int(b) + i, int(a) + j] = [0, 255, 0]

cv2.imwrite('line_parking.png', lines_edges)

你可能感兴趣的:(学习,算法,opencv)