项目提交

master
许伟豪 2 years ago
parent 3594bb1c18
commit 5229230b95

@ -0,0 +1,2 @@
# Default ignored files
/workspace.xml

@ -0,0 +1,6 @@
<component name="InspectionProjectProfileManager">
<settings>
<option name="USE_PROJECT_PROFILE" value="false" />
<version value="1.0" />
</settings>
</component>

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="JavaScriptSettings">
<option name="languageLevel" value="ES6" />
</component>
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.7 (Digital_Image_Process-master)" project-jdk-type="Python SDK" />
</project>

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/resource.iml" filepath="$PROJECT_DIR$/.idea/resource.iml" />
</modules>
</component>
</project>

@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="PYTHON_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$">
<excludeFolder url="file://$MODULE_DIR$/venv" />
</content>
<orderEntry type="jdk" jdkName="Python 3.7 (Digital_Image_Process-master)" jdkType="Python SDK" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
<component name="TestRunnerService">
<option name="PROJECT_TEST_RUNNER" value="Unittests" />
</component>
</module>

@ -0,0 +1,13 @@
### 运行环境
- python-opencv4.2
- pyqt5
- matplotlib3.2.1
- numpy1.18.4
- dlib
### 使用说明
> 配置好相关环境直接运行main.py就ok

@ -0,0 +1,35 @@
from custom.tableWidget import *
from custom.listWidgetItems import *
# Implemented functions
items = [
GeometricTransItem,
GrayingItem,
EqualizeItem,
FilterItem,
SharpenItem,
AddNoiseItem,
FrequencyFilterItem,
SelectFilterItem,
ColorImageProcessItem,
AffineItem,
BeautyItem,
IdCardPicGenerateItem,
]
tables = [
GeometricTransTableWight,
GrayingTableWidget,
EqualizeTableWidget,
FilterTabledWidget,
SharpenItemTableWidget,
AddNoiseItemTableWidget,
FrequencyFilterTabledWidget,
SelectFilterTabledWidget,
ColorImageProcessTabledWidget,
LineTableWidget,
BeautyTableWight,
IdCardPicGenerateTabledWidget,
]

@ -0,0 +1,15 @@
from cv2 import selectROI,imwrite
from PyQt5.QtWidgets import QMainWindow, QFileDialog
class childwindow1(QMainWindow):
def __init__(self,parent=None):
super(childwindow1, self).__init__(parent)
def openfile(self):
fname, _ = QFileDialog.getOpenFileName(self, 'Open file', '.', 'Image Files(*.jpg *.bmp *.png *.jpeg *.rgb *.tif)')
return fname
def selectROI(self,img):
bbox = selectROI(img, False)
cut = img[bbox[1]:bbox[1] + bbox[3], bbox[0]:bbox[0] + bbox[2]]
imwrite('cut.jpg', cut)

@ -0,0 +1,78 @@
from PyQt5.QtCore import Qt, QRectF
from PyQt5.QtGui import QCursor, QImage, QPixmap
from PyQt5.QtWidgets import QGraphicsView, QGraphicsPixmapItem, QGraphicsScene, QMenu, QAction, QFileDialog
from cv2 import cvtColor,COLOR_BGR2RGB
class GraphicsView(QGraphicsView):
def __init__(self, parent=None):
super(GraphicsView, self).__init__(parent=parent)
self._zoom = 0
self._empty = True
self._photo = QGraphicsPixmapItem()
self._scene = QGraphicsScene(self)
self._scene.addItem(self._photo)
self.setScene(self._scene)
# self.setScene(self._scene1)
self.setAlignment(Qt.AlignCenter) # 居中显示
self.setDragMode(QGraphicsView.ScrollHandDrag) # 设置拖动
self.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
self.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
self.setMinimumSize(640, 480)
def contextMenuEvent(self, event):
if not self.has_photo():
return
menu = QMenu()
save_action = QAction('另存为', self)
save_action.triggered.connect(self.save_current) # 传递额外值
menu.addAction(save_action)
menu.exec(QCursor.pos())
def save_current(self):
file_name = QFileDialog.getSaveFileName(self, '另存为', './', 'Image files(*.jpg *.gif *.png)')[0]
print(file_name)
if file_name:
self._photo.pixmap().save(file_name)
def get_image(self):
if self.has_photo():
return self._photo.pixmap().toImage()
def has_photo(self):
return not self._empty
def change_image(self, img):
self.update_image(img)
self.fitInView()
def img_to_pixmap(self, img):
img = cvtColor(img, COLOR_BGR2RGB) # bgr -> rgb
h, w, c = img.shape # 获取图片形状
image = QImage(img, w, h, 3 * w, QImage.Format_RGB888)
return QPixmap.fromImage(image)
def update_image(self, img):
self._empty = False
self._photo.setPixmap(self.img_to_pixmap(img))
def fitInView(self, scale=True):
rect = QRectF(self._photo.pixmap().rect())
if not rect.isNull():
self.setSceneRect(rect)
def wheelEvent(self, event):
if self.has_photo():
if event.angleDelta().y() > 0:
factor = 1.25
self._zoom += 1
else:
factor = 0.8
self._zoom -= 1
if self._zoom > 0:
self.scale(factor, factor)
elif self._zoom == 0:
self.fitInView()
else:
self._zoom = 0

