Bresenham算法opencv实现

/*
* =====================================================================================
*
* Filename: linebresenham.c
*
* Description: Bresenham for OpenCV
*
* Version: 1.0
* Created: 01/07/2009 04:59:47 PM
* Revision: none
* Compiler: gcc
*
* Author: Futuredaemon (BUPT), gnuhpc@gmail.com
* Company: BUPT_UNITED
*
* =====================================================================================
*/
#include <cv.h>
#include <highgui.h>
#include <cxcore.h>
#include <iostream>
#include <cmath>

using namespace std;

CvSize window = { 300, 300 }; //窗口大小

int main (int argc, char *argv[])
{
IplImage *imgA = cvCreateImage (window, IPL_DEPTH_8U, 3);//建立图像
cvSet (imgA, cvScalarAll (0), 0);//填充为0

int xx1, yy1, xx2, yy2;//直线的两个端点
int dx, dy, a, a1, e;//定义差量,误差等变量

int x, y;
/*设置直线的两个端点(20,20),(250,100) */
xx1 = 20, yy1 = 20;
xx2 = 250, yy2 = 100;
/*计算横纵坐标差量 */
dx = abs (xx2 - xx1);
dy = abs (yy2 - yy1);
/* 设定起始点 */
y = yy1;
x = xx1;
int sx, sy;//表明方向的变量

if (xx1 > xx2)
{
sx = -1;//x递减方向
}
else
{
sx = 1;//x递增方向
}



if (yy1 > yy2)
{
sy = -1;//y递减方向
}
else
{
sy = 1;//y递增方向
}


if (dx >= dy)
{
e=0;
for (x = xx1; (sx >= 0 ? x <= xx2 : x >= xx2); x += sx)
{
imgA->imageData[y * imgA->widthStep + x * 3] = (signed char) 255;
imgA->imageData[y * imgA->widthStep + x * 3 + 1] = (signed char) 255;
imgA->imageData[y * imgA->widthStep + x * 3 + 2] = (signed char) 255;
e+=dy;
if ((e<<1) >= dx)
{
y += sy;
e -= dx;
}

}
}
else
{
e=0;
for (y = yy1; (sy >= 0 ? y <= yy2 : y >= yy2); y += sy)
{
imgA->imageData[y * imgA->widthStep + x * 3] = (signed char) 255;
imgA->imageData[y * imgA->widthStep + x * 3 + 1] = (signed char) 255;
imgA->imageData[y * imgA->widthStep + x * 3 + 2] = (signed char) 255;
e+=dx;
if ((e<<1) >= dy)
{
x += sx;
e -= dy;
}

}
}


cvNamedWindow ("window", CV_WINDOW_AUTOSIZE);
cvShowImage ("window", imgA);

cvWaitKey (0);

cvReleaseImage (&imgA);
cvDestroyWindow ("window");

return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值