⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 iplimage.cpp

📁 一个语言识别引擎
💻 CPP
📖 第 1 页 / 共 4 页
字号:
            float tmp;
            float *source = (float *)srcImage->imageData;
            //float *dest = (float *)dstImage->imageData;
            for (int i = bordery; i <  h - bordery; i++)
                {
                    for (int j = borderx; j < w - borderx; j++)
                        {
                            tmp = 0;
                            for (int k = 0; k < krows; k++)
                                for (int l = 0; l < kcols; l++)
                                    {
                                        tmp += source[(i + k - bordery) * w + j + l - borderx] 
                                            * values[ksize - k * kcols - l - 1];
                                    }
                            __tmp_res[i * w + j] = tmp;
                        }
                }

            memcpy (dstImage->imageData, __tmp_res, dstImage->imageSize);
        }
}

// TODO: inplace operation.
IPLAPIIMPL(void, iplConvolveSep2DFP,(IplImage* srcImage,
                                     IplImage* dstImage,
                                     IplConvKernelFP* xKernel,
                                     IplConvKernelFP* yKernel))
{
	// here too I need memory to support inplace operations.
	static float *__tmp_res = NULL;
	static int __tmp_size = -1;

	if (xKernel != NULL)
        {
            assert (xKernel->nRows == 1);
            assert ((xKernel->nCols % 2) != 0);
        }
	if (yKernel != NULL)
        {
            assert (yKernel->nCols == 1);
            assert ((yKernel->nRows % 2) != 0);
        }

	// do not consider anchor, borders are not set, and assumes
	// that the kernel has odd dimensions (x only).
	float *xvalues = (xKernel != NULL) ? xKernel->values : NULL;
	const int xksize = (xKernel != NULL) ? xKernel->nCols : 0;
	float *yvalues = (yKernel != NULL) ? yKernel->values : NULL;
	const int yksize = (yKernel != NULL) ? yKernel->nRows : 0;

	const int borderx = (xKernel != NULL) ? xKernel->nCols / 2 : 0;
	const int bordery = (yKernel != NULL) ? yKernel->nRows / 2 : 0;
	const int w = srcImage->width;
	const int h = srcImage->height;

	assert (compareHeader (srcImage, dstImage));
	assert (srcImage->nChannels == 1);	// Mono images only.
	assert (srcImage->depth == IPL_DEPTH_32F);

	if (__tmp_res == NULL)
        {
            __tmp_size = dstImage->imageSize / sizeof(float);
            ///__tmp_res = new float[dstImage->imageSize / sizeof(float)];
            __tmp_res = AllocAligned<float> (dstImage->imageSize / sizeof(float));
            assert (__tmp_res != NULL);
        }
	else
        {
            if (__tmp_size < int(dstImage->imageSize / sizeof(float)))
                {
                    // new size.
                    ///delete[] __tmp_res;
                    FreeAligned<float> (__tmp_res);
                    __tmp_size = dstImage->imageSize / sizeof(float);
                    ///__tmp_res = new float[dstImage->imageSize / sizeof(float)];
                    __tmp_res = AllocAligned<float> (dstImage->imageSize / sizeof(float));
                    assert (__tmp_res != NULL);
                }
        }

	// inplace.
	float *src = (float *)srcImage->imageData;
	float *dst = (float *)dstImage->imageData;
	if (xKernel != NULL)
        {
            // apply x kernel.
            float tmp;
            for (int i = 0; i <  h; i++)
                {
                    for (int j = borderx; j < w - borderx; j++)
                        {
                            tmp = 0;
                            for (int k = 0; k < xksize; k++)
                                {
                                    tmp += src[i * w + j + k - borderx] 
                                        * xvalues[xksize - k - 1];
                                }
                            __tmp_res[i * w + j] = tmp;
                        }
                }
        }

	if (yKernel != NULL)
        {
            // apply y kernel.
            float tmp;
            for (int i = bordery; i <  h - bordery; i++)
                {
                    // can save borderx (if applied)!
                    for (int j = borderx; j < w - borderx; j++)
                        {
                            tmp = 0;
                            for (int k = 0; k < yksize; k++)
                                {
                                    tmp += __tmp_res[(i + k - bordery) * w + j] 
                                        * yvalues[yksize - k - 1];
                                }
                            dst[i * w + j] = tmp;
                        }
                }
        }
}