@ -0,0 +1,276 @@
from PyQt5.QtWidgets import QListWidgetItem
from flags import *
# 功能区引用
# 几何变换
from function.GeometricTrans.Ratate import ratate
from function.GeometricTrans.Mirror import mirror1,mirror2,mirror3
from function.GeometricTrans.LargeSmall import largeSmall
# 灰度变换
from function.GrayscaleTrans.Reverse import image_reverse
from function.GrayscaleTrans.GammaTrans import gammaTranform
from function.GrayscaleTrans.Binarization import binarization
from function.GrayscaleTrans.BGR2GRAY import rgbToGray
# 直方图处理
from function.HistogramTrans.Equalize import hist_equal
from function.HistogramTrans.HistogramMatch import HisgramMatch
# 平滑处理
from function.SmoothingTrans.GaussianFilter import gaussian_filter
from function.SmoothingTrans.MeanFilter import mean_filter
from function.SmoothingTrans.MedianFilter import median_filter
# 锐化处理
from function.SharpenTrans.SobelFilter import sobel_filter
from function.SharpenTrans.Robert import robert
from function.SharpenTrans.Laplacian import laplacian_filter
from function.SharpenTrans.Prewitt import prewitt_filter
from function.SharpenTrans.Canny import CannyFilter
from function.SharpenTrans.LoG import LogFilter
# 加性噪声
from function.AddNoise.GasussNoise import gasuss_noise
from function.AddNoise.ImpluseNoise import impluse_noise
from function.AddNoise.RayleighNoise import rayleigh_noise
from function.AddNoise.GammaNoise import gamma_noise
from function.AddNoise.UniformNoise import uniform_noise
from function.AddNoise.ExponentialNoise import exponential_noise
# 频域滤波 | 选择滤波
from function.FrequencyDomainFilter.ButterWorthFilter import butterworthSelectFilter, butterworthFilter
from function.FrequencyDomainFilter.IdealFilter import idealFilter,idealSelectFilter,idealNotchFilter
from function.FrequencyDomainFilter.GaussianFrequencyFilter import GaussianFilter,GaussianSelectFilter
# 彩色图像处理
from function.ColorImageProcess.HSIProcess import hsvProcess, rgb2hsi
from function.ColorImageProcess.RGB2CMY import rgb2cmy
from function.ColorImageProcess.PseudoColorTrans import pseudoColorTrans
#线条变换和图片修复
from function.LineChange.AffineTransformation import Affine
from function.LineChange.LineCheck import LineCheck
from function.LineChange.ImgeFix import ImgeFix
# 证件照生成
from function.IdCardPicGenerate.IdCardPicGenerate import idCardPicGenerate
# 美颜功能
from function.Beauty.Beauty import Beauty
class MyItem(QListWidgetItem):
def __init__(self, name=None, parent=None):
super(MyItem, self).__init__(name, parent=parent)
def get_params(self):
protected = [v for v in dir(self) if v.startswith('_') and not v.startswith('__')]
param = {}
for v in protected:
param[v.replace('_', '', 1)] = self.__getattribute__(v)
return param
def update_params(self, param):
for k, v in param.items():
if '_' + k in dir(self):
self.__setattr__('_' + k, v)
class GeometricTransItem(MyItem):
def __init__(self, parent=None):
super(GeometricTransItem, self).__init__(' 几何变换 ', parent=parent)
self._kind = 0
self._rate = 100
def __call__(self, img):
if self._kind == 0:
img = largeSmall(img,self._rate)
elif self._kind == 1:
img = mirror1(img,self._rate)
elif self._kind == 2:
img = mirror2(img,self._rate)
elif self._kind == 3:
img = mirror3(img,self._rate)
elif self._kind == 4:
img = ratate(img,self._rate)
return img
class GrayingItem(MyItem):
def __init__(self, parent=None):
super(GrayingItem, self).__init__(' 灰度变换 ', parent=parent)
self._kind = RBG2GRAY
self._c_value = 1
self._γ_value = 3.0
def __call__(self, img):
if self._kind == 0:
img = rgbToGray(img)
elif self._kind == 1:
img = image_reverse(img)
elif self._kind == 2:
img = binarization(img)
elif self._kind == 3:
img = gammaTranform(self._c_value,self._γ_value,img)
return img
class EqualizeItem(MyItem):
def __init__(self, parent=None):
super().__init__(' 直方图处理 ', parent=parent)
self._kind = 0
def __call__(self, img):
if self._kind == 0:
img = hist_equal(img)
elif self._kind == 1:
img = HisgramMatch(img)
return img
class AffineItem(MyItem):
def __init__(self, parent=None):
super().__init__(' 变换 ', parent=parent)
self._kind = 0
def __call__(self, img):
if self._kind == 0:
img = Affine(img)
elif self._kind == 1:
img = LineCheck(img)
elif self._kind == 2:
img = ImgeFix(img)
return img
class BeautyItem(MyItem):
def __init__(self, parent=None):
super().__init__('美颜功能', parent=parent)
self._kind = 0
def __call__(self, img):
img = Beauty(img)
return img
class FilterItem(MyItem):
def __init__(self, parent=None):
super().__init__('平滑处理', parent=parent)
self._ksize = 3
self._kind = 0
self._sigma = 1
def __call__(self, img):
if self._kind == 0:
img = cv2.blur(img, (self._ksize, self._ksize))
# img = mean_filter(img,self._ksize)
elif self._kind == 1:
# cv2实现的中值滤波
img = cv2.medianBlur(img, self._ksize)
# python实现的中值滤波
# img = median_filter(img, self._ksize)
elif self._kind == 2:
# img = cv2.GaussianBlur(img, (self._ksize, self._ksize), self._sigma)
img = gaussian_filter(img,self._ksize,self._sigma)
return img
class SharpenItem(MyItem):
def __init__(self, parent=None):
super().__init__('锐化处理', parent=parent)
self._kind = 0
def __call__(self, img):
if self._kind == 0:
# python实现
img = sobel_filter(img)
# cv2实现
# img = cv2_sobel(img)
elif self._kind == 1:
img = robert(img)
# cv2实现
# img = cv2_robert(img)
elif self._kind == 2:
img = prewitt_filter(img)
elif self._kind == 3:
img = laplacian_filter(img)
elif self._kind == 4:
img = CannyFilter(img)
elif self._kind == 5:
img = LogFilter(img)
return img
class AddNoiseItem(MyItem):
def __init__(self, parent=None):
super().__init__('加性噪声', parent=parent)
self._kind = 0
self._scale = 0.1
def __call__(self, img):
if self._kind == 0:
img = gasuss_noise(img,self._scale)
elif self._kind == 1:
img = rayleigh_noise(img,self._scale)
elif self._kind == 2:
img = gamma_noise(img,self._scale)
elif self._kind == 3:
img = uniform_noise(img,self._scale)
elif self._kind == 4:
img = impluse_noise(img,self._scale)
elif self._kind == 5:
img = exponential_noise(img,self._scale)
return img
class FrequencyFilterItem(MyItem):
def __init__(self, parent=None):
super().__init__('频域滤波', parent=parent)
self._kind = 0
self._scale = 30
self._n = 1
def __call__(self, img):
if self._kind == 0 or self._kind == 3:
img = idealFilter(img,self._scale,self._kind)
elif self._kind == 1 or self._kind == 4:
img = butterworthFilter(img,self._scale,self._n,self._kind)
elif self._kind == 2 or self._kind == 5:
img = GaussianFilter(img,self._scale,self._kind)
return img
class SelectFilterItem(MyItem):
def __init__(self, parent=None):
super().__init__('选择滤波', parent=parent)
self._kind = 0
self._scale = 30
self._n = 1
self._W = 10
def __call__(self, img):
if self._kind == 0 or self._kind == 3:
img = idealSelectFilter(img,self._scale,self._W,self._kind)
elif self._kind == 1 or self._kind == 4:
img = butterworthSelectFilter(img,self._scale,self._n,self._W,self._kind)
elif self._kind == 2 or self._kind == 5:
img = GaussianSelectFilter(img,self._scale,self._W,self._kind)
elif self._kind == 6 or self._kind == 7:
img = idealNotchFilter(img,self._scale,self._kind)
return img
class ColorImageProcessItem(MyItem):
def __init__(self, parent=None):
super().__init__('彩色图像处理', parent=parent)
self._kind = 0
self._H = 100
self._S = 100
self._V = 100
self._color_kind = 0
def __call__(self, img):
if self._kind == 0:
img = hsvProcess(img,self._H,self._S,self._V)
elif self._kind == 1:
img = rgb2cmy(img,self._H,self._S,self._V)
elif self._kind == 2:
img = rgb2hsi(img,self._H,self._S,self._V)
elif self._kind == 3:
img = pseudoColorTrans(img,self._H,self._S,self._V,self._color_kind)
return img
class IdCardPicGenerateItem(MyItem):
def __init__(self, parent=None):
super().__init__('人像背景切换', parent=parent)
self._kind = 0
self._H = 100
self._S = 100
self._V = 100
def __call__(self, img):
img = idCardPicGenerate(img, self._kind)
return img

