今天用MVEL犯迷糊了

 

 

//a
ParserContext ctx1 = new ParserContext();			
ctx1.addImport("time", System.class.getMethod("currentTimeMillis"));
Serializable exp1 = MVEL.compileExpression("time()", ctx1);
System.out.println(MVEL.executeExpression(exp1));	
			
//b
ParserContext ctx2 = new ParserContext();
ctx2.addImport("print", System.out.getClass().getMethod("print", String.class));
Serializable exp2 = MVEL.compileExpression("print('test')", ctx2);
System.out.println(MVEL.executeExpression(exp2));

 

     以上两段代码,b段代码始终报错“Caused by: java.lang.IllegalArgumentException: object is not an instance of declaring class”。

     开始一直找不到原因,认为是mvel不支持像out这种静态属性的方法,后来分析错误原因才知道自己犯傻了,a段代码不报错是因为currentTimeMillis方法是静态方法,调用时不需要instance,而out下面的print方法不是。


     所以在MVEL中调用System.out.print方法时没法偷懒,只能将b段代码改成:

 

 

ParserContext ctx2 = new ParserContext();
ctx2.addImport("print", System.out.getClass().getMethod("print", String.class));
Serializable exp2 = MVEL.compileExpression("System.out.print('test')", ctx2);
System.out.println(MVEL.executeExpression(exp2));
维纳滤波是一种非盲去模糊算法,可用于从模糊的输入图像中恢复清晰的图像,其目的是通过算法和数学模型来减少或消除由于摄像机抖动、物体运动或镜头失真等因素引起的图像模糊,提高图像的清晰度和可视化质量,在多个领域具有重要应用价值[^1][^2]。 ### 原理 维纳滤波的基本公式为: $G(u,v)=\frac{H^*(u,v)}{|H(u,v)|^2 + \frac{P_n(u,v)}{P_f(u,v)}}F(u,v)$ 其中,$G(u,v)$ 是滤波后的图像的频域表示,$H(u,v)$ 是退化函数的频域表示,$H^*(u,v)$ 是 $H(u,v)$ 的共轭,$P_n(u,v)$ 是噪声的功率谱,$P_f(u,v)$ 是原始图像的功率谱,$F(u,v)$ 是模糊图像的频域表示。 ### 代码实现(OpenCV) 以下是一个使用 OpenCV 和 Python 实现维纳滤波对图片进行去模糊处理的示例代码: ```python import cv2 import numpy as np def fftshift(input_img): rows, cols = input_img.shape crow, ccol = rows // 2, cols // 2 f_half1 = np.hstack((input_img[crow:, :ccol], input_img[crow:, ccol:])) f_half2 = np.hstack((input_img[:crow, :ccol], input_img[:crow, ccol:])) return np.vstack((f_half1, f_half2)) def calcWnrFilter(input_h_PSF, nsr): h_PSF_shifted = fftshift(input_h_PSF) planes = [np.float32(h_PSF_shifted.copy()), np.zeros(h_PSF_shifted.shape, np.float32)] complexI = cv2.merge(planes) cv2.dft(complexI, complexI) planes = cv2.split(complexI) denom = np.power(np.abs(planes[0]), 2) denom += nsr output_G = np.divide(planes[0], denom) return output_G # 读取模糊图像 blurry_img = cv2.imread('blurry_image.jpg', 0) # 假设已知PSF(点扩散函数),这里简单示例一个高斯PSF psf_size = 15 psf = cv2.getGaussianKernel(psf_size, 3) psf = psf * psf.T # 噪声功率谱与信号功率谱的比值(nsr) nsr = 0.01 # 计算维纳滤波的传递函数 G = calcWnrFilter(psf, nsr) # 对模糊图像进行傅里叶变换 planes = [np.float32(blurry_img), np.zeros(blurry_img.shape, np.float32)] complexI = cv2.merge(planes) cv2.dft(complexI, complexI) planes = cv2.split(complexI) # 应用维纳滤波 filtered_planes = [planes[0] * G, planes[1] * G] filtered_complexI = cv2.merge(filtered_planes) # 进行逆傅里叶变换 cv2.idft(filtered_complexI, filtered_complexI) cv2.split(filtered_complexI, planes) restored_img = cv2.magnitude(planes[0], planes[1]) # 显示结果 cv2.imshow('Blurry Image', blurry_img) cv2.imshow('Restored Image', restored_img.astype(np.uint8)) cv2.waitKey(0) cv2.destroyAllWindows() ``` ### 代码解释 1. **`fftshift` 函数**:用于将频域图像的零频率分量移到图像中心。 2. **`calcWnrFilter` 函数**:计算维纳滤波的传递函数 $G(u,v)$。 3. **读取模糊图像**:使用 `cv2.imread` 读取模糊图像。 4. **生成 PSF**:这里简单使用高斯核作为点扩散函数。 5. **计算维纳滤波传递函数**:调用 `calcWnrFilter` 函数。 6. **对模糊图像进行傅里叶变换**:使用 `cv2.dft` 进行傅里叶变换。 7. **应用维纳滤波**:将模糊图像的频域表示与维纳滤波传递函数相乘。 8. **进行逆傅里叶变换**:使用 `cv2.idft` 进行逆傅里叶变换得到恢复后的图像。 9. **显示结果**:使用 `cv2.imshow` 显示模糊图像和恢复后的图像。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值