// Line.cpp : Defines the entry point for the console application.  
//  
  
#include "stdafx.h"  
#include "cv.h"  
#include "highgui.h"  
  
void OnDrawDotline(CvPoint s, CvPoint e,IplImage *workimg)  
{  
    CvPoint pa,pb;  
      
    double k=(s.y-e.y)/(s.x-e.x+0.000001);//不加0.000001 会变成曲线,斜率可能为0,即e.x-s.x可能为0  
      
    double h=workimg->height,w=workimg->width;      
      
    pa.x=w;  
    pa.y=s.y+k*(w-s.x);   
      
    cvLine(workimg,e,pa,CV_RGB(0,255,255), 2, CV_AA, 0 );   //向右画线  
      
      
    pb.y=e.y-k*e.x;  
    pb.x=0;  
      
    cvLine(workimg,pb,s,CV_RGB(0,0,255), 2, CV_AA, 0 ); //向左画线  
  
}  
  
  
int main(int argc, char* argv[])  
{  
    IplImage * picture , * gray, *dst;  
    picture=cvLoadImage("3.jpg",1);  
    gray = cvCreateImage(cvSize(picture->width,picture->height),IPL_DEPTH_8U,1);  
    cvCvtColor(picture,gray,CV_BGR2GRAY);  
    dst = cvCreateImage(cvSize(picture->width,picture->height),IPL_DEPTH_8U,1);  
  
      
    int w=picture->width;  
    int h=picture->height;     
      
    cvThreshold(gray,dst,220,255,CV_THRESH_BINARY);   
    cvSaveImage("dst.jpg",dst);  
      
    cvLine(picture,cvPoint(20,50),cvPoint(60,40),CV_RGB(255,0,0),3);  
  
    OnDrawDotline(cvPoint(20,50), cvPoint(60,40),picture);  
  
    cvSaveImage("pi2.jpg",picture);  
      
    return 0;  
}