@ -0,0 +1,86 @@
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QCursor
from PyQt5.QtWidgets import QListWidget, QListView, QAbstractItemView, QAction, QMenu
from config import items
class MyListWidget(QListWidget):
def __init__(self, parent=None):
super().__init__(parent=parent)
self.mainwindow = parent
self.setDragEnabled(True)
# 选中不显示虚线
# self.setEditTriggers(QAbstractItemView.NoEditTriggers)
self.setFocusPolicy(Qt.NoFocus)
class UsedListWidget(MyListWidget):
def __init__(self, parent=None):
super().__init__(parent=parent)
self.setAcceptDrops(True) # 激活组件的拖拽事件
self.setFlow(QListView.TopToBottom) # 设置列表方向(表示数据项从上至下排列)
self.setDefaultDropAction(Qt.MoveAction) # 设置拖放为移动而不是复制一个
self.setDragDropMode(QAbstractItemView.InternalMove) # 设置拖放模式, 内部拖放
self.itemClicked.connect(self.show_attr)
self.setMinimumWidth(200)
self.move_item = None
def contextMenuEvent(self, e):
# 右键菜单事件
item = self.itemAt(self.mapFromGlobal(QCursor.pos()))
if not item: return # 判断是否是空白区域
menu = QMenu()
delete_action = QAction('删除', self)
delete_action.triggered.connect(lambda: self.delete_item(item)) # 传递额外值
menu.addAction(delete_action)
menu.exec(QCursor.pos())
def delete_item(self, item):
# 删除操作
self.takeItem(self.row(item))
self.mainwindow.update_image() # 更新frame
self.mainwindow.dock_attr.close()
def dropEvent(self, event):
super().dropEvent(event)
self.mainwindow.update_image()
def show_attr(self):
item = self.itemAt(self.mapFromGlobal(QCursor.pos()))
if not item: return
param = item.get_params() # 获取当前item的属性
if type(item) in items:
index = items.index(type(item)) # 获取item对应的table索引
self.mainwindow.stackedWidget.setCurrentIndex(index)
self.mainwindow.stackedWidget.currentWidget().update_params(param) # 更新对应的table
self.mainwindow.dock_attr.show()
class FuncListWidget(MyListWidget):
def __init__(self, parent=None):
super().__init__(parent=parent)
self.setFixedHeight(64)
self.setFlow(QListView.LeftToRight) # 设置列表方向
self.setViewMode(QListView.IconMode) # 设置列表模式
# self.setViewMode(QListView.ViewMode)
self.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff) # 关掉滑动条
self.setAcceptDrops(False)
for itemType in items:
self.addItem(itemType())
self.itemClicked.connect(self.add_used_function)
def add_used_function(self):
func_item = self.currentItem()
if type(func_item) in items:
use_item = type(func_item)()
self.mainwindow.useListWidget.addItem(use_item)
self.mainwindow.update_image()
def enterEvent(self, event):
self.setCursor(Qt.PointingHandCursor)
def leaveEvent(self, event):
self.setCursor(Qt.ArrowCursor)
self.setCurrentRow(-1) # 取消选中状态

@ -0,0 +1,11 @@
from PyQt5.QtWidgets import QStackedWidget
from config import tables
class StackedWidget(QStackedWidget):
def __init__(self, parent):
super().__init__(parent=parent)
for table in tables:
self.addWidget(table(parent=parent))
self.setMinimumWidth(200)

