권적과 동적 기획의 간단한 테스트 프로그램

19204 단어 화상 처리
// convolutionTest.cpp :              。
//

#include "stdafx.h"
#include 
#include 
#include 
#ifndef min
#define min(v1, v2) ((v1) < (v2) ? (v1) : (v2))
#endif
#ifndef max
#define max(v1, v2) ((v1) > (v2) ? (v1) : (v2))
#endif
/************************
src    channel     
des             , channel       
model        
srcW        
srcH        
desW        
desH        
modW      
modH      
offsetX                 offsetX < modW - 1
offsetY                  offsetY < modH - 1
Dim 
************************/
int cpuConvolution(float * src, float * des, float * model, int srcW, int srcH, int desW, int desH, int modW, int modH, int offsetX, int offsetY, int channel)
{
    int desValW = srcW - modW + 1;
    int desValH = srcH - modH + 1;

    int desValIndexX;
    int desValIndexY;

    int i, j, k;
    int m, n;

    for( i = 0; i < desH; i ++)
    {

        for(j = 0; j < desW; j ++)
        {
            desValIndexX = j - offsetX;
            desValIndexY = i - offsetY;
            if(i < offsetY || i >= offsetY + desValH || j < offsetX || j >= offsetX + desValW)
            {

                des[(i * desW + j)] = 0.0f;

            }else
            {
                for(m = 0; m < modH; m ++)
                {
                    for(n = 0; n < modW; n ++)
                    {
                        for(k = 0; k < channel; k ++)
                        {
                            des[(i * desW + j)] += src[((desValIndexY + m) * srcW + (desValIndexX + n)) * channel + k] * model[ (m * modW + n) * channel + k];
                        }
                    }
                }

            }
        }
    }

    return 0;
}
void dt1d(const float *src, float *dst, int *ptr, 
          int step, int n, float a, float b, float range,
          int *v, float *z, float *t) 
{
  int k     = 0;  
  v[0]      = 0; 
  z[0]      = -10000.0f;  // the first parabola's left boundary
  z[1]      = +10000.0f;  // the second parabola's right boundary
  float eps = 0.00001f;
  float a_inv = 1/a;

  for (int q = 1; q <= n-1; q++) 
  {
    // compute unbounded point of intersection
      float s = 0.5f * ((src[q*step] - src[v[k]*step]) * t[q - v[k]]  + q + v[k]  - b * a_inv);


    s = min(v[k]+range+eps, max(q-range-eps, s));
    while (s <= z[k]) 
    {
      // delete dominiated parabola
      k--;
      s = 0.5f * ((src[q*step] - src[v[k]*step]) * t[q - v[k]] 
                  + q + v[k] 
                  - b * a_inv);
      s = min(v[k]+range+eps, max(q-range-eps, s));
    }
    k++;
    v[k]   = q;
    z[k]   = s;

  }
  z[k+1] = 10000.0f;

  k = 0;
  for (int q = 0; q <= n-1; q++) 
  {
    while (z[k+1] < q)
      k++;
    dst[q*step] = a*(q-v[k])*(q-v[k]) + b*(q-v[k]) + src[v[k]*step];
    ptr[q*step] = v[k];
  }
}