/*
  IPLAPIIMPL(IPLStatus, iplFixedFilter,(IplImage* srcImage, IplImage* dstImage,
  IplFilter filter))
  {
  // NOT IMPLEMENTED YET.
  assert (implemented_yet == 0);
  return -1;
  }
*/

IPLAPIIMPL(void, iplConvolveSep2D,(IplImage* srcImage, IplImage* dstImage,
                                   IplConvKernel* xKernel, IplConvKernel* yKernel))
{
	// in place stuff.
	static char *__tmp_res = NULL;
	static int __tmp_size = -1;

	if (xKernel != NULL)
        {
            assert (xKernel->nRows == 1);
            assert ((xKernel->nCols % 2) != 0);
        }
	if (yKernel != NULL)
        {
            assert (yKernel->nCols == 1);
            assert ((yKernel->nRows % 2) != 0);
        }

	// do not consider anchor, borders are not set, and assumes
	// that the kernel has odd dimensions (x only).
	int *xvalues = (xKernel != NULL) ? xKernel->values : NULL;
	const int xksize = (xKernel != NULL) ? xKernel->nCols : 0;
	int *yvalues = (yKernel != NULL) ? yKernel->values : NULL;
	const int yksize = (yKernel != NULL) ? yKernel->nRows : 0;

	const int borderx = (xKernel != NULL) ? xKernel->nCols / 2 : 0;
	const int bordery = (yKernel != NULL) ? yKernel->nRows / 2 : 0;
	const int w = srcImage->width;
	const int h = srcImage->height;

	assert (compareHeader (srcImage, dstImage));
	assert (srcImage->nChannels == 1);	// Mono images only.

	if (__tmp_res == NULL)
        {
            __tmp_size = dstImage->imageSize;
            ///__tmp_res = new char[dstImage->imageSize];
            __tmp_res = AllocAligned<char> (dstImage->imageSize);
            assert (__tmp_res != NULL);
        }
	else
        {
            if (__tmp_size < dstImage->imageSize)
                {
                    // new size.
                    FreeAligned<char> (__tmp_res);
                    ///delete[] __tmp_res;
                    __tmp_size = dstImage->imageSize;
                    ///__tmp_res = new char[dstImage->imageSize];
                    __tmp_res = AllocAligned<char> (dstImage->imageSize);
                    assert (__tmp_res != NULL);
                }
        }
	
	switch (srcImage->depth)
        {
        case IPL_DEPTH_8U:
            {
                if (xKernel != NULL)
                    {
                        // apply x kernel.
                        int tmp;
                        for (int i = 0; i <  h; i++)
                            {
                                for (int j = borderx; j < w - borderx; j++)
                                    {
                                        tmp = 0;
                                        for (int k = 0; k < xksize; k++)
                                            {
                                                tmp += srcImage->imageData[i * w + j + k - borderx] 
                                                    * xvalues[xksize - k - 1];
                                            }
                                        tmp >>= xKernel->nShiftR;
                                        if (tmp > 255)
                                            tmp = 255;
                                        else
                                            if (tmp < 0)
                                                tmp = 0;
                                        __tmp_res[i * w + j] = char(tmp);
                                    }
                            }
                    }

                if (yKernel != NULL)
                    {
                        // apply y kernel.
                        int tmp;
                        for (int i = bordery; i <  h - bordery; i++)
                            {
                                // can save borderx (if applied)!
                                for (int j = borderx; j < w - borderx; j++)
                                    {
                                        tmp = 0;
                                        for (int k = 0; k < yksize; k++)
                                            {
                                                tmp += __tmp_res[(i + k - bordery) * w + j] 
                                                    * yvalues[yksize - k - 1];
                                            }
                                        tmp >>= yKernel->nShiftR;
                                        if (tmp > 255)
                                            tmp = 255;
                                        else
                                            if (tmp < 0)
                                                tmp = 0;
                                        dstImage->imageData[i * w + j] = char(tmp);
                                    }
                            }
                    }
            }
            break;

        case IPL_DEPTH_8S:
            {
                if (xKernel != NULL)
                    {
                        int tmp;
                        for (int i = 0; i <  h; i++)
                            {
                                for (int j = borderx; j < w - borderx; j++)
                                    {
                                        tmp = 0;
                                        for (int k = 0; k < xksize; k++)
                                            {
                                                tmp += srcImage->imageData[i * w + j + k - borderx] 
                                                    * xvalues[xksize - k - 1];
                                            }
                                        tmp >>= xKernel->nShiftR;
                                        if (tmp > 127)
                                            tmp = 127;
                                        else
                                            if (tmp < -128)
                                                tmp = -128;
                                        __tmp_res[i * w + j] = char(tmp);
                                    }
                            }
                    }

                if (yKernel != NULL)
                    {
                        int tmp;
                        for (int i = bordery; i <  h - bordery; i++)
                            {
                                for (int j = borderx; j < w - borderx; j++)
                                    {
                                        tmp = 0;
                                        for (int k = 0; k < yksize; k++)
                                            {
                                                tmp += __tmp_res[(i + k - bordery) * w + j] 
                                                    * yvalues[yksize - k - 1];
                                            }
                                        tmp >>= yKernel->nShiftR;
                                        if (tmp > 127)
                                            tmp = 127;
                                        else
                                            if (tmp < -128)
                                                tmp = -128;
                                        dstImage->imageData[i * w + j] = char(tmp);
                                    }
                            }
                    }
            }
        }
}