@ -0,0 +1,385 @@
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QTableWidget, QAbstractItemView, QHeaderView, QSpinBox, QDoubleSpinBox, QCheckBox, \
QComboBox, QWidget, QTableWidgetItem, QSlider, QLabel
class TableWidget(QTableWidget):
def __init__(self, parent=None):
super(TableWidget, self).__init__(parent=parent)
self.mainwindow = parent
self.setShowGrid(True) # 显示网格
self.setAlternatingRowColors(True) # 隔行显示颜色
self.setEditTriggers(QAbstractItemView.NoEditTriggers)
self.horizontalHeader().setVisible(False)
self.verticalHeader().setVisible(False)
self.horizontalHeader().sectionResizeMode(QHeaderView.Stretch)
self.verticalHeader().sectionResizeMode(QHeaderView.Stretch)
self.horizontalHeader().setStretchLastSection(True)
self.setFocusPolicy(Qt.NoFocus)
def signal_connect(self):
for spinbox in self.findChildren(QSpinBox):
spinbox.valueChanged.connect(self.update_item)
for doublespinbox in self.findChildren(QDoubleSpinBox):
doublespinbox.valueChanged.connect(self.update_item)
for combox in self.findChildren(QComboBox):
combox.currentIndexChanged.connect(self.update_item)
for checkbox in self.findChildren(QCheckBox):
checkbox.stateChanged.connect(self.update_item)
for qslider in self.findChildren(QSlider):
qslider.valueChanged.connect(self.update_item)
def update_item(self):
param = self.get_params()
self.mainwindow.useListWidget.currentItem().update_params(param)
self.mainwindow.update_image()
def update_params(self, param=None):
for key in param.keys():
box = self.findChild(QWidget, name=key)
if isinstance(box, QSpinBox) or isinstance(box, QDoubleSpinBox):
box.setValue(param[key])
elif isinstance(box, QComboBox):
box.setCurrentIndex(param[key])
elif isinstance(box, QCheckBox):
box.setChecked(param[key])
elif isinstance(box, QSlider):
box.setValue(param[key])
def get_params(self):
param = {}
for spinbox in self.findChildren(QSpinBox):
param[spinbox.objectName()] = spinbox.value()
for doublespinbox in self.findChildren(QDoubleSpinBox):
param[doublespinbox.objectName()] = doublespinbox.value()
for combox in self.findChildren(QComboBox):
param[combox.objectName()] = combox.currentIndex()
for combox in self.findChildren(QCheckBox):
param[combox.objectName()] = combox.isChecked()
for qslider in self.findChildren(QSlider):
param[qslider.objectName()] = qslider.value()
return param
class GeometricTransTableWight(TableWidget):
def __init__(self, parent=None):
super(GeometricTransTableWight, self).__init__(parent=parent)
self.kind_comBox = QComboBox()
self.kind_comBox.addItems(['放大|缩小','图像水平镜像','图像垂直镜像','图像对角镜像','图像旋转'])
self.kind_comBox.setObjectName('kind')
self.setColumnCount(2)
self.setRowCount(2)
self.rate_value_spinBox = QSpinBox()
self.rate_value_spinBox.setObjectName('rate')
self.rate_value_spinBox.setRange(0,360)
self.rate_value_spinBox.setSingleStep(10)
# self.rate_value_spinBox.setSuffix("%")
self.setItem(0, 0, QTableWidgetItem('类型'))
self.setCellWidget(0, 1, self.kind_comBox)
self.setItem(1, 0, QTableWidgetItem('比例%|角度°'))
self.setCellWidget(1, 1, self.rate_value_spinBox)
self.signal_connect()
class BeautyTableWight(TableWidget):
def __init__(self, parent=None):
super(BeautyTableWight, self).__init__(parent=parent)
self.kind_comBox = QComboBox()
self.kind_comBox.addItems(['美颜功能'])
self.kind_comBox.setObjectName('kind')
self.setColumnCount(1)
self.setRowCount(1)
self.setCellWidget(0, 1, self.kind_comBox)
self.signal_connect()
class GrayingTableWidget(TableWidget):
def __init__(self, parent=None):
super(GrayingTableWidget, self).__init__(parent=parent)
self.kind_comBox = QComboBox()
self.kind_comBox.addItems(['灰度化', '图像反转', '二值化','幂律变换'])
self.kind_comBox.setObjectName('kind')
self.c_value_spinBox = QSpinBox()
self.c_value_spinBox.setObjectName('c_value')
self.c_value_spinBox.setMinimum(1)
self.c_value_spinBox.setSingleStep(1)
self.γ_value_spinBox = QSpinBox()
self.γ_value_spinBox.setObjectName('γ_value')
self.γ_value_spinBox.setMinimum(1)
self.γ_value_spinBox.setSingleStep(1)
self.setColumnCount(2)
self.setRowCount(3)
self.setItem(0, 0, QTableWidgetItem('类型'))
self.setCellWidget(0, 1, self.kind_comBox)
self.setItem(1, 0, QTableWidgetItem('c值(幂律变换)'))
self.setCellWidget(1, 1, self.c_value_spinBox)
self.setItem(2, 0, QTableWidgetItem('γ值(幂律变换)'))
self.setCellWidget(2, 1, self.γ_value_spinBox)
self.signal_connect()
class EqualizeTableWidget(TableWidget):
def __init__(self, parent=None):
super(EqualizeTableWidget, self).__init__(parent=parent)
self.kind_comBox = QComboBox()
self.kind_comBox.addItems(['直方图均衡化', '直方图匹配'])
self.kind_comBox.setObjectName('kind')
self.setColumnCount(2)
self.setRowCount(1)
self.setItem(0, 0, QTableWidgetItem('类型'))
self.setCellWidget(0, 1, self.kind_comBox)
self.signal_connect()
class LineTableWidget(TableWidget):
def __init__(self, parent=None):
super(LineTableWidget, self).__init__(parent=parent)
self.kind_comBox = QComboBox()
self.kind_comBox.addItems(['仿射变换', '线条变化检测','图像修复'])
self.kind_comBox.setObjectName('kind')
self.setColumnCount(2)
self.setRowCount(1)
self.setItem(0, 0, QTableWidgetItem('类型'))
self.setCellWidget(0, 1, self.kind_comBox)
self.signal_connect()
class EqualizeTableWidget(TableWidget):
def __init__(self, parent=None):
super(EqualizeTableWidget, self).__init__(parent=parent)
self.kind_comBox = QComboBox()
self.kind_comBox.addItems(['直方图均衡化', '直方图匹配'])
self.kind_comBox.setObjectName('kind')
self.setColumnCount(2)
self.setRowCount(1)
self.setItem(0, 0, QTableWidgetItem('类型'))
self.setCellWidget(0, 1, self.kind_comBox)
self.signal_connect()
class FilterTabledWidget(TableWidget):
def __init__(self, parent=None):
super(FilterTabledWidget, self).__init__(parent=parent)
self.kind_comBox = QComboBox()
self.kind_comBox.addItems(['均值滤波', '中值滤波','高斯滤波'])
self.kind_comBox.setObjectName('kind')
self.ksize_spinBox = QSpinBox()
self.ksize_spinBox.setObjectName('ksize')
self.ksize_spinBox.setMinimum(1)
self.ksize_spinBox.setSingleStep(2)
self.sigma_DoubleBox = QDoubleSpinBox()
self.sigma_DoubleBox.setObjectName('sigma')
self.sigma_DoubleBox.setMinimum(1)
self.sigma_DoubleBox.setSingleStep(1)
self.setColumnCount(2)
self.setRowCount(3)
self.setItem(0, 0, QTableWidgetItem('类型'))
self.setCellWidget(0, 1, self.kind_comBox)
self.setItem(1, 0, QTableWidgetItem('size'))
self.setCellWidget(1, 1, self.ksize_spinBox)
self.setItem(2, 0, QTableWidgetItem('sigma(高斯滤波)'))
self.setCellWidget(2, 1, self.sigma_DoubleBox)
self.signal_connect()
class SharpenItemTableWidget(TableWidget):
def __init__(self, parent=None):
super(SharpenItemTableWidget, self).__init__(parent=parent)
self.kind_comBox = QComboBox()
self.kind_comBox.addItems(['sobel算子','robert算子','prewitt算子','laplacian算子','Canny算子','LoG边缘检测算子'])
self.kind_comBox.setObjectName('kind')
self.setColumnCount(2)
self.setRowCount(1)
self.setItem(0, 0, QTableWidgetItem('类型'))
self.setCellWidget(0, 1, self.kind_comBox)
self.signal_connect()
class AddNoiseItemTableWidget(TableWidget):
def __init__(self, parent=None):
super(AddNoiseItemTableWidget, self).__init__(parent=parent)
self.kind_comBox = QComboBox()
self.kind_comBox.addItems(['高斯噪声','瑞利噪声','伽马噪声','均匀噪声','椒盐噪声','指数噪声'])
self.kind_comBox.setObjectName('kind')
self.ksize_DoubleBox = QDoubleSpinBox()
self.ksize_DoubleBox.setMinimum(0)
self.ksize_DoubleBox.setSingleStep(0.1)
self.ksize_DoubleBox.setObjectName('scale')
self.setColumnCount(2)
self.setRowCount(2)
self.setItem(0, 0, QTableWidgetItem('噪声类型'))
self.setCellWidget(0, 1, self.kind_comBox)
self.setItem(1, 0, QTableWidgetItem('噪声比例'))
self.setCellWidget(1, 1, self.ksize_DoubleBox)
self.signal_connect()
class FrequencyFilterTabledWidget(TableWidget):
def __init__(self, parent=None):
super(FrequencyFilterTabledWidget, self).__init__(parent=parent)
self.kind_comBox = QComboBox()
self.kind_comBox.addItems(['理想低通滤波','巴特沃思低通滤波','高斯低通滤波','理想高通滤波','巴特沃思高通滤波','高斯高通滤波'])
self.kind_comBox.setObjectName('kind')
self.ksize_SpinBox = QSpinBox()
self.ksize_SpinBox.setMinimum(10)
self.ksize_SpinBox.setSingleStep(10)
self.ksize_SpinBox.setObjectName('scale')
self.n_Spinbox = QSpinBox()
self.n_Spinbox.setMinimum(1)
self.n_Spinbox.setSingleStep(1)
self.n_Spinbox.setObjectName('n')
self.setColumnCount(2)
self.setRowCount(3)
self.setItem(0, 0, QTableWidgetItem('滤波类型'))
self.setCellWidget(0, 1, self.kind_comBox)
self.setItem(1, 0, QTableWidgetItem('半径r'))
self.setCellWidget(1, 1, self.ksize_SpinBox)
self.setItem(2,0,QTableWidgetItem('阶数n'))
self.setCellWidget(2,1,self.n_Spinbox)
self.signal_connect()
class SelectFilterTabledWidget(TableWidget):
def __init__(self, parent=None):
super(SelectFilterTabledWidget, self).__init__(parent=parent)
self.kind_comBox = QComboBox()
self.kind_comBox.addItems(['理想带阻滤波', '巴特沃思带阻滤波', '高斯带阻滤波', '理想带通滤波', '巴特沃思带通滤波', '高斯带通滤波','理想带阻陷波','理想带通陷波'])
self.kind_comBox.setObjectName('kind')
self.ksize_SpinBox = QSpinBox()
self.ksize_SpinBox.setMinimum(0)
self.ksize_SpinBox.setMaximum(300)
self.ksize_SpinBox.setSingleStep(10)
self.ksize_SpinBox.setObjectName('scale')
self.W_Spinbox = QSpinBox()
self.W_Spinbox.setMinimum(0)
self.W_Spinbox.setMaximum(500)
self.W_Spinbox.setSingleStep(10)
self.W_Spinbox.setObjectName('W')
self.n_Spinbox = QSpinBox()
self.n_Spinbox.setMinimum(1)
self.n_Spinbox.setSingleStep(1)
self.n_Spinbox.setObjectName('n')
self.setColumnCount(2)
self.setRowCount(4)
self.setItem(0, 0, QTableWidgetItem('滤波类型'))
self.setCellWidget(0, 1, self.kind_comBox)
self.setItem(1, 0, QTableWidgetItem('半径r'))
self.setCellWidget(1, 1, self.ksize_SpinBox)
self.setItem(2, 0, QTableWidgetItem('带宽W'))
self.setCellWidget(2, 1, self.W_Spinbox)
self.setItem(3, 0, QTableWidgetItem('阶数n'))
self.setCellWidget(3, 1, self.n_Spinbox)
self.signal_connect()
class ColorImageProcessTabledWidget(TableWidget):
def __init__(self, parent=None):
super(ColorImageProcessTabledWidget, self).__init__(parent=parent)
self.kind_comBox = QComboBox()
self.kind_comBox.addItems(['RGB模型', 'CMY模型','HSI模型','伪彩色变换'])
# self.kind_comBox.addItems(['彩色模型', '伪彩色变换', '真彩色变换'])
self.kind_comBox.setObjectName('kind')
# self.H_qslider = QSlider(Qt.Horizontal)
self.H_qslider = MyQSlider()
self.H_qslider.setOrientation(Qt.Horizontal)
self.H_qslider.setMinimum(0)
self.H_qslider.setMaximum(150)
self.H_qslider.setSingleStep(10)
self.H_qslider.setTickPosition(QSlider.TicksBelow)
self.H_qslider.setTickInterval(10)
self.H_qslider.setObjectName('H')
# self.S_qslider = QSlider(Qt.Horizontal)
self.S_qslider = MyQSlider()
self.S_qslider.setOrientation(Qt.Horizontal)
self.S_qslider.setMinimum(0)
self.S_qslider.setMaximum(150)
self.S_qslider.setSingleStep(10)
self.S_qslider.setTickPosition(QSlider.TicksBelow)
self.S_qslider.setTickInterval(10)
self.S_qslider.setObjectName('S')
# self.V_qslider = QSlider(Qt.Horizontal)
self.V_qslider = MyQSlider()
self.V_qslider.setOrientation(Qt.Horizontal)
self.V_qslider.setMinimum(0)
self.V_qslider.setMaximum(150)
self.V_qslider.setSingleStep(10)
self.V_qslider.setTickPosition(QSlider.TicksBelow)
self.V_qslider.setTickInterval(10)
self.V_qslider.setObjectName('V')
self.pseudoColor_comBox = QComboBox()
self.pseudoColor_comBox.addItems(['COLORMAP_AUTUMN', 'COLORMAP_BONE', 'COLORMAP_JET', 'COLORMAP_WINTER', 'COLORMAP_RAINBOW','COLORMAP_OCEAN','COLORMAP_SUMMER','COLORMAP_SPRING','COLORMAP_COOL','COLORMAP_HSV','COLORMAP_PINK','COLORMAP_HOT'])
# self.kind_comBox.addItems(['彩色模型', '伪彩色变换', '真彩色变换'])
self.pseudoColor_comBox.setObjectName('color_kind')
self.setColumnCount(2)
self.setRowCount(5)
self.setItem(0, 0, QTableWidgetItem('颜色变换'))
self.setCellWidget(0, 1, self.kind_comBox)
self.setItem(1, 0, QTableWidgetItem('B通道'))
self.setCellWidget(1, 1, self.H_qslider)
self.setItem(2, 0, QTableWidgetItem('G通道'))
self.setCellWidget(2, 1, self.S_qslider)
self.setItem(3, 0, QTableWidgetItem('R通道'))
self.setCellWidget(3, 1, self.V_qslider)
self.setItem(4, 0, QTableWidgetItem('伪彩色类型'))
self.setCellWidget(4, 1, self.pseudoColor_comBox)
self.signal_connect()
class IdCardPicGenerateTabledWidget(TableWidget):
def __init__(self, parent=None):
super(IdCardPicGenerateTabledWidget, self).__init__(parent=parent)
self.kind_comBox = QComboBox()
self.kind_comBox.addItems(['蓝底', '红底','白底'])
self.kind_comBox.setObjectName('kind')
self.setColumnCount(2)
self.setRowCount(1)
self.setItem(0, 0, QTableWidgetItem('人像背景底色切换'))
self.setCellWidget(0, 1, self.kind_comBox)
self.signal_connect()
class MyQSlider(QSlider):
def __init__(self,parent=None,*args,**kwargs):
super().__init__(parent,*args,**kwargs)
label = QLabel(self)
self.label = label
label.setText('100')
label.setStyleSheet('background-color:cyan;color:red')
label.hide()
def mousePressEvent(self, evt):
super().mousePressEvent(evt)
y = (1-((self.value()-self.minimum())/(self.maximum()-self.minimum())))*(self.height()-self.label.height())
x = (self.width()-self.label.width())/2
self.label.move(x,y)
self.label.show()
self.label.setText(str(self.value()))
def mouseMoveEvent(self, evt):
super().mouseMoveEvent(evt)
y = (1-((self.value()-self.minimum())/(self.maximum()-self.minimum())))*(self.height()-self.label.height())
x = (self.width()-self.label.width())/2
self.label.move(x,y)
self.label.setText(str(self.value()))
self.label.adjustSize()
def mouseReleaseEvent(self, evt):
super().mouseReleaseEvent(evt)
self.label.hide()

