Merge branch 'filter-jiangruoyu'

This commit is contained in:
copper 2022-11-11 14:38:44 +08:00
commit 1762e760ef
7 changed files with 221 additions and 6 deletions

4
ECD.py
View File

@ -1,8 +1,8 @@
import os
import sys
sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'libs'))
os.environ['PROJ_LIB'] = os.path.join(os.path.dirname(__file__), 'share/proj')
os.environ['GDAL_DATA'] = os.path.join(os.path.dirname(__file__), 'share')
# os.environ['PROJ_LIB'] = os.path.join(os.path.dirname(__file__), 'share/proj')
# os.environ['GDAL_DATA'] = os.path.join(os.path.dirname(__file__), 'share')
os.environ['ECD_BASEDIR'] = os.path.dirname(__file__)
BASE_DIR = os.path.dirname(__file__)
from rscder import MulStart

View File

@ -1 +1 @@
vd4FiYncytyziGH9GNCAA8hGGr1/79Xmphtc5+PHPJDpxvqj1hP7+985QMojYO4M5Qn/aqEAvFgeDN3CA8x1YAK8SdCgSXSBJpRBK8wqPQjBY1ak96QfdPCrTLunr+xuPxK3Gxe772adTTsee2+ot7WePYUsC4y4NcS5+rlP1if87xtYqVeSwx3c64cOmAGP
wd1l4X/jr1cgW76fh50mc7p7gK2TwB6Mmn5Pcgdo3xJFxcfLcDFFQP5t0OpPv9oLnDe2zLQtNmjLkdTI3dwx4iQnBkfBeYX6/2V2A3Y1fzOVR35NoDNIhsu7qH7XD76gpyI20cRA6K4EKvIUMFwaVRBJjT7j6uJn74X4MtcixBhTvHJKrLzTF2AXDcSDyEVz

View File

@ -4,3 +4,5 @@ FILTER = Register('滤波处理算法')
from .mean_filter import MeanFilter
from filter_collection.main import *
from .morphology_filter import MorphologyFilter
from .bilater_filter import BilaterFilter

View File

@ -1,4 +1,5 @@
from misc import AlgFrontend
from misc.utils import format_now
from osgeo import gdal, gdal_array
from skimage.filters import rank
from skimage.morphology import rectangle
@ -9,3 +10,105 @@ from rscder.utils.project import PairLayer, Project, RasterLayer, ResultPointLay
import os
from datetime import datetime
@FILTER.register
class BilaterFilter(AlgFrontend):
@staticmethod
def get_name():
return '双边滤波'
@staticmethod
def get_icon():
return None
@staticmethod
def get_widget(parent=None):
widget = QtWidgets.QWidget(parent)
filter_window_r = QtWidgets.QLineEdit(widget)
filter_window_r.setText('6')
filter_window_r.setValidator(QtGui.QIntValidator())
filter_window_r.setObjectName('filter_window_r')
sigma_color = QtWidgets.QLineEdit(widget)
sigma_color.setValidator(QtGui.QIntValidator())
sigma_color.setObjectName('sigma_color')
sigma_color.setText('50')
sigma_space = QtWidgets.QLineEdit(widget)
sigma_space.setValidator(QtGui.QIntValidator())
sigma_space.setObjectName('sigma_space')
sigma_space.setText('50')
filter_window_r_label = QtWidgets.QLabel(widget)
filter_window_r_label.setText('滤波窗口直径:')
sigma_space_label = QtWidgets.QLabel(widget)
sigma_space_label.setText('空间域方差:')
sigma_color_label = QtWidgets.QLabel(widget)
sigma_color_label.setText('像素域方差:')
hlayout1 = QtWidgets.QHBoxLayout()
hlayout1.addWidget(filter_window_r_label)
hlayout1.addWidget(filter_window_r)
hlayout1.addWidget(sigma_space_label)
hlayout1.addWidget(sigma_space)
hlayout1.addWidget(sigma_color_label)
hlayout1.addWidget(sigma_color)
# hlayout1.addWidget(x_size_input)
# hlayout1.addWidget(time_label)
# hlayout1.addWidget(y_size_input)
widget.setLayout(hlayout1)
return widget
@staticmethod
def get_params(widget:QtWidgets.QWidget=None):
if widget is None:
return dict(w=6, sigma_color=50, sigma_space=50)
def default(o, v=None):
if o is None:
return v
else:
return o.text()
w = int(default(widget.findChild(QtWidgets.QLineEdit, 'filter_window_r'), 6))
sigma_space = int(default(widget.findChild(QtWidgets.QLineEdit, 'sigma_space'), 50))
sigma_color = int(default(widget.findChild(QtWidgets.QLineEdit, 'sigma_color'), 50))
# y_input = widget.findChild(QtWidgets.QLineEdit, 'yinput')
return dict(w=w, sigma_space=sigma_space, sigma_color=sigma_color)
@staticmethod
def run_alg(pth, w, sigma_space, sigma_color, *args, **kargs):
# x_size = int(x_size)
# y_size = int(y_size)
# pth = layer.path
if pth is None:
return
ds = gdal.Open(pth)
band_count = ds.RasterCount
name = os.path.splitext(os.path.basename(pth))[0]
out_path = os.path.join(Project().other_path, '{}_bilater_filter_{}.tif'.format(name, format_now()))
out_ds = gdal.GetDriverByName('GTiff').Create(out_path, ds.RasterXSize, ds.RasterYSize, band_count, ds.GetRasterBand(1).DataType)
out_ds.SetProjection(ds.GetProjection())
out_ds.SetGeoTransform(ds.GetGeoTransform())
import cv2
for i in range(band_count):
band = ds.GetRasterBand(i+1)
data = band.ReadAsArray()
#进行双边滤波处理cv2.bilateralFilter(影像,滤波窗口直径0-255,像素域方差0-255,空间域方差0-255)
data=cv2.bilateralFilter(data,w,sigma_space,sigma_color)
out_band = out_ds.GetRasterBand(i+1)
out_band.WriteArray(data)
out_ds.FlushCache()
del out_ds
del ds
return out_path

