- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想实现一个卷积函数以在均值滤波器和高斯滤波器中使用,并且我还需要实现这两个滤波器以应用于 pgm 文件。
我有
typedef struct _PGM{
int row;
int col;
int max_value;
int **matrix;
}PGM;
int convolution(int ** kernel,int ksize, PGM * image, PGM * output){
int i, j, x, y;
int sum;
int data;
int scale =ksize*ksize;
int coeff;
for (x=ksize/2; x<image->row-ksize/2;++x) {
for (y=ksize/2; y<image->col-ksize/2; ++y){
sum = 0;
for (i=-ksize/2; i<=ksize/2; ++i){
for (j=-ksize/2; j<=ksize/2; ++j){
data = image->matrix[x +i][y +j];
coeff = kernel[i+ksize/2][j+ksize/2];
sum += data * coeff;
}
}
output->matrix[x][y] = sum / scale;
}
}
return sum/scale;
}
最佳答案
在您的卷积中,有两件事可能不会导致崩溃。第一个是样式:您正在使用 x
迭代图像的行,我更像一个 y
位移,反之亦然。第二个是当你计算总和时,你没有重置变量 sum = 0
在评估每个像素的内核(内部两个循环)之前。相反,你积累 sum
在所有像素上,可能最终导致整数溢出。虽然严格来说这是 UB 并且可能导致崩溃,但这不是您面临的问题。
如果您确认崩溃发生在第一个像素( x = ksize/2
, y = ksize/2
),那么由于崩溃发生在从内核读取的第一个系数,我怀疑您可能已经通过了“错误的事情”作为kernel
.正如所介绍的,kernel
是 int**
.对于 3x3 的内核大小,这意味着要正确调用此函数,您必须在堆或堆栈上分配 int*
的数组。 ,您在其中存储了 3 个指向 int
的指针每个有 3 个系数的数组。如果您改为通过 int[3][3]
数组,卷积函数将尝试解释第一个或两个 int
在数组中作为指向 int
的指针当它不是时,并尝试取消引用它以引入系数。这很可能会导致段错误。
我也不知道你为什么要退回累计金额。这不是卷积的“传统”输出,但我推测您对输出图像的平均亮度感兴趣,这是合法的;在这种情况下,您应该使用单独且更宽的整数累加器( long
或 long long
),最后将其除以输出中的像素数。
您可能从 Internet 上找到了 PGM 数据结构,例如 here .请允许我放弃这个最佳实践建议。在我的领域(计算机视觉)中,选择的计算机视觉库 OpenCV 不会将矩阵表示为 row
的数组。指向 col
的缓冲区的指针元素。相反,分配了一大块内存,在这种情况下,大小为 image->row * image->col * sizeof(int)
至少,但经常 image->row * image->step * sizeof(int)
哪里image->step
是 image->col
四舍五入到下一个 4 或 16 的倍数。然后,只保留一个指针,一个指向整个图像底部的指针,尽管如果图像不连续,则必须保留一个额外的字段(步骤)。
因此,我将重新编写您的代码:
/* Includes */
#include <stdlib.h>
/* Defines */
#define min(a, b) (((a) < (b)) ? (a) : (b))
#define max(a, b) (((a) > (b)) ? (a) : (b))
/* Structure */
/**
* Mat structure.
*
* Stores the number of rows and columns in the matrix, the step size
* (number of elements to jump from one row to the next; must be larger than or
* equal to the number of columns), and a pointer to the first element.
*/
typedef struct Mat{
int rows;
int cols;
int step;
int* data;
} Mat;
/* Functions */
/**
* Allocation. Allocates a matrix big enough to hold rows * cols elements.
*
* If a custom step size is wanted, it can be given. Otherwise, an invalid one
* can be given (such as 0 or -1), and the step size will be chosen
* automatically.
*
* If a pointer to existing data is provided, don't bother allocating fresh
* memory. However, in that case, rows, cols and step must all be provided and
* must be correct.
*
* @param [in] rows The number of rows of the new Mat.
* @param [in] cols The number of columns of the new Mat.
* @param [in] step The step size of the new Mat. For newly-allocated
* images (existingData == NULL), can be <= 0, in
* which case a default step size is chosen; For
* pre-existing data (existingData != NULL), must be
* provided.
* @param [in] existingData A pointer to existing data. If NULL, a fresh buffer
* is allocated; Otherwise the given data is used as
* the base pointer.
* @return An allocated Mat structure.
*/
Mat allocMat(int rows, int cols, int step, int* existingData){
Mat M;
M.rows = max(rows, 0);
M.cols = max(cols, 0);
M.step = max(step, M.cols);
if(rows <= 0 || cols <= 0){
M.data = 0;
}else if(existingData == 0){
M.data = malloc(M.rows * M.step * sizeof(*M.data));
}else{
M.data = existingData;
}
return M;
}
/**
* Convolution. Convolves input by the given kernel (centered) and stores
* to output. Does not handle boundaries (i.e., in locations near the border,
* leaves output unchanged).
*
* @param [in] input The input image.
* @param [in] kern The kernel. Both width and height must be odd.
* @param [out] output The output image.
* @return Average brightness of output.
*
* Note: None of the image buffers may overlap with each other.
*/
int convolution(const Mat* input, const Mat* kern, Mat* output){
int i, j, x, y;
int coeff, data;
int sum;
int avg;
long long acc = 0;
/* Short forms of the image dimensions */
const int iw = input ->cols, ih = input ->rows, is = input ->step;
const int kw = kern ->cols, kh = kern ->rows, ks = kern ->step;
const int ow = output->cols, oh = output->rows, os = output->step;
/* Kernel half-sizes and number of elements */
const int kw2 = kw/2, kh2 = kh/2;
const int kelem = kw*kh;
/* Left, right, top and bottom limits */
const int l = kw2,
r = max(min(iw-kw2, ow-kw2), l),
t = kh2,
b = max(min(ih-kh2, oh-kh2), t);
/* Total number of pixels */
const int totalPixels = (r-l)*(b-t);
/* Input, kernel and output base pointers */
const int* iPtr = input ->data;
const int* kPtr = kern ->data + kw2 + ks*kh2;
int* oPtr = output->data;
/* Iterate over pixels of image */
for(y=t; y<b; y++){
for(x=l; x<r; x++){
sum = 0;
/* Iterate over elements of kernel */
for(i=-kh2; i<=kh2; i++){
for(j=-kw2; j<=kw2; j++){
data = iPtr[j + is*i + x];
coeff = kPtr[j + ks*i ];
sum += data * coeff;
}
}
/* Compute average. Add to accumulator and store as output. */
avg = sum / kelem;
acc += avg;
oPtr[x] = avg;
}
/* Bump pointers by one row step. */
iPtr += is;
oPtr += os;
}
/* Compute average brightness over entire output */
if(totalPixels == 0){
avg = 0;
}else{
avg = acc/totalPixels;
}
/* Return average brightness */
return avg;
}
/**
* Main
*/
int main(int argc, char* argv[]){
/**
* Coefficients of K. Binomial 3x3, separable. Unnormalized (weight = 16).
* Step = 3.
*/
int Kcoeff[3][3] = {{1, 2, 1}, {2, 4, 2}, {1, 2, 1}};
Mat I = allocMat(1920, 1080, 0, 0);/* FullHD 1080p: 1920x1080 */
Mat O = allocMat(1920, 1080, 0, 0);/* FullHD 1080p: 1920x1080 */
Mat K = allocMat( 3, 3, 3, &Kcoeff[0][0]);
/* Fill Mat I with something.... */
/* Convolve with K... */
int avg = convolution(&I, &K, &O);
/* Do something with O... */
/* Return */
return 0;
}
关于c - 高斯模糊、均值滤波器、卷积,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19413386/
我想获取每一行某些列的平均值。 我有此数据: w=c(5,6,7,8) x=c(1,2,3,4) y=c(1,2,3) length(y)=4 z=data.frame(w,x,y) 哪个返回:
类似于Numpy mean with condition我的问题将其扩展到对矩阵进行操作:计算矩阵 rdat 的行均值,跳过某些单元格 - 在本例中我使用 0 作为要跳过的单元格 - 就好像这些值从一
我有一个数据集,其中的列标题为产品名称、品牌、评级(1:5)、评论文本、评论有用性。我需要的是提出一个使用评论的推荐算法。我这里必须使用 python 进行编码。数据集采用.csv 格式。 为了识别数
我在 R^3 中有 n 个点,我想用 k 个椭球体或圆柱体覆盖它们(我不在乎;以更容易的为准)。我想大约最小化卷的并集。假设 n 是数万,k 是少数。开发时间(即简单性)比运行时更重要。 显然我可以运
我创建了一个计算均值、中位数和方差的程序。该程序最多接受 500 个输入。当有 500 个输入(我的数组的最大大小)时,我的所有方法都能完美运行。当输入较少时,只有“平均值”计算器起作用。这是整个程序
我已经完成了距离的计算并存储在推力 vector 中,例如,我有 2 个质心和 5 个数据点,我计算距离的方法是,对于每个质心,我首先计算 5 个数据点的距离并存储在阵列,然后与距离一维阵列中的另一个
下面的代码适用于每一列的总数,但我想计算出每个物种的平均值。 # Read data file into array data = numpy.genfromtxt('data/iris.csv',
我有一个独特的要求,我需要两个数据帧的公共(public)列(每行)的平均值。 我想不出这样做的 pythonic 方式。我知道我可以遍历两个数据框并找到公共(public)列,然后获取键匹配的行的平
我把它扔在那里,希望有人会尝试过这种荒谬的事情。我的目标是获取输入图像,并根据每个像素周围小窗口的标准差对其进行分割。基本上,这在数学上应该类似于高斯或盒式过滤器,因为它将应用于编译时(甚至运行时)用
有没有一种方法可以对函数进行向量化处理,使输出成为均值数组,其中每个均值代表输入数组的 0 索引值的均值?循环这个非常简单,但我正在努力尽可能高效。例如0 = 均值(0),1 = 均值(0-1),N
我正在尝试生成均值为 1 的指数分布随机数。我知道如何获取具有均值和标准差的正态分布随机数。我们可以通过normal(mean, standard_deviation)得到它,但是我不知道如何得到指数
我遇到了一段 Python 代码,它的内容类似于以下内容: a = np.array([1,2,3,4,5,6,7]) a array([1, 2, 3, 4, 5, 6, 7]) np.mean(a
我有两个数组。 x 是独立变量,counts 是 x 出现的次数,就像直方图一样。我知道我可以通过定义一个函数来计算平均值: def mean(x,counts): return np.sum
我有在纯 python 中计算平均速度的算法: speed = [...] avg_speed = 0.0 speed_count = 0 for i in speed: if i > 0:
我正在尝试计算扩展窗口的平均值,但是数据结构使得之前的答案至少缺少一点所需的内容(最接近的是:link)。 我的数据看起来像这样: Company TimePeriod IndividualID
我正在尝试实现 Kmeans python中的算法将使用cosine distance而不是欧几里得距离作为距离度量。 我知道使用不同的距离函数可能是致命的,应该小心使用。使用余弦距离作为度量迫使我改
有谁知道自组织映射 (SOM) 与 k 均值相比效果如何?我相信通常在颜色空间(例如 RGB)中,SOM 是将颜色聚类在一起的更好方法,因为视觉上不同的颜色之间的颜色空间存在重叠( http://ww
注意:我希望能得到更多有关如何处理和提出此类解决方案的指南,而不是解决方案本身。 我的系统中有一个非常关键的功能,它在特定上下文中显示为排名第一的分析热点。它处于 k-means 迭代的中间(已经是多
我有一个 pandas 数据框,看起来像这样: 给定行中的每个值要么是相同的数字,要么是 NaN。我想计算数据框中所有两列组合的平均值、中位数和获取计数,其中两列都不是 NaN。 例如,上述数据帧的结
任何人都知道如何调整简单的 K 均值算法来处理 this form 的数据集. 最佳答案 在仍然使用 k-means 的同时处理该形式的数据的最直接方法是使用 k-means 的内核化版本。 JSAT
我是一名优秀的程序员,十分优秀!