RenHao

20130529_Lab_HW02

Jun 4th, 2013
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.20 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. // add these two include files...
  5.  
  6. #include <cv.h>
  7. #include <highgui.h>
  8.  
  9. #define W 900
  10. #define H 720
  11.  
  12. void draw_line(IplImage *img2, int x1, int y1, int x2, int y2)
  13. {    
  14.      int xtemp,ytemp ;
  15.      if(x1 > x2)
  16.      {
  17.       xtemp = x1 ; x1 = x2 ; x2 = xtemp ;
  18.       ytemp = y1 ; y1 = y2 ; y2 = ytemp ;
  19.      }
  20.      //To fix the bug that point can't be printed from right-side to left-side
  21.      
  22.      if(x1 == x2)
  23.      {
  24.      cvLine(
  25.          img2, //CvArr* img,
  26.          cvPoint((x1 + W/2), (-y1 + H/2)),  //CvPoint pt1,
  27.          cvPoint((x1 + W/2), (-y2 + H/2)),  //CvPoint pt2,
  28.          cvScalar(0, 255, 0, 0),  //CvScalar color,
  29.          1, 8, 0
  30.        ); //int thickness=1, int line_type=8, int shift=0 );    
  31.      }
  32.      else
  33.      {
  34.      int dx = x2-x1;
  35.      int dy = y2-y1;
  36.      float m = (float)dy/dx;
  37.      
  38.      float x, y;  float IX, IY;
  39.      for (x =  x1; x < x2; x = x + 0.01)
  40.      {
  41.        //y = m*x + y1 + 0.5; //Plus 0.5 to provide for m wasn't an integer.
  42.        y = m * (x - x1) + y1 ;
  43.        IX = x + W/2;
  44.        IY = -y + H/2;
  45.        //putpixel(x, y);
  46.        
  47.        cvLine(
  48.          img2, //CvArr* img,
  49.          cvPoint(IX, IY),  //CvPoint pt1,
  50.          cvPoint(IX, IY),  //CvPoint pt2,
  51.          cvScalar(0, 255, 0, 0),  //CvScalar color,
  52.          1, 8, 0
  53.        ); //int thickness=1, int line_type=8, int shift=0 );
  54.       }
  55.       }
  56. }
  57.  
  58.  
  59. int main(int argc, char *argv[])
  60. {
  61.   IplImage *img, *img2;      // Just like FILE *fptr ...
  62.  
  63.   img2 = cvCreateImage(cvSize(W, H), //CvSize size,
  64.                        IPL_DEPTH_8U , //int depth,
  65.                        3); //int channels
  66.  
  67.   printf("Channels %d depth %d \n", img2->nChannels, img2->depth);
  68.   printf("The image Width by Height is %dX%d \n",
  69.          img2->width,img2->height);
  70.   printf("The image WidthStep %d \n", img2->widthStep);
  71.  
  72.  
  73.      cvLine(
  74.        img2, //CvArr* img,
  75.        cvPoint(0, H/2),  //CvPoint pt1,
  76.        cvPoint(W-1, H/2),  //CvPoint pt2,
  77.        cvScalar(0, 0, 255, 0),  //CvScalar color,
  78.        1, 8, 0); //int thickness=1, int line_type=8, int shift=0 );
  79.      cvLine(
  80.        img2, //CvArr* img,
  81.        cvPoint(W/2, 0),  //CvPoint pt1,
  82.        cvPoint(W/2, H-1),  //CvPoint pt2,
  83.        cvScalar(0, 0, 255, 0),  //CvScalar color,
  84.        1, 8, 0); //int thickness=1, int line_type=8, int shift=0 );
  85.      
  86.     /*              I              */
  87.     draw_line(img2, 200, 200, 100, 100);
  88.     /*              II             */
  89.     draw_line(img2,-200, 200,-100, 100);
  90.     /*              III            */
  91.     draw_line(img2,-200,-200,-100,-100);
  92.     /*              IIII           */
  93.     draw_line(img2, 200,-200, 100,-100);
  94.     /*              horizontal     */
  95.     draw_line(img2,-200,-200, 200,-200);
  96.     /*              vertical       */
  97.     draw_line(img2,-100,-100,-100, 100);
  98.     draw_line(img2, 100, 100, 100,-100);
  99.     /*              large slope    */
  100.     draw_line(img2, -20,-300,  20, 300);
  101.     draw_line(img2,  20,-300, -20, 300);
  102.    
  103.     cvNamedWindow( "Fig2", 1 );
  104.     cvShowImage("Fig2", img2 );  
  105.  
  106.  
  107.     cvWaitKey(0);
  108.     cvDestroyWindow( "Fig2" );
  109.     cvReleaseImage( &img2 );
  110.     system("PAUSE");      return 0;
  111.  
  112.  
  113. }
Advertisement
Add Comment
Please, Sign In to add comment