View File

@ -82,15 +82,29 @@ class MainPlugin(BasicPlugin):
for key in FILTER.keys():
alg:AlgFrontend = FILTER[key]
name = alg.get_name() or key
action = QAction(alg.get_icon(), name, self.mainwindow)
action = QAction(name, self.mainwindow)
func = functools.partial(self.run, key)
action.triggered.connect(func)
toolbar.addAction(action)
ActionManager().filter_menu.addAction(action)
self.alg_ok.connect(self.alg_oked)
# def set_action(self):
# self.action = QAction(IconInstance().VECTOR, '均值滤波', self.mainwindow)
# self.action.triggered.connect(self.run)
# ActionManager().filter_menu.addAction(self.action)
# self.alg_ok.connect(self.alg_oked)
# self.action = QAction('均值滤波', self.mainwindow)
# # self.action.setCheckable)
# # self.action.setChecked(False)
# self.action.triggered.connect(self.run)
# ActionManager().filter_menu.addAction(self.action)
# self.alg_ok.connect(self.alg_oked)
# basic
def alg_oked(self, layer, outpath):
rlayer = RasterLayer(path = outpath, enable= True, view_mode = layer.view_mode )
layer.layer_parent.add_result_layer(rlayer)

View File

@ -0,0 +1,95 @@
from misc import AlgFrontend
from osgeo import gdal, gdal_array
from skimage.filters import rank
from skimage.morphology import rectangle
from filter_collection import FILTER
from PyQt5.QtWidgets import QDialog, QAction
from PyQt5 import QtCore, QtGui, QtWidgets
from rscder.utils.project import PairLayer, Project, RasterLayer, ResultPointLayer
import os
from datetime import datetime
@FILTER.register
class MorphologyFilter(AlgFrontend):
@staticmethod
def get_name():
return '形态学滤波'
@staticmethod
def get_icon():
return None
@staticmethod
def get_widget(parent=None):
widget = QtWidgets.QWidget(parent)
x_size_input = QtWidgets.QLineEdit(widget)
x_size_input.setText('3')
x_size_input.setValidator(QtGui.QIntValidator())
x_size_input.setObjectName('xinput')
y_size_input = QtWidgets.QLineEdit(widget)
y_size_input.setValidator(QtGui.QIntValidator())
y_size_input.setObjectName('yinput')
y_size_input.setText('3')
size_label = QtWidgets.QLabel(widget)
size_label.setText('窗口大小:')
time_label = QtWidgets.QLabel(widget)
time_label.setText('X')
hlayout1 = QtWidgets.QHBoxLayout()
hlayout1.addWidget(size_label)
hlayout1.addWidget(x_size_input)
hlayout1.addWidget(time_label)
hlayout1.addWidget(y_size_input)
widget.setLayout(hlayout1)
return widget
@staticmethod
def get_params(widget:QtWidgets.QWidget=None):
if widget is None:
return dict(x_size=3, y_size=3)
x_input = widget.findChild(QtWidgets.QLineEdit, 'xinput')
y_input = widget.findChild(QtWidgets.QLineEdit, 'yinput')
if x_input is None or y_input is None:
return dict(x_size=3, y_size=3)
x_size = int(x_input.text())
y_size = int(y_input.text())
return dict(x_size=x_size, y_size=y_size)
@staticmethod
def run_alg(pth, x_size, y_size, *args, **kargs):
x_size = int(x_size)
y_size = int(y_size)
# pth = layer.path
if pth is None:
return
ds = gdal.Open(pth)
band_count = ds.RasterCount
out_path = os.path.join(Project().other_path, 'morphology_filter_{}.tif'.format(int(datetime.now().timestamp() * 1000)))
out_ds = gdal.GetDriverByName('GTiff').Create(out_path, ds.RasterXSize, ds.RasterYSize, band_count, ds.GetRasterBand(1).DataType)
out_ds.SetProjection(ds.GetProjection())
out_ds.SetGeoTransform(ds.GetGeoTransform())
for i in range(band_count):
band = ds.GetRasterBand(i+1)
data = band.ReadAsArray()
#gradient形态学梯度计算
data=rank.gradient(data,rectangle(y_size,x_size))
out_band = out_ds.GetRasterBand(i+1)
out_band.WriteArray(data)
out_ds.FlushCache()
del out_ds
del ds
return out_path

View File

@ -55,6 +55,7 @@ class ActionManager(QtCore.QObject):
self.file_menu = menubar.addMenu( '&文件')
self.basic_menu = menubar.addMenu( '&基础工具')
self.filter_menu = self.basic_menu.addMenu(IconInstance().FILTER, '&滤波处理')
self.change_detection_menu = menubar.addMenu('&通用变化检测')
# self.unsupervised_menu = self.change_detection_menu.addMenu(IconInstance().UNSUPERVISED, '&无监督变化检测')
self.supervised_menu = self.change_detection_menu.addMenu(IconInstance().SUPERVISED,'&监督变化检测')