@ -0,0 +1,33 @@
from cv2 import imdecode
import numpy as np
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QTreeView, QDockWidget, QFileSystemModel
class FileSystemTreeView(QTreeView, QDockWidget):
def __init__(self, parent=None):
super().__init__(parent=parent)
self.mainwindow = parent
self.fileSystemModel = QFileSystemModel()
self.fileSystemModel.setRootPath('.')
self.setModel(self.fileSystemModel)
# 隐藏size,date等列
self.setColumnWidth(0, 200)
self.setColumnHidden(1, True)
self.setColumnHidden(2, True)
self.setColumnHidden(3, True)
# 不显示标题栏
self.header().hide()
# 设置动画
self.setAnimated(True)
# 选中不显示虚线
self.setFocusPolicy(Qt.NoFocus)
self.doubleClicked.connect(self.select_image)
self.setMinimumWidth(200)
def select_image(self, file_index):
file_name = self.fileSystemModel.filePath(file_index)
if file_name.endswith(('.jpg', '.png', '.bmp')):
src_img = imdecode(np.fromfile(file_name, dtype=np.uint8), -1)
self.mainwindow.change_image(src_img)

@ -0,0 +1,125 @@
import cv2
GRAYING_STACKED_WIDGET = 0
FILTER_STACKED_WIDGET = 1
MORPH_STACKED_WIDGET = 2
GRAD_STACKED_WIDGET = 3
THRESH_STACKED_WIDGET = 4
EDGE_STACKED_WIDGET = 5
# 功能区
BGR2GRAY_COLOR = 0
GRAY2BGR_COLOR = 1
COLOR = {
BGR2GRAY_COLOR: cv2.COLOR_BGR2GRAY,
GRAY2BGR_COLOR: cv2.COLOR_GRAY2BGR
}
# 图像灰度处理
RBG2GRAY = 0
REVERSE = 1
PLTRANS = 2
Binarization = 3
MEAN_FILTER = 0
GAUSSIAN_FILTER = 1
MEDIAN_FILTER = 2
ERODE_MORPH_OP = 0
DILATE_MORPH_OP = 1
OPEN_MORPH_OP = 2
CLOSE_MORPH_OP = 3
GRADIENT_MORPH_OP = 4
TOPHAT_MORPH_OP = 5
BLACKHAT_MORPH_OP = 6
MORPH_OP = {
ERODE_MORPH_OP: cv2.MORPH_ERODE,
DILATE_MORPH_OP: cv2.MORPH_DILATE,
OPEN_MORPH_OP: cv2.MORPH_OPEN,
CLOSE_MORPH_OP: cv2.MORPH_CLOSE,
GRADIENT_MORPH_OP: cv2.MORPH_GRADIENT,
TOPHAT_MORPH_OP: cv2.MORPH_TOPHAT,
BLACKHAT_MORPH_OP: cv2.MORPH_BLACKHAT
}
RECT_MORPH_SHAPE = 0
CROSS_MORPH_SHAPE = 1
ELLIPSE_MORPH_SHAPE = 2
MORPH_SHAPE = {
RECT_MORPH_SHAPE: cv2.MORPH_RECT,
CROSS_MORPH_SHAPE: cv2.MORPH_CROSS,
ELLIPSE_MORPH_SHAPE: cv2.MORPH_ELLIPSE
}
SOBEL_GRAD = 0
SCHARR_GRAD = 1
LAPLACIAN_GRAD = 2
BINARY_THRESH_METHOD = 0
BINARY_INV_THRESH_METHOD = 1
TRUNC_THRESH_METHOD = 2
TOZERO_THRESH_METHOD = 3
TOZERO_INV_THRESH_METHOD = 4
OTSU_THRESH_METHOD = 5
THRESH_METHOD = {
BINARY_THRESH_METHOD: cv2.THRESH_BINARY, # 0
BINARY_INV_THRESH_METHOD: cv2.THRESH_BINARY_INV, # 1
TRUNC_THRESH_METHOD: cv2.THRESH_TRUNC, # 2
TOZERO_THRESH_METHOD: cv2.THRESH_TOZERO, # 3
TOZERO_INV_THRESH_METHOD: cv2.THRESH_TOZERO_INV, # 4
OTSU_THRESH_METHOD: cv2.THRESH_OTSU # 5
}
EXTERNAL_CONTOUR_MODE = 0
LIST_CONTOUR_MODE = 1
CCOMP_CONTOUR_MODE = 2
TREE_CONTOUR_MODE = 3
CONTOUR_MODE = {
EXTERNAL_CONTOUR_MODE: cv2.RETR_EXTERNAL,
LIST_CONTOUR_MODE: cv2.RETR_LIST,
CCOMP_CONTOUR_MODE: cv2.RETR_CCOMP,
TREE_CONTOUR_MODE: cv2.RETR_TREE
}
NONE_CONTOUR_METHOD = 0
SIMPLE_CONTOUR_METHOD = 1
CONTOUR_METHOD = {
NONE_CONTOUR_METHOD: cv2.CHAIN_APPROX_NONE,
SIMPLE_CONTOUR_METHOD: cv2.CHAIN_APPROX_SIMPLE
}
NORMAL_CONTOUR = 0
RECT_CONTOUR = 1
MINRECT_CONTOUR = 2
MINCIRCLE_CONTOUR = 3
# 均衡化
BLUE_CHANNEL = 0
GREEN_CHANNEL = 1
RED_CHANNEL = 2
ALL_CHANNEL = 3
# 伪彩色变换
COLORMAP_AUTUMN = 0
COLORMAP_BONE = 1
COLORMAP_JET = 2
COLORMAP_WINTER = 3
COLORMAP_RAINBOW = 4
COLORMAP_OCEAN = 5
COLORMAP_SUMMER = 6
COLORMAP_SPRING = 7
COLORMAP_COOL = 8
COLORMAP_HSV = 9
COLORMAP_PINK = 10
COLORMAP_HOT = 11

@ -0,0 +1,15 @@
# import numpy as np
from numpy import array,random,clip,uint8
def exponential_noise(image, scale = 0.1):
image = array(image/255, dtype=float)
noise = random.exponential(scale,image.shape)
out = image + noise
if out.min() < 0:
low_clip = -1.
else:
low_clip = 0.
out = clip(out, low_clip, 1.0)
out = uint8(out*255)
return out

@ -0,0 +1,17 @@
# import numpy as np
# import cv2
from numpy import array,random,clip,uint8
def gamma_noise(image, var=0.1):
image = array(image/255, dtype=float)
# 伽马分布
noise = random.gamma(3,var ** 0.5, image.shape)
out = image + noise
if out.min() < 0:
low_clip = -1.
else:
low_clip = 0.
out = clip(out, low_clip, 1.0)
out = uint8(out*255)
return out

@ -0,0 +1,17 @@
# import numpy as np
# import cv2
from numpy import array,random,clip,uint8
def gasuss_noise(image, var=0.1, mean=0):
image = array(image/255, dtype=float)
# 高斯分布
noise = random.normal(mean, var ** 0.5, image.shape)
out = image + noise
if out.min() < 0:
low_clip = -1.
else:
low_clip = 0.
out = clip(out, low_clip, 1.0)
out = uint8(out*255)
return out

@ -0,0 +1,19 @@
from random import random
from numpy import zeros,uint8,random
import cv2
def impluse_noise(image,prob=0.1):
output = zeros(image.shape,uint8)
thres = 1 - prob
for i in range(image.shape[0]):
for j in range(image.shape[1]):
rdn = random.random()
if rdn < prob:
output[i][j] = 0
elif rdn > thres:
output[i][j] = 255
else:
output[i][j] = image[i][j]
return output

@ -0,0 +1,16 @@
# import numpy as np
from numpy import array,clip,uint8
from numpy.random import rayleigh
def rayleigh_noise(image,var=0.1):
image = array(image/255, dtype=float)
# 瑞利分布
noise = rayleigh(var ** 0.5, image.shape)
out = image + noise
if out.min() < 0:
low_clip = -1.
else:
low_clip = 0.
out = clip(out, low_clip, 1.0)
out = uint8(out*255)
return out

@ -0,0 +1,15 @@
from numpy import array,clip,uint8
from numpy.random import uniform
def uniform_noise(image,hight=1.0,low=0.0):
image = array(image/255, dtype=float)
# 均匀分布
noise = uniform(low,hight,image.shape)
out = image + noise
if out.min() < 0:
low_clip = -1.
else:
low_clip = 0.
out = clip(out, low_clip, 1.0)
out = uint8(out*255)
return out

@ -0,0 +1,27 @@
import cv2
import numpy as np
def Beauty(img):
step = 5
kernel = (32,32) #图片大一点,此处尺寸大一点
img = img/255.0
sz = img.shape[:2]
sz1 = (int(round(sz[1] * step)), int(round(sz[0] * step)))
sz2 = (int(round(kernel[0] * step)), int(round(kernel[0] * step)))
sI = cv2.resize(img, sz1, interpolation=cv2.INTER_LINEAR)
sp = cv2.resize(img, sz1, interpolation=cv2.INTER_LINEAR)
msI = cv2.blur(sI, sz2)
msp = cv2.blur(sp, sz2)
msII = cv2.blur(sI*sI, sz2)
msIp = cv2.blur(sI*sp, sz2)
vsI = msII - msI*msI
csIp = msIp - msI*msp
recA = csIp/(vsI+0.01)
recB = msp - recA*msI
mA = cv2.resize(recA, (sz[1],sz[0]), interpolation=cv2.INTER_LINEAR)
mB = cv2.resize(recB, (sz[1],sz[0]), interpolation=cv2.INTER_LINEAR)
gf = mA*img + mB
gf = gf*255
gf[gf>255] = 255
final = gf.astype(np.uint8)
return final

@ -0,0 +1,62 @@
import cv2
import numpy as np
def hsvProcess(img,h_value,s_value,v_value):
h_value = float(h_value/100)
s_value = float(s_value/100)
v_value = float(v_value/100)
if img.shape[2] == 4:
img = cv2.cvtColor(img, cv2.COLOR_RGBA2RGB)
H, S, V = cv2.split(img) # (b,g,r)
new_pic = cv2.merge([np.uint8(H * h_value), np.uint8(S * s_value), np.uint8(V * v_value)])
return new_pic
def rgb2hsi(rgb_img,h_value,s_value,i_value):
h_value = float(h_value / 100)
s_value = float(s_value / 100)
i_value = float(i_value / 100)
if rgb_img.shape[2] == 4:
img = cv2.cvtColor(rgb_img, cv2.COLOR_RGBA2RGB)
rows = int(rgb_img.shape[0])
cols = int(rgb_img.shape[1])
b, g, r = cv2.split(img)
# 归一化到[0,1]
b = b / 255.0
g = g / 255.0
r = r / 255.0
hsi_img = img.copy()
H, S, I = cv2.split(hsi_img)
for i in range(rows):
for j in range(cols):
# 获得theta值
num = 0.5 * ((r[i, j]-g[i, j])+(r[i, j]-b[i, j]))
den = np.sqrt((r[i, j]-g[i, j])**2+(r[i, j]-b[i, j])*(g[i, j]-b[i, j]))
theta = float(np.arccos(num/den))
if den == 0:
H = 0
elif b[i, j] <= g[i, j]:
H = theta
else:
H = 2*3.14169265 - theta
min_RGB = min(min(b[i, j], g[i, j]), r[i, j])
sum = b[i, j]+g[i, j]+r[i, j]
if sum == 0:
S = 0
else:
S = 1 - 3*min_RGB/sum
# 归一到0-1之间
H = H/(2*3.14159265)
I = sum/3.0
# 输出HSI图像扩充到255以方便显示一般H分量在[0,2pi]之间S和I在[0,1]之间
hsi_img[i, j, 0] = H*255
hsi_img[i, j, 1] = S*255
hsi_img[i, j, 2] = I*255
hsi_img[:,:,0] = hsi_img[:,:,0]*h_value
hsi_img[:,:,1] = hsi_img[:,:,0]*s_value
hsi_img[:,:,1] = hsi_img[:,:,0]*i_value
return hsi_img

@ -0,0 +1,46 @@
import cv2
from function.GrayscaleTrans.BGR2GRAY import rgbToGray
from function.ColorImageProcess.HSIProcess import hsvProcess
def pseudoColorTrans(img,H,S,V,type):
if img.shape == 4:
img = cv2.cvtColor(img,cv2.COLOR_RGBA2BGR)
img_gray = rgbToGray(img)
if type == 0:
img_color = cv2.applyColorMap(img_gray, cv2.COLORMAP_AUTUMN)
elif type == 1:
img_color = cv2.applyColorMap(img_gray, cv2.COLORMAP_BONE)
elif type == 2:
img_color = cv2.applyColorMap(img_gray, cv2.COLORMAP_JET)
elif type == 3:
img_color = cv2.applyColorMap(img_gray, cv2.COLORMAP_WINTER)
elif type == 4:
img_color = cv2.applyColorMap(img_gray, cv2.COLORMAP_RAINBOW)
elif type == 5:
img_color = cv2.applyColorMap(img_gray, cv2.COLORMAP_OCEAN)
elif type == 6:
img_color = cv2.applyColorMap(img_gray, cv2.COLORMAP_SUMMER)
elif type == 7:
img_color = cv2.applyColorMap(img_gray, cv2.COLORMAP_SPRING)
elif type == 8:
img_color = cv2.applyColorMap(img_gray, cv2.COLORMAP_COOL)
elif type == 9:
img_color = cv2.applyColorMap(img_gray, cv2.COLORMAP_HSV)
elif type == 10:
img_color = cv2.applyColorMap(img_gray, cv2.COLORMAP_PINK)
elif type == 11:
img_color = cv2.applyColorMap(img_gray, cv2.COLORMAP_HOT)
img_color = hsvProcess(img_color,H,S,V)
return img_color
# img_gray = cv2.imread("../pic/beach.png",cv2.IMREAD_GRAYSCALE)
# img_color = cv2.applyColorMap(img_gray,cv2.COLORMAP_JET)
# img = cv2.imread('../pic/beach.png')
# img_gray = pseudoColorTrans(img,type)
# cv2.imshow('img_color',img_gray)
# cv2.waitKey(0)
# cv2.imshow('img_color',img_color)
# cv2.waitKey(0)

@ -0,0 +1,16 @@
import cv2
from function.ColorImageProcess.HSIProcess import hsvProcess
def rgb2cmy(img,H,S,V):
if img.shape[2] == 4:
img = cv2.cvtColor(img,cv2.COLOR_RGBA2RGB)
(b,g,r) = cv2.split(img)
b = 1 - b/b.max()
g = 1 - g/g.max()
r = 1 - r/r.max()
img_1 = cv2.merge([255*b,255*g,255*r])
img_result = hsvProcess(img_1,H,S,V)
return img_result

@ -0,0 +1,75 @@
import cv2
import numpy as np
from function.GrayscaleTrans.BGR2GRAY import rgbToGray
def make_transform_matrix(image,d,s1,n):
transfor_matrix = np.zeros(image.shape)
center_point = tuple(map(lambda x: (x - 1) / 2, s1.shape))
for i in range(transfor_matrix.shape[0]):
for j in range(transfor_matrix.shape[1]):
def cal_distance(pa, pb):
from math import sqrt
dis = sqrt((pa[0] - pb[0]) ** 2 + (pa[1] - pb[1]) ** 2)
return dis
dis = cal_distance(center_point, (i, j))
transfor_matrix[i, j] = 1 / ((1 + (dis / d)) ** (2 * n))
return transfor_matrix
def butterworthFilter(image, d, n,kind):
'''
巴特沃斯低通滤波器
:param image: 输入图像
:param d: 滤波半径
:param n: 阶数
:return:
'''
image = cv2.cvtColor(image, cv2.COLOR_RGBA2BGR)
image = rgbToGray(image)
f = np.fft.fft2(image)
fshift = np.fft.fftshift(f)
s1 = np.log(np.abs(fshift))
if kind == 1:
d_matrix = make_transform_matrix(image,d,s1,n)
elif kind == 4:
d_matrix = 1-make_transform_matrix(image, d, s1, n)
img_d1 = np.abs(np.fft.ifft2(np.fft.ifftshift(fshift * d_matrix)))
# 高通滤波
# img_d1 = np.abs(np.fft.ifft2(np.fft.ifftshift(fshift * (1-d_matrix))))
img_d1 = img_d1 / img_d1.max()
img_d1 = img_d1 * 255
img_d1 = img_d1.astype(np.uint8)
return img_d1
# 定义函数,巴特沃斯带阻/通滤波模板
def ButterworthBand(src, w, d0, n):
template = np.zeros(src.shape, dtype=np.float32) # 构建滤波器
r, c = src.shape
for i in np.arange(r):
for j in np.arange(c):
distance = np.sqrt((i - r / 2) ** 2 + (j - c / 2) ** 2)
# 巴特沃斯分布
template[i, j] = 1/(1+(distance*w/(distance**2 - d0**2))**(2*n))
return template
def butterworthSelectFilter(image, d, n,W,kind):
image = cv2.cvtColor(image, cv2.COLOR_RGBA2BGR)
image = rgbToGray(image) # 图像灰度化
f = np.fft.fft2(image) # 图像的傅里叶变换
fshift = np.fft.fftshift(f) # 将低频移动到中心
s1 = np.log(np.abs(fshift))
if kind == 1: # 巴特沃斯带阻滤波器
d_matrix = ButterworthBand(image,W,d,n)
elif kind == 4: # 巴特沃斯带通滤波器
d_matrix = 1-ButterworthBand(image, W, d, n)
# 与模板相乘后再傅里叶逆变换
img_d1 = np.abs(np.fft.ifft2(np.fft.ifftshift(fshift * d_matrix)))
img_d1 = img_d1 / img_d1.max()
img_d1 = img_d1 * 255
img_d1 = img_d1.astype(np.uint8)
return img_d1

@ -0,0 +1,66 @@
import cv2
import numpy as np
from function.GrayscaleTrans.BGR2GRAY import rgbToGray
# 高斯滤波器模板
def make_transform_matrix(d,image,s1):
transfor_matrix = np.zeros(image.shape)
center_point = tuple(map(lambda x: (x - 1) / 2, s1.shape))
for i in range(transfor_matrix.shape[0]):
for j in range(transfor_matrix.shape[1]):
def cal_distance(pa, pb):
from math import sqrt
dis = sqrt((pa[0] - pb[0]) ** 2 + (pa[1] - pb[1]) ** 2)
return dis
dis = cal_distance(center_point, (i, j))
transfor_matrix[i, j] = np.exp(-(dis ** 2) / (2 * (d ** 2)))
return transfor_matrix
def GaussianFilter(image,d,kind):
image = cv2.cvtColor(image, cv2.COLOR_RGBA2BGR)
image = rgbToGray(image)
f = np.fft.fft2(image)
fshift = np.fft.fftshift(f)
s1 = np.log(np.abs(fshift))
if kind == 2:
d_matrix = make_transform_matrix(d,image,s1)
elif kind == 5:
d_matrix = 1-make_transform_matrix(d,image,s1)
img_d1 = np.abs(np.fft.ifft2(np.fft.ifftshift(fshift*d_matrix)))
img_d1 = img_d1 / img_d1.max()
img_d1 = img_d1 * 255
img_d1 = img_d1.astype(np.uint8)
return img_d1
# 定义函数,高斯带阻/通滤波模板
def GaussianBand(src, w, d0):
template = np.zeros(src.shape, dtype=np.float32) # 构建滤波器
r, c = src.shape
for i in np.arange(r):
for j in np.arange(c):
distance = np.sqrt((i - r / 2) ** 2 + (j - c / 2) ** 2)
temp = ((distance**2 - d0**2)/(distance*w+0.00000001))**2
template[i, j] = 1 - np.exp(-0.5 * temp)
return template
def GaussianSelectFilter(image,d,W,kind):
image = cv2.cvtColor(image, cv2.COLOR_RGBA2BGR)
image = rgbToGray(image)
f = np.fft.fft2(image)
fshift = np.fft.fftshift(f)
s1 = np.log(np.abs(fshift))
if kind == 2: # 高斯带阻滤波器
d_matrix = GaussianBand(image,W,d)
elif kind == 5: # 高斯带通滤波器
d_matrix = 1-GaussianBand(image,W,d)
img_d1 = np.abs(np.fft.ifft2(np.fft.ifftshift(fshift*d_matrix)))
img_d1 = img_d1 / img_d1.max()
img_d1 = img_d1 * 255
img_d1 = img_d1.astype(np.uint8)
return img_d1

