/tactip/opencvtactip/shear.c

https://github.com/alexsleat/GPC_IRB-120 · C · 152 lines · 102 code · 35 blank · 15 comment · 7 complexity · cc8358ae9ecfd6317ae03b0b95185027 MD5 · raw file

  1. #include <stdio.h>
  2. #include "cv.h"
  3. #include "highgui.h"
  4. int PxCount(IplImage *PxC);
  5. int main( int argc, char** argv )
  6. {
  7. /* data structure for the image */
  8. IplImage *img = 0;
  9. IplImage *Right = 0;
  10. IplImage *Left = 0;
  11. IplImage* src = 0, * res = 0, * roi = 0;
  12. int aX0 = 0, aY0 = 0, aX1 = 0, aY1 = 0;
  13. int bX0 = 0, bY0 = 0, bX1 = 0, bY1 = 0;
  14. /* check for supplied argument */
  15. if( argc < 2 ) {
  16. fprintf( stderr, "Usage: loading <filename>\n" );
  17. return 1;
  18. }
  19. /* load the image,
  20. use CV_LOAD_IMAGE_GRAYSCALE to load the image in grayscale */
  21. img = cvLoadImage( argv[1], CV_LOAD_IMAGE_GRAYSCALE );
  22. cvThreshold(img, img, 110, 120, CV_THRESH_BINARY); //Threshold image
  23. /* always check */
  24. if( img == 0 ) {
  25. fprintf( stderr, "Cannot load file %s!\n", argv[1] );
  26. return 1;
  27. }
  28. cvNamedWindow( "image", CV_WINDOW_AUTOSIZE ); //create a window
  29. cvShowImage( "image", img ); //display the image
  30. /*src = cvCloneImage(img);
  31. res = cvCreateImage(cvGetSize(src), 8, 1); //last arg was 3
  32. roi = cvCreateImage(cvGetSize(src), 8, 1);
  33. cvZero(roi); //prepare the 'ROI' image
  34. */
  35. //set the rectangle ROI's
  36. aX0 = (img->width/2);
  37. printf("aX0 = %d\n", aX0);
  38. aY0 = ((img->height/2)+0)-40;
  39. printf("aY0 = %d\n", aY0);
  40. aX1 = (img->width/2)+120;
  41. printf("aX1 = %d\n", aX1);
  42. aY1 = ((img->height/2)+0)+40;
  43. printf("aY1 = %d\n", aY1);
  44. printf("aX1-aX0 = %d\n",(aX1-aX0));
  45. printf("aY1-aY0 = %d\n",(aY1-aY0));
  46. bX0 = (img->width/2)-120;
  47. printf("bX0 = %d\n", bX0);
  48. bY0 = ((img->height/2)+0)-40;
  49. printf("bY0 = %d\n", bY0);
  50. bX1 = (img->width/2);
  51. printf("bX1 = %d\n", bX1);
  52. bY1 = ((img->height/2)+0)+40;
  53. printf("bY1 = %d\n", bY1);
  54. printf("bX1-bX0 = %d\n",(bX1-bX0));
  55. printf("bY1-bY0 = %d\n",(bY1-bY0));
  56. IplImage* newImage2 = 0;
  57. cvSetImageROI(img, cvRect(aX0, aY0, aX1-aX0, aY1-aY0));
  58. newImage2 = cvCreateImage(cvGetSize(img), img->depth, img->nChannels);
  59. cvSetImageROI(img, cvRect(bX0, bY0, bX1-bX0, bY1-aY0));
  60. cvCopy(img, newImage2, NULL);
  61. cvResetImageROI(img);
  62. Left = cvCloneImage(newImage2);
  63. printf("The pixel count on the Left is %d\n",PxCount(Left));
  64. IplImage* newImage = 0;
  65. cvSetImageROI(img, cvRect(aX0, aY0, aX1-aX0, aY1-aY0));
  66. newImage = cvCreateImage(cvGetSize(img), img->depth, img->nChannels);
  67. cvCopy(img, newImage, NULL);
  68. cvResetImageROI(img);
  69. Right = cvCloneImage(newImage);
  70. printf("The pixel count on the Right is %d\n",PxCount(Right));
  71. //draw the rectangle on the original image for reference
  72. CvPoint pt1, pt2;
  73. pt1.x = aX0;
  74. pt1.y = aY0;
  75. pt2.x = aX1;
  76. pt2.y = aY1;
  77. cvRectangle(img, pt1, pt2, CV_RGB(255,0,0),2, 8,0);
  78. CvPoint bpt1, bpt2;
  79. bpt1.x = bX0;
  80. bpt1.y = bY0;
  81. bpt2.x = bX1;
  82. bpt2.y = bY1;
  83. cvRectangle(img, bpt1, bpt2, CV_RGB(255,0,0),2, 8,0);
  84. //cvThreshold(Right, Right, 110, 120, CV_THRESH_BINARY); //Threshold image
  85. cvNamedWindow( "image", CV_WINDOW_AUTOSIZE ); //create a window
  86. cvShowImage( "image", img ); //display the image
  87. cvNamedWindow( "Left", CV_WINDOW_AUTOSIZE );
  88. cvShowImage( "Left", Left );
  89. cvNamedWindow( "Right", CV_WINDOW_AUTOSIZE );
  90. cvShowImage( "Right", Right );
  91. /* wait until user press a key */
  92. cvWaitKey(0);
  93. /* free memory */
  94. cvDestroyWindow( "image" );
  95. cvReleaseImage( &img );
  96. cvDestroyWindow( "Right" );
  97. cvReleaseImage( &Right );
  98. cvDestroyWindow( "Left" );
  99. cvReleaseImage( &Left );
  100. return 0;
  101. }
  102. int PxCount(IplImage *PxC)
  103. {
  104. int whitePixels = 0;
  105. int value;
  106. for ( int i=0 ; i < PxC->height ; i++ )
  107. {
  108. uchar* ptr = (uchar*) (PxC->imageData + i * PxC->widthStep );
  109. for ( int j=0 ; j < PxC->width ; j++ )
  110. {
  111. value = ptr[j];
  112. if ( value == 120 )
  113. whitePixels++;
  114. }
  115. }
  116. return whitePixels;
  117. }