Exemplo n.º 1
0
void Convolve(CImageOf<T> src, CImageOf<T>& dst,
              CFloatImage kernel)
{
    // Determine the shape of the kernel and source image
    CShape kShape = kernel.Shape();
    CShape sShape = src.Shape();

    // Allocate the result, if necessary
    dst.ReAllocate(sShape, false);
    if (sShape.width * sShape.height * sShape.nBands == 0)
        return;

    // Do the convolution
    for (int y = 0; y < sShape.height; y++)
		for (int x = 0; x < sShape.width; x++)
			for (int c = 0; c < sShape.nBands; c++)
			{
				double sum = 0;
				for (int k = 0; k < kShape.height; k++)
					for (int l = 0; l < kShape.width; l++)
						if ((x-kernel.origin[0]+k >= 0) && (x-kernel.origin[0]+k < sShape.width) && (y-kernel.origin[1]+l >= 0) && (y-kernel.origin[1]+l < sShape.height))
							sum += kernel.Pixel(k,l,0) * src.Pixel(x-kernel.origin[0]+k,y-kernel.origin[1]+l,c);
				dst.Pixel(x,y,c) = (T) __max(dst.MinVal(), __min(dst.MaxVal(), sum));
			}
}
Exemplo n.º 2
0
void WarpLocal(CImageOf<T> src, CImageOf<T>& dst,
               CFloatImage uv, bool relativeCoords,
               EWarpInterpolationMode interp, float cubicA)
{
    // Check that dst is of the right shape
    CShape sh(uv.Shape().width, uv.Shape().height, src.Shape().nBands);
    dst.ReAllocate(sh);

    // Allocate a row buffer for coordinates
    int n = sh.width;
    std::vector<float> rowBuf;
    rowBuf.resize(n*2);

    // Precompute the cubic interpolant
    if (interp == eWarpInterpCubic)
        InitializeCubicLUT(cubicA);

    // Process each row
    for (int y = 0; y < sh.height; y++)
    {
        float *uvP  = &uv .Pixel(0, y, 0);
        float *xyP  = (relativeCoords) ? &rowBuf[0] : uvP;
        T *dstP     = &dst.Pixel(0, y, 0);

        // Convert to absolute coordinates if necessary
        if (relativeCoords)
        {
            for (int x = 0; x < n; x++)
            {
                xyP[2*x+0] = x + uvP[2*x+0];
                xyP[2*x+1] = y + uvP[2*x+1];
            }
        }

        // Resample the line
        WarpLine(src, dstP, xyP, n, sh.nBands, interp, src.MinVal(), src.MaxVal());
    }
}
Exemplo n.º 3
0
void Convolve(CImageOf<T> src, CImageOf<T>& dst,
              CFloatImage kernel)
{
    // Allocate the result, if necessary
    dst.ReAllocate(src.Shape(), false);

    // Create the IPL images
    IplImage* srcImage = IPLCreateImage(src);
    IplImage* dstImage = IPLCreateImage(dst);

    // Call the convolution code
    if (typeid(T) == typeid(float)) {
        IplConvKernelFP* iplKernel = IPLConvKernelFP(kernel);
        iplConvolve2DFP(srcImage, dstImage, &iplKernel, 1, IPL_SUM);
        iplDeleteConvKernelFP(iplKernel);
    } else {
        IplConvKernel* iplKernel = IPLConvKernel(kernel);
        iplConvolve2D(srcImage, dstImage, &iplKernel, 1, IPL_SUM);
        iplDeleteConvKernel(iplKernel);
    }
    iplDeallocate(srcImage, IPL_IMAGE_HEADER);
    iplDeallocate(dstImage, IPL_IMAGE_HEADER);
}
Exemplo n.º 4
0
void testGetFloatFromMatrix()
{
	CImageOf<double> img = GetImageFromMatrix((double *)sobelX, 3, 3);
	bool areSame = true;	
	for (int i = 0; i < 3; i++)
	{
		for (int j = 0; j < 3; j++)
		{
			float imgPixel = (float)img.Pixel(j, i, 0);
			float sobelPixel = (float)sobelX[i * 3 + j];

			if (img.Pixel(j, i, 0) != sobelX[i * 3 + j])
			{
				areSame = false;
			}
		}
	}

	if (areSame)
		printf("Same\n");
	else
		printf("Not the same\n");
}
Exemplo n.º 5
0
void ConvolveSeparable(CImageOf<T> src, CImageOf<T>& dst,
                       CFloatImage x_kernel, CFloatImage y_kernel,
                       int subsample)
{
    // Allocate the result, if necessary
    CShape dShape = src.Shape();
    if (subsample > 1) {
        dShape.width  = (dShape.width  + subsample - 1) / subsample;
        dShape.height = (dShape.height + subsample - 1) / subsample;
    }
    dst.ReAllocate(dShape, false);

    // Allocate the intermediate (or final) result
    CImageOf<T> tmpImg;
    if (subsample > 1)
        tmpImg.ReAllocate(src.Shape());
    CImageOf<T>& tmp = (subsample > 1) ? tmpImg : dst;

    // Create the IPL images
    IplImage* srcImage = IPLCreateImage(src);
    IplImage* dstImage = IPLCreateImage(tmp);
    srcImage->alphaChannel = 0;     // convolve the A channel also
    dstImage->alphaChannel = 0;     // convolve the A channel also

    // Call the convolution code
    if (typeid(T) == typeid(float)) {
        IplConvKernelFP* xKernel = IPLConvKernelFP(x_kernel, false);
        IplConvKernelFP* yKernel = IPLConvKernelFP(y_kernel, true);
        iplConvolveSep2DFP(srcImage, dstImage, xKernel, yKernel);
        iplDeleteConvKernelFP(xKernel);
        iplDeleteConvKernelFP(yKernel);
    } else {
        IplConvKernel* xKernel = IPLConvKernel(x_kernel, false);
        IplConvKernel* yKernel = IPLConvKernel(y_kernel, true);
        iplConvolveSep2D(srcImage, dstImage, xKernel, yKernel);
        iplDeleteConvKernel(xKernel);
        iplDeleteConvKernel(yKernel);
    }
    iplDeallocate(srcImage, IPL_IMAGE_HEADER);
    iplDeallocate(dstImage, IPL_IMAGE_HEADER);

    // Downsample if necessary
    if (subsample > 1) {
        for (int y = 0; y < dShape.height; y++) {
            T* sPtr = &tmp.Pixel(0, y * subsample, 0);
            T* dPtr = &dst.Pixel(0, y, 0);
            int nB  = dShape.nBands;
            for (int x = 0; x < dShape.width; x++) {
                for (int b = 0; b < nB; b++)
                    dPtr[b] = sPtr[b];
                sPtr += subsample * nB;
                dPtr += nB;
            }
        }
    }
}
Exemplo n.º 6
0
static void FillRowBuffer(T buf[], CImageOf<T>& src, CFloatImage& kernel, int k, int n)
{
    // Compute the real row address
    CShape sShape = src.Shape();
    int nB = sShape.nBands;
    int k0 = TrimIndex(k + kernel.origin[1], src.borderMode, sShape.height);
    if (k0 < 0)
    {
        memset(buf, 0, n * sizeof(T));
        return;
    }

    // Fill the row
    T* srcP = &src.Pixel(0, k0, 0);
    int m = n / nB;
    for (int l = 0; l < m; l++, buf += nB)
    {
        int l0 = TrimIndex(l + kernel.origin[0], src.borderMode, sShape.width);
        if (l0 < 0)
            memset(buf, 0, nB * sizeof(T));
        else
            memcpy(buf, &srcP[l0*nB], nB * sizeof(T));
    }
}
Exemplo n.º 7
0
void ConvolveSeparable(CImageOf<T> src, CImageOf<T>& dst,
                       CFloatImage x_kernel, CFloatImage y_kernel,
                       float scale, float offset,
                       int decimate, int interpolate)
{
    // Allocate the result, if necessary
    CShape dShape = src.Shape();
    if (decimate > 1)
    {
        dShape.width  = (dShape.width  + decimate-1) / decimate;
        dShape.height = (dShape.height + decimate-1) / decimate;
    }
    dst.ReAllocate(dShape, false);

    // Allocate the intermediate images
    CImageOf<T> tmpImg1(src.Shape());
    //CImageOf<T> tmpImgCUDA(src.Shape());
    CImageOf<T> tmpImg2(src.Shape());

    // Create a proper vertical convolution kernel
    CFloatImage v_kernel(1, y_kernel.Shape().width, 1);
    for (int k = 0; k < y_kernel.Shape().width; k++)
        v_kernel.Pixel(0, k, 0) = y_kernel.Pixel(k, 0, 0);
    v_kernel.origin[1] = y_kernel.origin[0];

#ifdef RUN_ON_GPU
    // Modifications for integrating CUDA kernels
    BinomialFilterType type;

    profilingTimer->startTimer();

    // CUDA Convolve
    switch (x_kernel.Shape().width)
    {
       case 3:
           type = BINOMIAL6126;
           break;
       case 5:
           type = BINOMIAL14641;
           break;
       default:
           // Unsupported kernel case
           throw CError("Convolution kernel Unknown");
           assert(false);
    }

    // Skip copy if decimation is not required
    if (decimate != 1) CudaConvolveXY(src, tmpImg2, type); 
    else CudaConvolveXY(src, dst, type);

    printf("\nGPU convolution time = %f ms\n", profilingTimer->stopAndGetTimerValue());
#else

    profilingTimer->startTimer();
    //VerifyComputedData(&tmpImg2.Pixel(0, 0, 0), &tmpImgCUDA.Pixel(0, 0, 0), 7003904);

    // Perform the two convolutions
    Convolve(src, tmpImg1, x_kernel, 1.0f, 0.0f);
    Convolve(tmpImg1, tmpImg2, v_kernel, scale, offset);

    printf("\nCPU Convolution time = %f ms\n", profilingTimer->stopAndGetTimerValue());
#endif

    profilingTimer->startTimer();
    // Downsample or copy
    // Skip decimate and recopy if not required
#ifdef RUN_ON_GPU
    if (decimate != 1)
    {
#endif
       for (int y = 0; y < dShape.height; y++)
       {
           T* sPtr = &tmpImg2.Pixel(0, y * decimate, 0);
           T* dPtr = &dst.Pixel(0, y, 0);
           int nB  = dShape.nBands;
           for (int x = 0; x < dShape.width; x++)
           {
               for (int b = 0; b < nB; b++)
                   dPtr[b] = sPtr[b];
               sPtr += decimate * nB;
               dPtr += nB;
           }
       }
#ifdef RUN_ON_GPU
    }
#endif
    printf("\nDecimate/Recopy took = %f ms\n", profilingTimer->stopAndGetTimerValue());
}
Exemplo n.º 8
0
extern void ForwardWarp(CImageOf<T>& src, CImageOf<T>& dst, CFloatImage& disp,
                        float d_scale, bool line_interpolate, float disp_gap)
{
    // Warps src into dst using disparities disp.
    //  Each disparity is scaled by d_scale
    // Note that "empty" pixels are left at their original value

    CShape sh = src.Shape();
    int w = sh.width, h = sh.height, n_bands = sh.nBands;
    float round_offset = (typeid(T) == typeid(float)) ? 0.0f : 0.5f;

    if (! sh.SameIgnoringNBands(disp.Shape()))
        throw CError("ForwardWarp: disparity image has wrong size");

    if (sh != dst.Shape())
        dst.ReAllocate(sh);
    
    // Optional clipping (if necessary)
    CFloatImage flt;
    T minVal = dst.MinVal();
    T maxVal = dst.MaxVal();
    if (minVal <= flt.MinVal() && maxVal >= flt.MaxVal())
        minVal = maxVal = 0;

    for (int y = 0; y < h; y++)
    {
        // determine correct warping direction
        int xstart = (d_scale>0 ? 0   : w-1);
        int xend   = (d_scale>0 ? w   : -1  );
        int xincr  = (d_scale>0 ? 1   : -1  );
        
        float *dp = &disp.Pixel(0, y, 0);
        T *ps = &src .Pixel(0, y, 0);
        T *pd = &dst .Pixel(0, y, 0);

        for (int x = xstart; x != xend; x += xincr)
        {
            // determine if a line should be drawn
            int x2 = x + xincr;
            float d_diff = fabs(dp[x] - dp[x2]);
            bool draw_line = line_interpolate && (x2 != xend) &&
                 (d_diff < disp_gap);

            // scaled disparity:
            float d = d_scale * dp[x];

            // line drawing
            if (draw_line)
            {
                float d2 = d_scale * dp[x2];

                if (xincr > 0)
                    draw_intensity_line(&ps[x * n_bands], &ps[x2 * n_bands], pd,
                                        x - d, x2 - d2, w, n_bands, round_offset,
                                        minVal, maxVal);
                else
                    draw_intensity_line(&ps[x2 * n_bands], &ps[x * n_bands], pd,
                                        x2 - d, x - d2, w, n_bands, round_offset,
                                        minVal, maxVal);
                continue;
            }
            
            // splatting
            int xx = x - ROUND(d);
            if (xx >= 0 && xx < w)
                memcpy(&pd[xx * n_bands], &ps[x * n_bands],
                       n_bands*sizeof(T));
        }
    }
}
Exemplo n.º 9
0
extern void InverseWarp(CImageOf<T>& src, CImageOf<T>& dst, CFloatImage& disp,
                        float d_scale, float disp_gap, int order)
{
    // Warps src into dst using disparities disp.
    //  Each disparity is scaled by d_scale
    // Note that "empty" pixels are left at their original value

    CShape sh = src.Shape();
    int w = sh.width, h = sh.height, n_bands = sh.nBands;
    int n = w * n_bands;

    if (! sh.SameIgnoringNBands(disp.Shape()))
        throw CError("InverseWarp: disparity image has wrong size");

    if (sh != dst.Shape())
        dst.ReAllocate(sh);

    // Optional forward warped depth map if checking for visibility
    CFloatImage fwd, fwd_tmp;
    if (disp_gap > 0.0f)
    {
        ScaleAndOffset(disp, fwd_tmp, d_scale, 0.0f);
        fwd.ReAllocate(disp.Shape());
        fwd.FillPixels(-9999.0f);
        ForwardWarp(fwd_tmp, fwd, disp, d_scale, true, disp_gap);
    }

    // Allocate line buffers
    std::vector<float> src_buf, dst_buf, dsp_buf;
    src_buf.resize(n);
    dst_buf.resize(n);
    dsp_buf.resize(n);
    CFloatImage fimg;   // dummy, used for MinVal(), MaxVal()

    for (int y = 0; y < h; y++)
    {
        // Set up (copy) the line buffers
        ScaleAndOffsetLine(&src .Pixel(0, y, 0), &src_buf[0], n,
                           1.0f,       0.0f, fimg.MinVal(), fimg.MaxVal());
        ScaleAndOffsetLine(&disp.Pixel(0, y, 0), &dsp_buf[0], w,
                           d_scale, 0.0f, 0.0f, 0.0f);
        ScaleAndOffsetLine(&dst .Pixel(0, y, 0), &dst_buf[0], n,
                           1.0f,       0.0f, fimg.MinVal(), fimg.MaxVal());

        // Forward warp the depth map
        float *fwd_buf = (disp_gap > 0.0f) ? &fwd.Pixel(0, y, 0) : 0;

        // Process (warp) the line
        InverseWarpLine(&src_buf[0], &dst_buf[0], &dsp_buf[0],
                        w, n_bands, order, fwd_buf, disp_gap);

        // Convert back to native type
        T minVal = dst.MinVal();
        T maxVal = dst.MaxVal();
        float offset = (typeid(T) == typeid(float)) ? 0.0f : 0.5;   // rounding
        if (minVal <= fimg.MinVal() && maxVal >= fimg.MaxVal())
            minVal = maxVal = 0;
        ScaleAndOffsetLine(&dst_buf[0], &dst.Pixel(0, y, 0), n,
                           1.0f, offset, minVal, maxVal);
    }
}