OpenCV 的四种边界生成方式
来源:互联网 发布:php进销存源码erp 编辑:程序博客网 时间:2024/05/01 05:08
OpenCV 的 cvCopyMakeBorder 函数有四种边界生成方式,分别是 常量(constant)、复制(replicate)、反射(reflect) 和 包围(wrap)。它们的效果图如下:
原图是 Lena.jpg,512×512 的,扩充的边界是 左右上下 各 500 个像素。
代码如下:(未使用 cvCopyMakeBorder 函数)
/*-------------------------------------------------------类 名:CWjpBorder文件名:WjpBorder.hpp描 述:完成图像的边界扩充。备 注:wujp2013-07-01初次创建。-------------------------------------------------------*/#pragma onceclass CWjpBorder{public:enum WjpBorderType{WJP_BORDER_CONSTANT = 1,WJP_BORDER_REPLICATE = 2,WJP_BORDER_REFLECT = 3,WJP_BORDER_WRAP = 4,};public:CWjpBorder(){};virtual ~CWjpBorder(){};public:void MakeBorder(const BYTE* pbySrcImage, int nSrcWidth, int nSrcHeight, int nBits, WjpBorderType borderType, BYTE* pbyDstImage, int nDstWidth, int nDstHeight, BYTE byConstant = 0);};inline void CWjpBorder::MakeBorder(const BYTE* pbySrcImage, int nSrcWidth, int nSrcHeight, int nBits, WjpBorderType borderType, BYTE* pbyDstImage, int nDstWidth, int nDstHeight, BYTE byConstant){if (nBits != 8 && nBits != 24)return;int nColorComponent = nBits / 8;int nSrcSize = nSrcWidth * nSrcHeight * nColorComponent;int nDstSize = nDstWidth * nDstHeight * nColorComponent;if (nSrcSize == nDstSize){memcpy(pbyDstImage, pbySrcImage, nSrcSize);return;}int nXStart = (nDstWidth - nSrcWidth) / 2;int nYStart = (nDstHeight - nSrcHeight) / 2;// nXStart == nYStartif (borderType == WJP_BORDER_CONSTANT)memset(pbyDstImage, byConstant, nDstSize);int nSrcLineWidth = nSrcWidth * nColorComponent;int nDstLineWidth = nDstWidth * nColorComponent;int i;for (i=0; i<nSrcHeight; i++){const BYTE* pbySrcLine = pbySrcImage + i * nSrcLineWidth;BYTE* pbyDstLine = pbyDstImage + (nYStart + i) * nDstLineWidth + nXStart * nColorComponent;memcpy(pbyDstLine, pbySrcLine, nSrcLineWidth);}if (borderType == WJP_BORDER_CONSTANT)return;//从内向外,一圈圈扩充。for (i=0; i<nXStart; i++){//“内”指已知图像的最外边界,“外”指需扩充的最内边界。//最上边和最下边的内行宽度。int nInnerWidth = nSrcWidth + i * 2;//最左边和最右边的内列高度。int nInnerHeight = nSrcHeight + i * 2;BYTE* pbyDstInner;BYTE* pbyDstOuter;//最上边的行。pbyDstOuter = pbyDstImage + (nYStart - i - 1) * nDstLineWidth + (nXStart - i) * nColorComponent;if (borderType == WJP_BORDER_REPLICATE)pbyDstInner = pbyDstImage + (nYStart - i) * nDstLineWidth + (nXStart - i) * nColorComponent;else if (borderType == WJP_BORDER_REFLECT)pbyDstInner = pbyDstImage + (nYStart + i) * nDstLineWidth + (nXStart - i) * nColorComponent;else if (borderType == WJP_BORDER_WRAP)pbyDstInner = pbyDstImage + (nYStart - i + nSrcHeight - 1) * nDstLineWidth + (nXStart - i) * nColorComponent;memcpy(pbyDstOuter, pbyDstInner, nInnerWidth * nColorComponent);//最下边的行。pbyDstOuter = pbyDstImage + (nYStart + i + nSrcHeight) * nDstLineWidth + (nXStart - i) * nColorComponent;if (borderType == WJP_BORDER_REPLICATE)pbyDstInner = pbyDstImage + (nYStart + i + nSrcHeight - 1) * nDstLineWidth + (nXStart - i) * nColorComponent;else if (borderType == WJP_BORDER_REFLECT)pbyDstInner = pbyDstImage + (nYStart - i + nSrcHeight - 1) * nDstLineWidth + (nXStart - i) * nColorComponent;else if (borderType == WJP_BORDER_WRAP)pbyDstInner = pbyDstImage + (nYStart + i) * nDstLineWidth + (nXStart - i) * nColorComponent;memcpy(pbyDstOuter, pbyDstInner, nInnerWidth * nColorComponent);//最左边的列。pbyDstOuter = pbyDstImage + (nYStart - i) * nDstLineWidth + (nXStart - i - 1) * nColorComponent;if (borderType == WJP_BORDER_REPLICATE)pbyDstInner = pbyDstImage + (nYStart - i) * nDstLineWidth + (nXStart - i) * nColorComponent;else if (borderType == WJP_BORDER_REFLECT)pbyDstInner = pbyDstImage + (nYStart - i) * nDstLineWidth + (nXStart + i) * nColorComponent;else if (borderType == WJP_BORDER_WRAP)pbyDstInner = pbyDstImage + (nYStart - i) * nDstLineWidth + (nXStart - i + nSrcWidth - 1) * nColorComponent;int j;for (j=0; j<nInnerHeight; j++){memcpy(pbyDstOuter, pbyDstInner, nColorComponent);pbyDstInner += nDstLineWidth;pbyDstOuter += nDstLineWidth;}//最右边的列。pbyDstOuter = pbyDstImage + (nYStart - i) * nDstLineWidth + (nXStart + i + nSrcWidth) * nColorComponent;if (borderType == WJP_BORDER_REPLICATE)pbyDstInner = pbyDstImage + (nYStart - i) * nDstLineWidth + (nXStart + i + nSrcWidth - 1) * nColorComponent;else if (borderType == WJP_BORDER_REFLECT)pbyDstInner = pbyDstImage + (nYStart - i) * nDstLineWidth + (nXStart - i + nSrcWidth - 1) * nColorComponent;else if (borderType == WJP_BORDER_WRAP)pbyDstInner = pbyDstImage + (nYStart - i) * nDstLineWidth + (nXStart + i) * nColorComponent;for (j=0; j<nInnerHeight; j++){memcpy(pbyDstOuter, pbyDstInner, nColorComponent);pbyDstInner += nDstLineWidth;pbyDstOuter += nDstLineWidth;}//处理四个角。//左上角pbyDstOuter = pbyDstImage + (nYStart - i - 1) * nDstLineWidth + (nXStart - i - 1) * nColorComponent;if (borderType == WJP_BORDER_REPLICATE)pbyDstInner = pbyDstImage + (nYStart - i) * nDstLineWidth + (nXStart - i) * nColorComponent;else if (borderType == WJP_BORDER_REFLECT)pbyDstInner = pbyDstImage + (nYStart + i) * nDstLineWidth + (nXStart + i) * nColorComponent;else if (borderType == WJP_BORDER_WRAP)pbyDstInner = pbyDstImage + (nYStart - i + nSrcHeight - 1) * nDstLineWidth + (nXStart - i + nSrcWidth - 1) * nColorComponent;memcpy(pbyDstOuter, pbyDstInner, nColorComponent);//右上角pbyDstOuter = pbyDstImage + (nYStart - i - 1) * nDstLineWidth + (nXStart + i + nSrcWidth) * nColorComponent;if (borderType == WJP_BORDER_REPLICATE)pbyDstInner = pbyDstImage + (nYStart - i) * nDstLineWidth + (nXStart + i + nSrcWidth - 1) * nColorComponent;else if (borderType == WJP_BORDER_REFLECT)pbyDstInner = pbyDstImage + (nYStart + i) * nDstLineWidth + (nXStart - i + nSrcWidth - 1) * nColorComponent;else if (borderType == WJP_BORDER_WRAP)pbyDstInner = pbyDstImage + (nYStart - i + nSrcHeight - 1) * nDstLineWidth + (nXStart + i) * nColorComponent;memcpy(pbyDstOuter, pbyDstInner, nColorComponent);//左下角pbyDstOuter = pbyDstImage + (nYStart + i + nSrcHeight) * nDstLineWidth + (nXStart - i - 1) * nColorComponent;if (borderType == WJP_BORDER_REPLICATE)pbyDstInner = pbyDstImage + (nYStart + i + nSrcHeight - 1) * nDstLineWidth + (nXStart - i) * nColorComponent;else if (borderType == WJP_BORDER_REFLECT)pbyDstInner = pbyDstImage + (nYStart - i + nSrcHeight - 1) * nDstLineWidth + (nXStart + i) * nColorComponent;else if (borderType == WJP_BORDER_WRAP)pbyDstInner = pbyDstImage + (nYStart + i) * nDstLineWidth + nXStart - i + (nSrcWidth - 1) * nColorComponent;memcpy(pbyDstOuter, pbyDstInner, nColorComponent);//右下角pbyDstOuter = pbyDstImage + (nYStart + i + nSrcHeight) * nDstLineWidth + (nXStart + i + nSrcWidth) * nColorComponent;if (borderType == WJP_BORDER_REPLICATE)pbyDstInner = pbyDstImage + (nYStart + i + nSrcHeight - 1) * nDstLineWidth + (nXStart + i + nSrcWidth - 1) * nColorComponent;else if (borderType == WJP_BORDER_REFLECT)pbyDstInner = pbyDstImage + (nYStart - i + nSrcHeight - 1) * nDstLineWidth + (nXStart - i + nSrcWidth - 1) * nColorComponent;else if (borderType == WJP_BORDER_WRAP)pbyDstInner = pbyDstImage + (nYStart + i) * nDstLineWidth + (nXStart + i) * nColorComponent;memcpy(pbyDstOuter, pbyDstInner, nColorComponent);}return;}
- OpenCV 的四种边界生成方式
- opencv 的几种边界
- java生成线程的四种方式
- SQL生成序号的四种方式
- XML的四种生成方式
- opencv图像边界的填充
- MyBatis Generator生成代码的四种方式
- axis2生成客户端代码的四种方式
- Java生成xml文件的四种方式
- OpenCV轮廓、边缘、边界的相关函数
- Java生成xml四种方式
- 基于边界四边形的凸包生成
- opencv学习(四)之像素遍历三种方式
- xml生成、解析的四种方式,dom、sax、dom4j、jdom
- javaEE:day1-验证码生成技术和启动tomcat的四种方式
- OpenCV边界处理类型
- [OpenCV]拓展图像边界
- [OpenCV]拓展图像边界
- Android开发(4)Android电话拨号器
- linux进程通信---几个发送信号的函数(kill,raise,alarm,pause)
- DIV层的五条叠加法则
- Android开发(5)--对应用进行单元测试
- 设置APP加载页面
- OpenCV 的四种边界生成方式
- 【Boost】boost库asio详解——boost::asio::buffer用法
- linux 远程shell 实现
- DXTREME Mobile中文教程分享
- 比较文件名时,大小写转换函数需慎用
- 插入新记录时 索引或主关键字不能包含一个 空 (Null) 值
- POJ 3239 Solution to the n Queens Puzzle
- Android开发(6)--完成登陆界面的数据保存回显的操作
- 输入IP后跳到指定域名