@ -0,0 +1,139 @@
import numpy as np
import cv2
from function.GrayscaleTrans.BGR2GRAY import rgbToGray
def make_transform_matrix(d,image,s1):
transfor_matrix = np.zeros(image.shape)
center_point = tuple(map(lambda x:(x-1)/2,s1.shape))
for i in range(transfor_matrix.shape[0]):
for j in range(transfor_matrix.shape[1]):
def cal_distance(pa,pb):
from math import sqrt
dis = sqrt((pa[0]-pb[0])**2+(pa[1]-pb[1])**2)
return dis
dis = cal_distance(center_point,(i,j))
if dis <= d:
transfor_matrix[i,j]=1
else:
transfor_matrix[i,j]=0
return transfor_matrix
def idealFilter(img,r,kind):
'''
理想滤波器
:param img: 输入图像
:param r: 滤波器半径
:param kind: 滤波器类型
:return: 滤波后的图像
'''
img = cv2.cvtColor(img, cv2.COLOR_RGBA2BGR) # 四维转三维
img = rgbToGray(img) # 灰度化
f = np.fft.fft2(img) # 傅里叶变换
fshift = np.fft.fftshift(f) # 将低频部分移到中心
# 取绝对值:将复数变化成实数
# 取对数的目的为了将数据变化到0-255
s1 = np.log(np.abs(fshift))
# d1 = make_transform_matrix(r, fshift, s1)
if kind == 0: # 理想低通滤波
d1 = make_transform_matrix(r, fshift, s1)
elif kind == 3: # 理想高通滤波
d1 = 1-make_transform_matrix(r, fshift, s1)
img_d1 = np.abs(np.fft.ifft2(np.fft.ifftshift(fshift * d1)))
img_d1 = img_d1 / img_d1.max()
img_d1 = img_d1 * 255
img_d1 = img_d1.astype(np.uint8)
return img_d1
def make_select_matrix(d,image,s1,W):
"""
构建理想选择滤波器
:param d: 滤波器半径
:param image: 图像的傅里叶变换
:return:
"""
transfor_matrix = np.zeros(image.shape)
center_point = tuple(map(lambda x:(x-1)/2,s1.shape))
for i in range(transfor_matrix.shape[0]):
for j in range(transfor_matrix.shape[1]):
def cal_distance(pa,pb):
from math import sqrt
dis = sqrt((pa[0]-pb[0])**2+(pa[1]-pb[1])**2) # 计算两点之间距离
return dis
dis = cal_distance(center_point,(i,j))
# if dis <= d + W/2 and dis >= d - W/2:
if dis <= d + W and dis >= d:
transfor_matrix[i,j]=0
else:
transfor_matrix[i,j]=1
return transfor_matrix
def idealSelectFilter(img,r,W,kind):
img = cv2.cvtColor(img, cv2.COLOR_RGBA2BGR)
img = rgbToGray(img)
# 傅里叶变换
f = np.fft.fft2(img)
# 将低频部分移到中心
fshift = np.fft.fftshift(f)
# 取绝对值:将复数变化成实数
# 取对数的目的为了将数据变化到0-255
s1 = np.log(np.abs(fshift))
if kind == 0: # 理想带阻滤波器
d1 = make_select_matrix(r, fshift, s1, W)
elif kind == 3: # 理想带通滤波器
d1 = 1-make_select_matrix(r, fshift, s1, W)
# 与模板相乘后再傅里叶逆变换
img_d1 = np.abs(np.fft.ifft2(np.fft.ifftshift(fshift * d1)))
img_d1 = img_d1 / img_d1.max()
img_d1 = img_d1 * 255
img_d1 = img_d1.astype(np.uint8)
return img_d1
def make_NotchFilter_matrix(d,image,s1):
"""
构建理想陷波滤波器
:param d: 滤波器半径
:param image: 图像的傅里叶变换
:return:
"""
transfor_matrix = np.zeros(image.shape)
# center_point = tuple(map(lambda x:(x-1)/2,s1.shape))
center_point_1 = (s1.shape[0]/4,s1.shape[1]/2)
center_point_2 = (3*s1.shape[0]/4,s1.shape[1]/2)
for i in range(transfor_matrix.shape[0]):
for j in range(transfor_matrix.shape[1]):
def cal_distance(pa,pb):
from math import sqrt
dis = sqrt((pa[0]-pb[0])**2+(pa[1]-pb[1])**2)
return dis
dis_1 = cal_distance(center_point_1,(i,j))
dis_2 = cal_distance(center_point_2,(i,j))
# if dis <= d + W/2 and dis >= d - W/2:
# if dis <= d + W and dis >= d:
if dis_1 <= d or dis_2 <= d:
transfor_matrix[i,j]=0
else:
transfor_matrix[i,j]=1
return transfor_matrix
def idealNotchFilter(img,r,kind):
img = cv2.cvtColor(img, cv2.COLOR_RGBA2BGR)
img = rgbToGray(img)
# 傅里叶变换
f = np.fft.fft2(img)
# 将低频部分移到中心
fshift = np.fft.fftshift(f)
# fshift = fshift.astype(np.uint8)
# 取绝对值:将复数变化成实数
# 取对数的目的为了将数据变化到0-255
s1 = np.log(np.abs(fshift))
if kind == 6:
d1 = make_NotchFilter_matrix(r, fshift, s1)
elif kind == 7:
d1 = 1-make_NotchFilter_matrix(r, fshift, s1)
img_d1 = np.abs(np.fft.ifft2(np.fft.ifftshift(fshift * d1)))
img_d1 = img_d1 / img_d1.max()
img_d1 = img_d1 * 255
img_d1 = img_d1.astype(np.uint8)
return img_d1

@ -0,0 +1,9 @@
from cv2 import selectROI
from function.GeometricTrans.LargeSmall import largeSmall
def cut(img,rate):
# 得到手动裁剪的矩形区域
bbox = selectROI(img, False)
cut = img[bbox[1]:bbox[1] + bbox[3], bbox[0]:bbox[0] + bbox[2]]
return largeSmall(cut, rate)

@ -0,0 +1,13 @@
# import cv2
from cv2 import resize
def largeSmall(img,rate=100):
rate = rate / 100
img_info = img.shape
image_height = img_info[0]
image_weight = img_info[1]
desHeight = int(rate*image_height)
desWeight = int(rate*image_weight)
img = resize(img,(desWeight,desHeight))
return img

@ -0,0 +1,15 @@
import cv2
import numpy as np
def mirror1(img,rate):
print("111")
cv2.flip(img, 1, img)
return img
def mirror2(img,rate):
cv2.flip(img, 0, img)
return img
def mirror3(img,rate):
cv2.flip(img, -1,img)
return img

@ -0,0 +1,16 @@
from cv2 import getRotationMatrix2D,warpAffine
from math import fabs,sin,radians,cos
def ratate(img,degree=0):
height, width = img.shape[:2]
# 旋转后的尺寸
heightNew = int(width * fabs(sin(radians(degree))) + height * fabs(cos(radians(degree))))
widthNew = int(height * fabs(sin(radians(degree))) + width * fabs(cos(radians(degree))))
# 获得仿射变换矩阵
matRotation = getRotationMatrix2D((width / 2, height / 2), degree, 1)
matRotation[0, 2] += (widthNew - width) / 2
matRotation[1, 2] += (heightNew - height) / 2
# 进行仿射变换
imgRotation = warpAffine(img, matRotation, (widthNew, heightNew), borderValue=(68, 68, 68))
return imgRotation

Some files were not shown because too many files have changed in this diff Show More

Loading…
Cancel
Save