void dynamicAll(float * src, float * desDynamic, int * desQx, int * desQy, int desW, int desH, float a, float b, float range)
{
       int * buffer_v = (int *)malloc(desW * sizeof(int));
       float * buffer_z = (float *)malloc((desW + 1) * sizeof(float));
       float * buffer_t = (float *)malloc(desW * sizeof(float));
       float * tempDynamic = (float *)malloc(desW * desH * sizeof(float));




        buffer_t[0] = 10000.0f;
        for (int i = 1; i < desW; i++)
        {
            buffer_t[i] = 1 / ((-a) * i);
        }
        for(int i = 0; i < desW; i ++)
        {
            dt1d(src + i, tempDynamic + i, desQy + i, desW, desH, a, b, range, buffer_v, buffer_z, buffer_t);
        }
        for(int i = 0; i < desH; i ++)
        {
            dt1d(tempDynamic + i * desW, desDynamic + i * desW, desQx + i * desW, 1, desW, a, b, range, buffer_v, buffer_z, buffer_t);

        }
        free(buffer_v);
        free(buffer_t);
        free(buffer_z);

}
//            
void tidyQxQy(int * srcX, int * srcY, int width, int height)
{
    int * temp = (int *)malloc(width * height * sizeof(int));
    memcpy(temp, srcY, width * height * sizeof(int));

    int i, j;
    for(i = 0; i < height; i ++)
    {
        for(j = 0; j < width; j ++)
        {
            int tempIndex = srcX[i * width + j];
            srcY[i * width + j] = temp[i * width + tempIndex];
        }
    }
    free(temp);
}
int _tmain(int argc, _TCHAR* argv[])
{
    int srcW = 15;
    int srcH = 10;
    int modW = 3;
    int modH = 3;
    int desW = 20;
    int desH = 15;

    float * src = (float *)malloc(srcW * srcH * sizeof(float) * 2);
    float * mod = (float *)malloc(modW * modH * sizeof(float) * 2);
    float * des = (float *)malloc(desW * desH * sizeof(float));
    float * dynamic = (float *)malloc(desW * desH * sizeof(float));
    int * desQx = (int *)malloc(desW * desH * sizeof(int));
    int * desQy = (int *)malloc(desW * desH * sizeof(int));


    memset(dynamic, 0, desW * desH * sizeof(float));
    memset(desQx, 0, desW * desH * sizeof(int));
    memset(desQy, 0, desW * desH * sizeof(int));

    memset(src, 0, srcW * srcH * sizeof(float));
    memset(des, 0, desW * desH * sizeof(float));


    memset(mod, 0, modW * modH * sizeof(float));
    for(int i = 0; i < srcH; i ++)
    {
        for(int j = 0; j < srcW * 2; j ++)
        {
            src[i * srcW * 2 + j] = 2.0f;
            printf("%f ",src[i * srcW * 2 + j]);
        }
        printf("%f
"
, src[i * srcW * 2 + srcW - 1]); } for(int i = 0; i < modH; i ++) { for(int j = 0; j < modW * 2; j ++) { mod[i * modW * 2 + j] = 1.5f; } } cpuConvolution(src, des, mod, srcW, srcH, desW, desH, modW, modH, 3, 3, 2); printf("
"
); for(int i = 0; i < desH; i ++) { for(int j = 0; j < desW - 1; j ++) { printf("%f ",des[i * desW + j]); } printf("%f
"
, des[i * desW + desW - 1]); } int * dumpCpuQnx = (int *)malloc(desW * desH * sizeof(int)); int * dumpCpuQny = (int *)malloc(desW * desH * sizeof(int)); float a = 2.0f; float b = 1.0f; dynamicAll(des, dynamic, dumpCpuQnx, dumpCpuQny, desW, desH, a, b, 2.0f); printf("
"
); for(int i = 0; i < desH; i ++) { for(int j = 0; j < desW; j ++) { printf("%f ",dynamic[i * desW + j]); } printf("
"
); } printf("
"
); for(int i = 0; i < desH; i ++) { for(int j = 0; j < desW; j ++) { printf("%d ",dumpCpuQnx[i * desW + j]); } printf("
"
); } printf("
"
); for(int i = 0; i < desH; i ++) { for(int j = 0; j < desW; j ++) { printf("%d ",dumpCpuQny[i * desW + j]); } printf("
"
); } tidyQxQy(dumpCpuQnx, dumpCpuQny, desW, desH); printf("
"
); for(int i = 0; i < desH; i ++) { for(int j = 0; j < desW; j ++) { printf("%d ",dumpCpuQny[i * desW + j]); } printf("
"
); } free(src); free(des); free(mod); system("pause"); return 0; }

좋은 웹페이지 즐겨찾기