convolve

本文深入探讨了图像处理与AI音视频处理的关键技术,包括OpenGL、OpenCV、AR特效、视频分割与语义识别等。通过具体实例,展示了如何运用这些技术解决实际问题,特别强调了其在AR、SLAM、物体检测识别等方面的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


/***************************************************************************
* Func: convolve *
* *
* Desc: convolves an image with the floating point kernel passed and *
* writes out new image one line at a time *
* *
* Params: source - pointer to image in memory *
* cols - number of columns in image *
* rows - number of rows in image *
* kwidth - width of convolution kernel *
* kheight - height of convolution kernel *
* kernel - pointer to convolution kernel *
* bias - value to add to convolution sum *
* filename - name of output file *
***************************************************************************/

void convolve(image_ptr source, int cols, int rows, int kwidth, int kheight,
float *kernel, int bias, char *filename)
{
int x, y, i; /* image loop variables */
int kernx, kerny; /* kernel loop variables */
int index; /* image index */
int xextra, yextra; /* size of boundary */
int conv_line; /* size of output line */
float sum; /* accumulator used during convolution */
unsigned long destadd; /* destination image address */
unsigned long sourceadd; /* index into source image */
unsigned long sourcebase; /* address of line */
unsigned char *dest; /* destination image line */
FILE *fp; /* output file pointer */
unsigned char left[25]; /* storage of left pixel for duplication */
unsigned char right[25]; /* storage of right pixel for duplication */
int xpad, ypad; /* number of pixels to duplicate at edges */
int last_line; /* last line to process */

yextra = (kheight/2)*2;
ypad = yextra/2;
xextra = (kwidth/2)*2;
xpad = xextra/2;
conv_line = cols - xextra;
last_line = rows - yextra;
dest = malloc(cols);

if((fp=fopen(filename, "wb")) == NULL)
{
printf("Unable to open %s for output\n",filename);
exit(1);
}
fprintf(fp, "P5\n%d %d\n255\n", cols, rows); /* print out header */

for(y=0; y<last_line; y++)
{
sourcebase=(unsigned long) cols * y;
destadd=0;
for(x=xextra/2; x< (cols-xpad); x++)
{
sum=0.0;
index=0;
for(kerny=0; kerny<kheight; kerny++)
for(kernx=0; kernx<kwidth; kernx++)
{
sourceadd = sourcebase + kernx + kerny * cols;
sum += (source[sourceadd] * kernel[index++]);
}

sum += bias;
CLIP(sum,0.0,255.0);

dest[destadd++]=(unsigned char) sum;
sourcebase++;
} /* for x */
for(i=0; i<xpad; i++)
left[i]=dest[0];
for(i=0; i<xpad; i++)
right[i]=dest[conv_line-1];
if(y==0)
for(i=0; i<ypad; i++)
{
fwrite(left, 1, xpad, fp);
fwrite(dest, 1, conv_line, fp);
fwrite(right, 1, xpad, fp);
}
fwrite(left, 1, xpad, fp);
fwrite(dest, 1, conv_line, fp);
fwrite(right, 1, xpad, fp);
if(y==(last_line-1))
for(i=0; i<ypad; i++)
{
fwrite(left, 1, xpad, fp);
fwrite(dest, 1, conv_line, fp);
fwrite(right, 1, xpad, fp);
}
} /* for y */

}


### Numpy Convolve 使用方法与实例 `np.convolve` 是 NumPy 中用于执行一维离散线性卷积的函数。该函数接受两个输入数组并返回它们的一维卷积结果。 #### 函数签名 ```python numpy.convolve(a, v, mode='full') ``` - `a`: 输入的第一个一维数组。 - `v`: 输入的第二个一维数组,通常称为核或滤波器。 - `mode`: 卷积模式,默认为 `'full'`,其他选项有 `'same'` 和 `'valid'`[^2]。 #### 不同模式解释 - **`'full'`**: 返回完整的离散线性卷积,输出长度等于第一个向量加上第二个向量减去1。 - **`'same'`**: 输出形状与较长的输入相同,在中心对齐的情况下截取卷积结果。 - **`'valid'`**: 只返回那些不依赖于零填充边界的元素,即只计算完全重叠部分的结果。 #### 实际应用案例:移动平均算法实现 下面是一个简单的例子来展示如何利用 `np.convolve` 来创建一个滑动窗口均值过滤器: ```python import numpy as np def moving_average(data, window_size): """Calculate the moving average over given data using convolution.""" weights = np.ones(window_size) / window_size # Create uniform filter of size=window_size ma_result = np.convolve(data, weights, 'valid') # Apply convolution to calculate MA return ma_result data_points = [10, 15, 12, 18, 13, 16, 14, 17, 19, 15] window_length = 3 ma_output = moving_average(data_points, window_length) print(ma_output) ``` 上述代码定义了一个名为 `moving_average` 的函数,它接收数据序列和窗口大小作为参数,并通过调用 `np.convolve()` 方法实现了简单移动平均(SMA)[^2]。 #### 更多示例:高斯平滑处理图像边缘检测中的应用 除了时间序列分析外,卷积还广泛应用于信号处理领域,比如图像处理中常用的 Sobel 边缘探测算子也可以借助此功能轻松构建: ```python from scipy import signal import matplotlib.pyplot as plt from skimage import io, color, img_as_float image = img_as_float(color.rgb2gray(io.imread('example_image.png'))) sobel_x_kernel = np.array([[-1, 0, 1], [-2, 0, 2], [-1, 0, 1]]) edges_x_direction = signal.convolve2d(image, sobel_x_kernel, boundary='symm', mode='same') plt.figure(figsize=(10, 5)) plt.subplot(121); plt.imshow(image, cmap="gray"); plt.title("Original Image") plt.subplot(122); plt.imshow(edges_x_direction, cmap="gray"); plt.title("Edges Detected (Sobel X)") plt.show() ``` 这里使用了 SciPy 库里的 `signal.convolve2d` 对二维灰度图片进行了水平方向上的梯度运算,从而提取出了物体轮廓特征[^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值