如何在OpenCV中从图像中提取条纹区域?

3
我正在尝试从这张图片中提取条纹区域。

enter image description here

这是我想要提取的区域。

enter image description here

可能有多种方法,也许可以结合使用。
  1. 使用阈值处理、形态学和Grabcut操作提取条纹
  2. Gabor滤波器
  3. 傅里叶变换

如何从fft2变换中检测条纹的方向。

import numpy as np
import cv2 
import os
import sys
from matplotlib import pyplot as plt


plt.figure(figsize=(12, 12))
gray = cv2.imread('zebra.jpg',0)    
f = np.fft.fft2(gray)
fshift = np.fft.fftshift(f)
magnitude_spectrum = 20*np.log(np.abs(fshift))
imgs_comb = np.hstack([gray,magnitude_spectrum])
plt.axis('off')
plt.title('magnitude_spectrum')
plt.imshow(imgs_comb,cmap='gray')   
plt.show()

这段文字的大致意思是:这组图像基于它们的路面类型和涂料新旧是独特的。通常情况下,涂料已经磨损。尽管存在所有这些变化,FFT图像始终为我提供正确的方向和频率。结果看起来很有前途,我可以直观地看到模式的频率以及它们的方向(图像中主要垂直模式)。如何使用FFT图像来过滤其他区域?欢迎使用其他方法提出建议。

enter image description here

1个回答

0

不要使用fft2,而是使用阈值化+形态学的方法。这个思路是通过Otsu二值化得到一个二进制图像,然后使用形态学来检测水平线。从这里我们将检测到的线绘制到掩模上,然后执行其他形态学操作,将条纹合并成单一轮廓。从这里我们找到边界矩形并提取ROI。


大津阈值 -> 在掩码上绘制线条

enter image description here enter image description here

-> 开始/结束 -> 检测到ROI -> 提取的ROI

enter image description here enter image description here enter image description here

import cv2
import numpy as np

image = cv2.imread('1.jpg')
mask = np.zeros(image.shape, dtype=np.uint8)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
thresh = cv2.threshold(gray, 0 ,255, cv2.THRESH_OTSU + cv2.THRESH_BINARY)[1]

horizontal_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (30,1))
detect_horizontal = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, horizontal_kernel, iterations=2)
cnts = cv2.findContours(detect_horizontal, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
for c in cnts:
    cv2.drawContours(mask, [c], -1, (255,255,255), 5)

mask = cv2.cvtColor(mask, cv2.COLOR_BGR2GRAY)
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3,5))
close = cv2.morphologyEx(mask, cv2.MORPH_CLOSE, kernel, iterations=3)
opening = cv2.morphologyEx(close, cv2.MORPH_OPEN, kernel, iterations=2)

x,y,w,h = cv2.boundingRect(opening)
ROI = image[y:y+h, x:x+w]

cv2.imshow('thresh', thresh)
cv2.imshow('mask', mask)
cv2.imshow('opening', opening)
cv2.imshow('ROI', ROI)
cv2.waitKey()

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接