作者:gnuhpc
出处:http://www.cnblogs.com/gnuhpc/
/* * ===================================================================================== * * 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; }
作者:gnuhpc
出处:http://www.cnblogs.com/gnuhpc/
除非另有声明,本网站采用知识共享“署名 2.5 *”许可协议授权。