// TODO: manage IPL ROI and tiling.
IPLAPIIMPL(void, iplAllocateImage,(IplImage* image, int doFill, int fillValue))
{
	// Not implemented depth != 8
	//int depth = (image->depth & IPL_DEPTH_MASK)/8;
	assert (image->dataOrder == IPL_DATA_ORDER_PIXEL);
	///assert (image->widthStep == image->width * (image->depth & IPL_DEPTH_MASK) / 8 * image->nChannels);
	assert (image->imageSize == image->widthStep * image->height);

	image->imageData = AllocAligned<char> (image->imageSize);	/// new char[image->imageSize];
	assert (image->imageData != NULL);

	if (image->origin == IPL_ORIGIN_TL)
		image->imageDataOrigin = image->imageData + image->imageSize - image->widthStep;
	else
		image->imageDataOrigin = image->imageData;

	if (doFill)
        {
            // this of course is only valid for depth == 8.
            switch (image->depth)
                {
                case IPL_DEPTH_8U:
                case IPL_DEPTH_8S:
                    memset (image->imageData, fillValue, image->imageSize);
                    break;

                default:
                    assert (1 == 0);
                    break;
                }
        }
}

IPLAPIIMPL(void, iplAllocateImageFP,(IplImage* image, int doFill, float fillValue))
{
	assert (image->depth == IPL_DEPTH_32F);
	assert (image->dataOrder == IPL_DATA_ORDER_PIXEL);
    ///	assert (image->widthStep == image->width * (image->depth & IPL_DEPTH_MASK) / 8 * image->nChannels);
	assert (image->imageSize == image->widthStep * image->height);

	image->imageData = AllocAligned<char> (image->imageSize);
	assert (image->imageData != NULL);

	if (image->origin == IPL_ORIGIN_TL)
		image->imageDataOrigin = image->imageData + image->imageSize - image->widthStep;
	else
		image->imageDataOrigin = image->imageData;

	if (doFill)
        {
            if (fillValue == 0)
                {
                    // assume IEEE float
                    memset (image->imageData, 0, image->imageSize);
                }
            else
                {
                    assert (PAD_BYTES (image->widthStep, YARP_IMAGE_ALIGN) == 0);

                    // time consuming
                    float *tmp = (float *)image->imageData;
                    const int limit = image->imageSize / sizeof(float);
                    for (int i = 0; i < limit; i++)
                        {
                            *tmp++ = float(fillValue);
                        }
                }
        }
}

IPLAPIIMPL(void, iplDeallocateImage,(IplImage* image))
{
	if (image->imageData != NULL)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -