This commit is contained in:
蒋若愚 2022-11-11 13:24:05 +08:00
parent 80dc7781e3
commit 84d888dc07
10 changed files with 336 additions and 5 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

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

View File

@ -0,0 +1,135 @@
# 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
# import cv2 as cv
# import numpy as np
# cv.namedWindow("image")
# #d表示滤波窗口的直径
# #sigmaSpace表示空间域方差以及边缘处理方式
# #sigmaColor表示像素域方差
# cv.createTrackbar("d","image",0,255,print)
# cv.createTrackbar("sigmaColor","image",0,255,print)
# cv.createTrackbar("sigmaSpace","image",0,255,print)
# img = cv.imread("test-data/BBB.tif",0)
# while(1):
# d = cv.getTrackbarPos("d","image")
# sigmaColor = cv.getTrackbarPos("sigmaColor","image")
# sigmaSpace = cv.getTrackbarPos("sigmaSpace","image")
# result_img = cv.bilateralFilter(img,d,sigmaColor,sigmaSpace)
# cv.imshow("result",result_img)
# k = cv.waitKey(1) & 0xFF
# if k ==27:
# break
# cv.destroyAllWindows()
import os
import cv2
import numpy as np
def BilateralFilter_11(img_path='test-data/BBB.tif'):
img_src=cv2.imread(img_path)
img=cv2.resize(src=img_src,dsize=(1020,1020))
img=cv2.bilateralFilter(img,5,110,110)
cv2.imshow('img',img)
cv2.imshow('img_src',img_src)
cv2.waitKey(0)
cv2.destroyAllWindows()
# def detectBilateralFilter():
# cap=cv2.VideoCapture(0)
# while cap.isOpened():
# OK,frame=cap.read()
# img_src = cv2.imread(frame)
# img = cv2.resize(src=img_src, dsize=(450, 450))
# img = cv2.bilateralFilter(img, 10, 150, 150)
# cv2.imshow('img', img)
# if cv2.waitKey(1)&0XFF==27:
# break
# cap.release()
# cv2.destroyAllWindows()
if __name__ == '__main__':
print('Pycharm')
# BilateralFilter_11()
# detectBilateralFilter()
BilateralFilter_11()
#
# import numpy as np
# from scipy import signal
# import cv2
# import random
# import math
# #双边滤波
# def getClosenessWeight(sigma_g,H,W):
# r,c=np.mgrid[0:H:1,0:W:1]
# r -= (H - 1) // 2
# c -= int(W - 1) // 2
# closeWeight=np.exp(-0.5*(np.power(r,2)+np.power(c,2))/math.pow(sigma_g,2))
# return closeWeight
# def bfltGray(I,H,W,sigma_g,sigma_d):
# #构建空间距离权重模板
# closenessWeight=getClosenessWeight(sigma_g,H,W)
# #模板的中心点位置
# cH = (H - 1) // 2 #//表示整数除法
# cW = (W - 1) // 2
# #图像矩阵的行数和列数
# rows,cols=I.shape
# #双边滤波后的结果
# bfltGrayImage=np.zeros(I.shape,np.float32)
# for r in range(rows):
# for c in range(cols):
# pixel=I[r][c]
# #判断边界
# rTop=0 if r-cH<0 else r-cH
# rBottom=rows-1 if r+cH>rows-1 else r+cH
# cLeft=0 if c-cW<0 else c-cW
# cRight=cols-1 if c+cW>cols-1 else c+cW
# # 权重模板作用的区域
# region=I[rTop:rBottom+1,cLeft:cRight+1]
# #构建灰度值相似性的权重因子
# similarityWeightTemp=np.exp(-0.5*np.power(region-pixel,2.0)/math.pow(sigma_d,2))
# #similarityWeightTemp = np.exp(-0.5 * np.power(region - pixel, 2.0) / math.pow(sigma_d, 2))
# closenessWeightTemp=closenessWeight[rTop-r+cH:rBottom-r+cH+1,cLeft-c+cW:cRight-c+cW+1]
# #两个权重模板相乘
# weightTemp=similarityWeightTemp*closenessWeightTemp
# #归一化权重模板
# weightTemp=weightTemp/np.sum(weightTemp)
# #权重模板和对应的领域值相乘求和
# bfltGrayImage[r][c]=np.sum(region*weightTemp)
# return bfltGrayImage
# if __name__=='__main__': ##启动语句
# a= cv2.imread('test-data/BBB.tif', cv2.IMREAD_UNCHANGED) # 路径名中不能有中文,会出错,cv2.
# image1 = cv2.split(a)[0]#蓝通道
# cv2.imshow("image1",image1)
# image1=image1/255.0
# #双边滤波
# bfltImage=bfltGray(image1,3,3,19,0.2)
# cv2.imshow("增强后图",bfltImage)
# cv2.waitKey(0)
# cv2.destroyAllWindows()

View File

@ -9,3 +9,89 @@ 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)
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, 'bilater_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())
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,6,50,50)
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,12 +82,18 @@ 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)
# 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)
@ -96,6 +102,8 @@ class MainPlugin(BasicPlugin):
# ActionManager().filter_menu.addAction(self.action)
# self.alg_ok.connect(self.alg_oked)
# basic
def alg_oked(self, parent, layer:RasterLayer):
parent.add_result_layer(layer)

View File

@ -0,0 +1,99 @@
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

@ -54,6 +54,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,'&监督变化检测')

BIN
test-data/AAA.tif.ovr Normal file

Binary file not shown.

BIN
test-data/BBB.tif.ovr Normal file

Binary file not shown.