DFT 低通滤波器
This commit is contained in:
43
pkg/rrc/filter_test.go
Normal file
43
pkg/rrc/filter_test.go
Normal file
@@ -0,0 +1,43 @@
|
||||
package rrc
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/airbusgeo/godal"
|
||||
"gocv.io/x/gocv"
|
||||
"starwiz.cn/sjy01/image-proc/pkg/utils"
|
||||
)
|
||||
|
||||
func TestFilter(t *testing.T) {
|
||||
godal.RegisterAll()
|
||||
tif := "/Users/lan/workspace/sjy01/image-proc/data/052022-no-rrc/010/PAN/SJY01_PAN_20240520_115428_052022_103_010_L1A.tiff"
|
||||
tif = "/Users/lan/workspace/sjy01/image-proc/data/051622/007/PAN/SJY01_PAN_20240516_101236_051622_096_007_L1A.tiff"
|
||||
// tif = "/Users/lan/workspace/sjy01/image-proc/data/060622-mm/003/PAN/SJY01_PAN_20240606_012004_060622_065_003_L1A.tiff"
|
||||
|
||||
ds, err := godal.Open(tif)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
bands := ds.Bands()
|
||||
width := bands[0].Structure().SizeX
|
||||
height := bands[0].Structure().SizeY
|
||||
img := gocv.NewMatWithSize(height, width, gocv.MatTypeCV16UC1)
|
||||
|
||||
data := make([]uint16, width*height)
|
||||
err = bands[0].Read(0, 0, data, width, height)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
// 将 16 位数据存储到图像的相应通道
|
||||
for y := 0; y < height; y++ {
|
||||
for x := 0; x < width; x++ {
|
||||
value := data[y*width+x]
|
||||
img.SetShortAt(y, x, int16(value))
|
||||
}
|
||||
}
|
||||
|
||||
m0 := HFNoiseFilter(img, float64(img.Cols())*0.45)
|
||||
defer m0.Close()
|
||||
utils.SavePanToGDALGTiff(m0, 0, 0, "/Users/lan/workspace/sjy01/image-proc/data/fft/filtered.tiff", 1.3)
|
||||
}
|
||||
118
pkg/rrc/hf_filter.go
Normal file
118
pkg/rrc/hf_filter.go
Normal file
@@ -0,0 +1,118 @@
|
||||
package rrc
|
||||
|
||||
import (
|
||||
"image"
|
||||
"math"
|
||||
|
||||
"github.com/sirupsen/logrus"
|
||||
"gocv.io/x/gocv"
|
||||
)
|
||||
|
||||
// filter high frequence noise
|
||||
func HFNoiseFilter(img gocv.Mat, radius float64) gocv.Mat {
|
||||
logrus.Println("applying hf noise filter...")
|
||||
imgFloat := gocv.NewMat()
|
||||
img.ConvertTo(&imgFloat, gocv.MatTypeCV32F)
|
||||
|
||||
// rows, cols := img.Rows(), img.Cols()
|
||||
// utils.SavePanToGDALGTiff(img, 0, 0, "/Users/lan/workspace/sjy01/image-proc/data/fft/original.tiff", 1.3)
|
||||
|
||||
// 将图像转换为32位浮点型
|
||||
img32f := gocv.NewMat()
|
||||
img.ConvertTo(&img32f, gocv.MatTypeCV32F)
|
||||
defer img32f.Close()
|
||||
|
||||
// 获取图像尺寸
|
||||
rows, cols := img32f.Rows(), img32f.Cols()
|
||||
|
||||
// zeros := gocv.NewMatFromScalar(gocv.NewScalar(0, 0, 0, 0), gocv.MatTypeCV32F)
|
||||
|
||||
// 执行 DFT
|
||||
DFT := gocv.NewMat()
|
||||
defer DFT.Close()
|
||||
|
||||
planes := gocv.NewMatWithSize(rows, cols, gocv.MatTypeCV32F)
|
||||
img32f.ConvertTo(&planes, gocv.MatTypeCV32F)
|
||||
gocv.Merge([]gocv.Mat{planes, gocv.Zeros(rows, cols, gocv.MatTypeCV32F)}, &DFT)
|
||||
gocv.DFT(DFT, &DFT, gocv.DftComplexOutput)
|
||||
fftshift(&DFT)
|
||||
// 将复数部分分离并计算振幅光谱
|
||||
pss := gocv.Split(DFT)
|
||||
// SaveDFT(DFT, "/Users/lan/workspace/sjy01/image-proc/data/fft/dft.tiff")
|
||||
|
||||
// 应用滤波器
|
||||
for y := 0; y < rows; y++ {
|
||||
for x := 0; x < cols; x++ {
|
||||
if math.Sqrt(math.Pow(float64(x-cols/2), 2)+math.Pow(float64(y-rows/2), 2)) > float64(radius) {
|
||||
// if math.Abs(float64(x-cols/2)) < float64(radius) || math.Abs(float64(y-rows/2)) < float64(radius) {
|
||||
pss[0].SetFloatAt(y, x, 0.0)
|
||||
pss[1].SetFloatAt(y, x, 0.0)
|
||||
}
|
||||
}
|
||||
}
|
||||
gocv.Merge(pss, &DFT)
|
||||
// SaveDFT(DFT, "/Users/lan/workspace/sjy01/image-proc/data/fft/dft_filtered.tiff")
|
||||
fftshift(&DFT)
|
||||
// 执行 IDFT
|
||||
IDFT := gocv.NewMat()
|
||||
gocv.DFT(DFT, &IDFT, gocv.DftInverse|gocv.DftRealOutput)
|
||||
|
||||
// 归一化到0-65535
|
||||
gocv.Normalize(IDFT, &IDFT, 0, 8192, gocv.NormMinMax)
|
||||
IDFT.ConvertTo(&IDFT, gocv.MatTypeCV16U)
|
||||
|
||||
// 保存 IDFT 结果
|
||||
return IDFT
|
||||
}
|
||||
|
||||
// 创建低通滤波器
|
||||
func createLowPassFilter(filter *gocv.Mat, radius int) {
|
||||
rows, cols := filter.Rows(), filter.Cols()
|
||||
center := image.Pt(cols/2, rows/2)
|
||||
for y := 0; y < rows; y++ {
|
||||
for x := 0; x < cols; x++ {
|
||||
dist := math.Sqrt(math.Pow(float64(x-center.X), 2) + math.Pow(float64(y-center.Y), 2))
|
||||
if dist <= float64(radius) {
|
||||
filter.SetFloatAt(y, x, 1.0)
|
||||
} else {
|
||||
filter.SetFloatAt(y, x, 0.0)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// fftshift 将频率直流分量移动到图像中心
|
||||
func fftshift(input *gocv.Mat) {
|
||||
cx, cy := input.Cols()/2, input.Rows()/2
|
||||
|
||||
q0 := input.Region(image.Rect(0, 0, cx, cy)) // Top-Left
|
||||
q1 := input.Region(image.Rect(cx, 0, input.Cols(), cy)) // Top-Right
|
||||
q2 := input.Region(image.Rect(0, cy, cx, input.Rows())) // Bottom-Left
|
||||
q3 := input.Region(image.Rect(cx, cy, input.Cols(), input.Rows())) // Bottom-Right
|
||||
|
||||
tmp := gocv.NewMatWithSize(q0.Rows(), q0.Cols(), input.Type())
|
||||
q0.CopyTo(&tmp)
|
||||
q3.CopyTo(&q0)
|
||||
tmp.CopyTo(&q3)
|
||||
|
||||
q1.CopyTo(&tmp)
|
||||
q2.CopyTo(&q1)
|
||||
tmp.CopyTo(&q2)
|
||||
}
|
||||
|
||||
func SaveDFT(DFT gocv.Mat, file string) {
|
||||
pss := gocv.Split(DFT)
|
||||
planes := gocv.NewMat()
|
||||
gocv.Magnitude(pss[0], pss[1], &planes)
|
||||
// 进行对数尺度变换,便于可视化
|
||||
ones := gocv.NewMatFromScalar(gocv.NewScalar(1, 0, 0, 0), gocv.MatTypeCV64F)
|
||||
gocv.Add(planes, ones, &planes)
|
||||
gocv.Log(planes, &planes)
|
||||
|
||||
// 归一化
|
||||
gocv.Normalize(planes, &planes, 0, 1, gocv.NormMinMax)
|
||||
|
||||
// 保存DFT结果
|
||||
gocv.IMWrite(file, planes)
|
||||
|
||||
}
|
||||
56
pkg/rrc/hf_filter_fft.go
Normal file
56
pkg/rrc/hf_filter_fft.go
Normal file
@@ -0,0 +1,56 @@
|
||||
package rrc
|
||||
|
||||
import (
|
||||
"math"
|
||||
|
||||
"github.com/mjibson/go-dsp/fft"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"gocv.io/x/gocv"
|
||||
)
|
||||
|
||||
func HFNoiseFilterFFT(input gocv.Mat, radius int) gocv.Mat {
|
||||
// 将灰度图像转换为浮点数数组
|
||||
height := input.Rows()
|
||||
width := input.Cols()
|
||||
|
||||
data := make([][]complex128, height)
|
||||
for y := 0; y < height; y++ {
|
||||
data[y] = make([]complex128, width)
|
||||
for x := 0; x < width; x++ {
|
||||
grayColor := float64(uint16(input.GetShortAt(y, x)))
|
||||
data[y][x] = complex(grayColor, 0)
|
||||
}
|
||||
}
|
||||
|
||||
// 进行二维DFT
|
||||
log.Println("Performing 2D DFT...")
|
||||
dft := fft.FFT2(data)
|
||||
// 应用低通滤波器
|
||||
log.Println("Applying low-frequency-pass filter...")
|
||||
for y := 0; y < height; y++ {
|
||||
for x := 0; x < width; x++ {
|
||||
if math.Abs(float64(x-width/2)) < float64(radius) || math.Abs(float64(y-height/2)) < float64(radius) {
|
||||
dft[y][x] = 0
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 进行二维逆DFT
|
||||
log.Println("Performing 2D inverse DFT...")
|
||||
idft := fft.IFFT2(dft)
|
||||
|
||||
// 将浮点数数组转换回灰度图像
|
||||
for y := 0; y < height; y++ {
|
||||
for x := 0; x < width; x++ {
|
||||
realPart := real(idft[y][x])
|
||||
if realPart < 0 {
|
||||
realPart = 0
|
||||
} else if realPart > 65535 {
|
||||
realPart = 65535
|
||||
}
|
||||
input.SetShortAt(y, x, int16(uint16(realPart)))
|
||||
}
|
||||
}
|
||||
|
||||
return input
|
||||
}
|
||||
15
pkg/rrc/mean_filter.go
Normal file
15
pkg/rrc/mean_filter.go
Normal file
@@ -0,0 +1,15 @@
|
||||
package rrc
|
||||
|
||||
import (
|
||||
"image"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
"gocv.io/x/gocv"
|
||||
)
|
||||
|
||||
func MeanFilter(img gocv.Mat, ksize int) gocv.Mat {
|
||||
log.Println("Applying mean filter, kernel size:", ksize)
|
||||
meanFilteredImg := gocv.NewMat()
|
||||
gocv.Blur(img, &meanFilteredImg, image.Pt(ksize, ksize))
|
||||
return meanFilteredImg
|
||||
}
|
||||
Reference in New Issue
Block a user