wkr145 2019-05-13 22:40
浏览 328

C++代码优化,以缩短时间为先(仅从代码分析)图片无法由于格式无法上传。麻烦

#include "stdio.h"
#include "stdlib.h"
#include "math.h"
#include
#include

#define SUPPORT_SIZE 2 // 高斯滤波窗口半径
#define IM_WIDTH 1920 // 输入图像宽和高
#define IM_HEIGHT 1080

// 计算高斯系数
void calGaussCoef(double *pGaussCoef)
{
int i, j, k;
int wlen = (SUPPORT_SIZE<<1) + 1;
double sum = 0;

if(pGaussCoef)
{
    for(i = -SUPPORT_SIZE; i <= SUPPORT_SIZE; ++i)
    {
        for(j = -SUPPORT_SIZE; j <= SUPPORT_SIZE; ++j)
        {
            //计算高斯系数,2.71828为自然数e
            pGaussCoef[ (i+SUPPORT_SIZE)*wlen + j+SUPPORT_SIZE ] = pow( 2.71828, (-double(i*i + j*j)/25) );
        }
    }

    for(k = 0; k < wlen*wlen; ++k)
    {
        sum += pGaussCoef[k];
    }

    for(k = 0; k < wlen*wlen; ++k)  //进行归一化处理
    {
        pGaussCoef[k] /= sum;
    }
}

}

// 2维卷积函数,实现2维图像高斯滤波
// pGaussCoef为高斯系数
// pSrcImg为原始图像
// pDstImg为滤波后图像
// height:图像高度
// width:图像宽度
// supportSize:高斯滤波窗口半径,直径为2*supportSize + 1
void conv2D(double *pGaussCoef, unsigned char *pSrcImg, unsigned char *pDstImg, int height, int width, int supportSize)
{
int i, j;
int m, n;
int indexI, indexG;
int sw = supportSize;
int slen = sw*2 + 1;
double sum;
double *pTemp = NULL;
pTemp = new double[height*width]; //图像面积

calGaussCoef(pGaussCoef);

for(i = sw;  i < height - sw;  ++i) //  sw=2 height=1080 width=1920         i=2;i<1078;i++
{
    for(j = sw; j < width - sw; ++j)// j=2;l<1918;j++
    {
        sum = 0;

        for(m = -sw; m <= sw; ++m) // m=-2;m<2;m++
        {   
            for(n = -sw; n <= sw; ++n)
            {

                indexI = (i+m)*width + j+n;
                indexG = (m+sw)*slen/*直径*/ + n+sw;
                sum += pSrcImg[indexI] * pGaussCoef[indexG];
            }
        }
        pDstImg[i*width + j] = (unsigned char)(sum);
    }
}

}

int main()
{
int wlen, imLen;
time_t start, end;
double *pGaussCoef = NULL;
unsigned char *pSrc = NULL, *pDst = NULL;
unsigned char buffer[1078]; //定义BMP图像文件头部缓冲区1078
FILE *fin, *fout;

if( !(fin=fopen("in.bmp","rb")) )   //打开原始输入图像
{
    printf("Open file %s error!\n","in.bmp");
    exit(1);
}
if( !(fout=fopen("out.bmp","wb")) ) //创建滤波后输出图像
{   
    printf("Open file %s error!\n","out.bmp");
    exit(1);
}

wlen    = SUPPORT_SIZE * 2 + 1;
imLen   = IM_WIDTH * IM_HEIGHT;

pGaussCoef = new double[wlen*wlen];
pSrc = new unsigned char[IM_WIDTH*IM_HEIGHT];
pDst = new unsigned char[IM_WIDTH*IM_HEIGHT];

fread(buffer, 1, 1078, fin);    //读取1078字节BMP图像文件头信息
fread(pSrc, sizeof(unsigned char), imLen, fin); //从输入文件读入图像数据


start = clock();
conv2D(pGaussCoef, pSrc, pDst, IM_HEIGHT, IM_WIDTH, SUPPORT_SIZE);  //执行滤波
end = clock();
printf("running time is %d\n", end - start);

fwrite(buffer, sizeof(unsigned char), 1078, fout);  
fwrite(pDst, sizeof(unsigned char), imLen, fout);   //将滤波结果写到新文件

delete[] pGaussCoef;
delete[] pSrc;
delete[] pDst;
fclose(fin);
fclose(fout);

}

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 delta降尺度计算的一些细节,有偿
    • ¥15 Arduino红外遥控代码有问题
    • ¥15 数值计算离散正交多项式
    • ¥30 数值计算均差系数编程
    • ¥15 redis-full-check比较 两个集群的数据出错
    • ¥15 Matlab编程问题
    • ¥15 训练的多模态特征融合模型准确度很低怎么办
    • ¥15 kylin启动报错log4j类冲突
    • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
    • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序