def __init__(self): #读入图像 img = cv.imread("lena_full.jpg") img2 = cv.Mat() cv.cvtColor(img, img2, cv.CV_BGR2GRAY) img = cv.Mat() cv.resize(img2, img, cv.Size(N, N)) self.fimg = fft.fft2(img[:]) # 图像的频域信号 mag_img = np.log10(np.abs(self.fimg)) # 创建计算用图像 filtered_img = np.zeros((N, N), dtype=np.float) self.mask = np.zeros((N, N), dtype=np.float) self.mask_img = cv.asMat(self.mask) # 在self.mask上绘制多边形用的图像 # 创建数据源 self.data = ArrayPlotData(mag_img=fft.fftshift(mag_img), filtered_img=filtered_img, mask_img=self.mask) # 创建三个图像绘制框以及容器 meg_plot, img = self.make_image_plot("mag_img") mask_plot, _ = self.make_image_plot("mask_img") filtered_plot, _ = self.make_image_plot("filtered_img") self.plot = HPlotContainer(meg_plot, mask_plot, filtered_plot) # 创建套索工具 lasso_selection = LassoSelection(component=img) lasso_overlay = LassoOverlay(lasso_selection=lasso_selection, component=img, selection_alpha=0.3) img.tools.append(lasso_selection) img.overlays.append(lasso_overlay) self.lasso_selection = lasso_selection # 监听套索工具的事件、开启时钟事件 lasso_selection.on_trait_change(self.lasso_updated, "disjoint_selections") self.timer = Timer(50, self.on_timer)
def __init__(self): #读入图像 img = cv.imread("lena_full.jpg") img2 = cv.Mat() cv.cvtColor(img, img2, cv.CV_BGR2GRAY) img = cv.Mat() cv.resize(img2, img, cv.Size(N, N)) self.fimg = fft.fft2(img[:]) # 图像的频域信号 mag_img = np.log10(np.abs(self.fimg)) # 创建计算用图像 filtered_img = np.zeros((N, N), dtype=np.float) self.mask = np.zeros((N, N), dtype=np.float) self.mask_img = cv.asMat(self.mask) # 在self.mask上绘制多边形用的图像 # 创建数据源 self.data = ArrayPlotData( mag_img = fft.fftshift(mag_img), filtered_img = filtered_img, mask_img = self.mask ) # 创建三个图像绘制框以及容器 meg_plot, img = self.make_image_plot("mag_img") mask_plot, _ = self.make_image_plot("mask_img") filtered_plot, _ = self.make_image_plot("filtered_img") self.plot = HPlotContainer(meg_plot, mask_plot, filtered_plot) # 创建套索工具 lasso_selection = LassoSelection(component=img) lasso_overlay = LassoOverlay(lasso_selection = lasso_selection, component=img, selection_alpha=0.3) img.tools.append(lasso_selection) img.overlays.append(lasso_overlay) self.lasso_selection = lasso_selection # 监听套索工具的事件、开启时钟事件 lasso_selection.on_trait_change(self.lasso_updated, "disjoint_selections") self.timer = Timer(50, self.on_timer)
# -*- coding: utf-8 -*- import pyopencv as cv import numpy as np import matplotlib.pyplot as plt # 读入图片并缩小为1/2 img0 = cv.imread("lena.jpg") size = img0.size() w, h = size.width, size.height img1 = cv.Mat() cv.resize(img0, img1, cv.Size(w // 2, h // 2)) # 各种卷积核 kernels = [("低通滤波器", np.array([[1, 1, 1], [1, 2, 1], [1, 1, 1]]) * 0.1), ("高通滤波器", np.array([[0, -1, 0], [-1, 5, -1], [0, -1, 0]])), ("边缘检测", np.array([[-1, -1, -1], [-1, 8, -1], [-1, -1, -1]]))] index = 0 for name, kernel in kernels: plt.subplot(131 + index) # 将卷积核转换为Mat对象 kmat = cv.asMat(kernel.astype(np.float), force_single_channel=True) img2 = cv.Mat() cv.filter2D(img1, img2, -1, kmat) # 由于matplotlib的颜色顺序和OpenCV的顺序相反 plt.imshow(img2[:, :, ::-1]) plt.title(name) index += 1 plt.gca().set_axis_off() plt.subplots_adjust(0.02, 0, 0.98, 1, 0.02, 0) plt.show()
# -*- coding: utf-8 -*- import numpy as np from numpy import fft import pyopencv as cv import matplotlib.pyplot as plt N = 256 img = cv.imread("lena_full.jpg") img2 = cv.Mat() cv.cvtColor(img, img2, cv.CV_BGR2GRAY) img = cv.Mat() cv.resize(img2, img, cv.Size(N, N)) fimg = fft.fft2(img[:]) mag_img = np.log10(np.abs(fimg)) shift_mag_img = fft.fftshift(mag_img) rects = [(80,125,85,130),(90,90,95,95), (150, 10, 250, 250), (110, 110, 146, 146)] filtered_results = [] for i, (x0, y0, x1, y1) in enumerate(rects): mask = np.zeros((N, N), dtype=np.bool) mask[x0:x1+1, y0:y1+1] = True mask[N-x1:N-x0+1, N-y1:N-y0+1] = True mask = fft.fftshift(mask) fimg2 = fimg * mask filtered_img = fft.ifft2(fimg2).real
def resize(src, w, h, dst=None): if dst is None: dst = src.clone() cv.resize(src, dst, cv.Size(w, h)) return dst
# -*- coding: utf-8 -*- import pyopencv as cv import numpy as np import matplotlib.pyplot as plt # 读入图片并缩小为1/2 img0 = cv.imread("lena.jpg") size = img0.size() w, h = size.width, size.height img1 = cv.Mat() cv.resize(img0, img1, cv.Size(w//2, h//2)) # 各种卷积核 kernels = [ (u"低通滤波器",np.array([[1,1,1],[1,2,1],[1,1,1]])*0.1), (u"高通滤波器",np.array([[0,-1,0],[-1,5,-1],[0,-1,0]])), (u"边缘检测",np.array([[-1,-1,-1],[-1,8,-1],[-1,-1,-1]])) ] index = 0 for name, kernel in kernels: plt.subplot(131+index) # 将卷积核转换为Mat对象 kmat = cv.asMat(kernel.astype(np.float), force_single_channel=True) img2 = cv.Mat() cv.filter2D(img1, img2, -1, kmat) # 由于matplotlib的颜色顺序和OpenCV的顺序相反 plt.imshow(img2[:,:,::-1]) plt.title(name) index += 1 plt.gca().set_axis_off()
# -*- coding: utf-8 -*- import numpy as np from numpy import fft import pyopencv as cv import matplotlib.pyplot as plt N = 256 img = cv.imread("lena_full.jpg") img2 = cv.Mat() cv.cvtColor(img, img2, cv.CV_BGR2GRAY) img = cv.Mat() cv.resize(img2, img, cv.Size(N, N)) fimg = fft.fft2(img[:]) mag_img = np.log10(np.abs(fimg)) shift_mag_img = fft.fftshift(mag_img) rects = [(80, 125, 85, 130), (90, 90, 95, 95), (150, 10, 250, 250), (110, 110, 146, 146)] filtered_results = [] for i, (x0, y0, x1, y1) in enumerate(rects): mask = np.zeros((N, N), dtype=np.bool) mask[x0:x1 + 1, y0:y1 + 1] = True mask[N - x1:N - x0 + 1, N - y1:N - y0 + 1] = True mask = fft.fftshift(mask) fimg2 = fimg * mask filtered_img = fft.ifft2(fimg2).real filtered_results.append(filtered_img) ### 绘图部分 ###