请求绘制指定参数椭圆及实现核回归论文伪彩色效果的技术支持
实现《Kernel Regression for Image Processing and Reconstruction》论文效果图:椭圆绘制与伪彩色效果
一、参数化椭圆绘制
给定椭圆中心$(x_0, y_0)$、旋转角度$\theta$、拉伸系数$a$(x轴方向)、$b$(y轴方向)及缩放参数$s$,可通过参数方程+坐标变换实现精准绘制:
- 生成标准椭圆参数点:$x = s \cdot a \cdot \cos(t)$,$y = s \cdot b \cdot \sin(t)$,其中$t \in [0, 2\pi)$
- 旋转变换:\begin{bmatrix}
$$
\begin{bmatrix}
x' \
y'
\end{bmatrix}
\cos\theta & -\sin\theta \
\sin\theta & \cos\theta
\end{bmatrix}
\begin{bmatrix}
x \
y
\end{bmatrix}
$$ - 平移到指定中心:$x_{final} = x' + x_0$,$y_{final} = y' + y_0$
Python 实现代码
import numpy as np import matplotlib.pyplot as plt def draw_ellipse(x0, y0, theta, a, b, s, color='red', linestyle='-'): # 生成参数t t = np.linspace(0, 2*np.pi, 1000) # 标准椭圆点 x_ellipse = s * a * np.cos(t) y_ellipse = s * b * np.sin(t) # 旋转变换 theta_rad = np.deg2rad(theta) x_rot = x_ellipse * np.cos(theta_rad) - y_ellipse * np.sin(theta_rad) y_rot = x_ellipse * np.sin(theta_rad) + y_ellipse * np.cos(theta_rad) # 平移到中心 x_final = x_rot + x0 y_final = y_rot + y0 # 绘制 plt.plot(x_final, y_final, color=color, linestyle=linestyle) # 示例调用:中心(100,100),旋转30度,拉伸系数20、10,缩放1.5 draw_ellipse(100, 100, 30, 20, 10, 1.5) plt.axis('equal') plt.show()
二、伪彩色效果实现(论文第二张图)
论文中的伪彩色效果本质是将核回归输出的灰度响应值映射到彩色映射表,把单通道灰度数据转换为三通道彩色数据,核心是颜色映射的匹配。
实现思路
- 获取核回归输出的灰度矩阵(假设为
gray_data,值域通常为[0,1]或[0,255]) - 选择或自定义彩色映射(colormap):
- 若匹配通用论文风格,可直接用matplotlib预设的
jet、viridis等渐变colormap; - 若需精准匹配论文颜色,可提取图中关键颜色节点,自定义渐变映射。
- 若匹配通用论文风格,可直接用matplotlib预设的
- 通过colormap将灰度值转换为RGB颜色并可视化。
Python 实现代码
import numpy as np import matplotlib.pyplot as plt from matplotlib.colors import LinearSegmentedColormap # 模拟核回归输出的灰度响应矩阵 x = np.linspace(-5, 5, 200) y = np.linspace(-5, 5, 200) X, Y = np.meshgrid(x, y) gray_data = np.exp(-(X**2 + Y**2)/2) # 高斯分布模拟响应 # 方法1:使用预设colormap(接近多数论文伪彩色风格) plt.figure(figsize=(8,6)) plt.imshow(gray_data, cmap='jet', origin='lower') plt.colorbar(label='Kernel Regression Response') plt.title('Pseudocolor Plot (Preset Colormap)') plt.show() # 方法2:自定义colormap(匹配论文特定颜色) # 示例:深蓝→蓝→绿→黄→红的渐变 custom_colors = [ (0, 0, 0.8), # 深蓝 (0, 0.5, 1), # 蓝 (0, 1, 0.5), # 绿 (1, 1, 0), # 黄 (0.8, 0, 0) # 红 ] custom_cmap = LinearSegmentedColormap.from_list('custom_kernel', custom_colors, N=256) plt.figure(figsize=(8,6)) plt.imshow(gray_data, cmap=custom_cmap, origin='lower') plt.colorbar(label='Kernel Regression Response') plt.title('Pseudocolor Plot (Custom Colormap)') plt.show()
关键说明
- 若论文伪彩色是基于阈值分段着色,可先对灰度数据进行区间划分,再为每个区间分配对应颜色;
- 注意设置
imshow的origin参数,确保坐标系与论文图一致(lower对应原点在左下角,upper对应左上角)。
内容的提问来源于stack exchange,提问作者张桂宇
相关产品推荐
相关产品推荐

