PageRenderTime 273ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/images/book-hgts-pages/try.c

https://bitbucket.org/jialongl/pad
C | 117 lines | 94 code | 21 blank | 2 comment | 24 complexity | 9223132b8de6944d80de569f0aac38f6 MD5 | raw file
  1. // gcc -o try try.c -lm -lopencv_core -lopencv_highgui -lopencv_imgproc -std=c99
  2. // use command "./try" for the first run, and "./try An Y thing" for subsequent runs, to save time.
  3. #include <stdio.h>
  4. #include <opencv/cv.h>
  5. #include <opencv/highgui.h>
  6. #define N_IMAGES 10
  7. int main (int argc, char **argv) {
  8. char window_name[] = "try";
  9. IplImage *averaged;
  10. CvSize imgSize;
  11. int jpg_quality = 90;
  12. if (argc == 1) {
  13. IplImage* imgReds[N_IMAGES];
  14. IplImage* imgGreens[N_IMAGES];
  15. IplImage* imgBlues[N_IMAGES];
  16. for (int i=0;i<N_IMAGES;i++) {
  17. IplImage* img;
  18. char filename[8];
  19. sprintf(filename, "%d.jpg", (i+1));
  20. img = cvLoadImage(filename, CV_LOAD_IMAGE_COLOR);
  21. imgReds[i] = cvCreateImage(cvGetSize(img), 8, 1);
  22. imgGreens[i] = cvCreateImage(cvGetSize(img), 8, 1);
  23. imgBlues[i] = cvCreateImage(cvGetSize(img), 8, 1);
  24. cvSplit(img, imgReds[i], imgGreens[i], imgBlues[i], NULL);
  25. cvReleaseImage(&img);
  26. }
  27. imgSize = cvGetSize(imgReds[0]);
  28. averaged = cvCreateImage(imgSize, 8, 3);
  29. IplImage *imgRedsSum = cvCreateImage(imgSize, 8, 1);
  30. IplImage *imgGreensSum = cvCreateImage(imgSize, 8, 1);
  31. IplImage *imgBluesSum = cvCreateImage(imgSize, 8, 1);
  32. for (int y = 0; y < imgSize.height; y++) {
  33. for (int x = 0; x < imgSize.width; x++) {
  34. double sumRed = 0.0f;
  35. double sumGreen = 0.0f;
  36. double sumBlue = 0.0f;
  37. for (int i = 0; i < N_IMAGES; i++) {
  38. sumRed += cvGetReal2D(imgReds[i], y, x);
  39. sumGreen += cvGetReal2D(imgGreens[i], y, x);
  40. sumBlue += cvGetReal2D(imgBlues[i], y, x);
  41. }
  42. sumRed /= N_IMAGES;
  43. sumGreen /= N_IMAGES;
  44. sumBlue /= N_IMAGES;
  45. cvSetReal2D(imgRedsSum, y, x, sumRed);
  46. cvSetReal2D(imgGreensSum, y, x, sumGreen);
  47. cvSetReal2D(imgBluesSum, y, x, sumBlue);
  48. }
  49. }
  50. cvMerge(imgRedsSum, imgGreensSum, imgBluesSum, NULL, averaged);
  51. cvSaveImage("averaged.jpg", averaged, &jpg_quality);
  52. }
  53. else {
  54. averaged = cvLoadImage("averaged.jpg", CV_LOAD_IMAGE_COLOR);
  55. imgSize = cvGetSize(averaged);
  56. }
  57. cvNamedWindow(window_name, CV_WINDOW_AUTOSIZE);
  58. cvShowImage(window_name, averaged);
  59. printf("Showing \"averaged\" image. Going to blur it. Press any key to continue...\n");
  60. cvWaitKey(0);
  61. int neighborhood = 3;
  62. double offset = 3.6;
  63. cvSmooth(averaged, averaged, CV_GAUSSIAN, 3, 0, 0, 0);
  64. cvShowImage(window_name, averaged);
  65. printf("Showing blurred image. Going to do adaptive thresholding. Press any key to continue...\n");
  66. cvWaitKey(0);
  67. IplImage *gray = cvCreateImage(imgSize, averaged->depth, 1);
  68. cvCvtColor(averaged, gray, CV_RGB2GRAY);
  69. IplImage *thres = cvCreateImage(imgSize, averaged->depth, 1);
  70. char k;
  71. while (1) {
  72. cvAdaptiveThreshold(gray, thres,
  73. 255.0f,
  74. CV_ADAPTIVE_THRESH_GAUSSIAN_C,
  75. CV_THRESH_BINARY, neighborhood, offset);
  76. cvShowImage(window_name, thres);
  77. printf("Now adjust neighborhood and offset with i, o, -, =.\n");
  78. k = cvWaitKey(0);
  79. if (k == '-')
  80. offset -= 0.1;
  81. else if (k == '=')
  82. offset += 0.1;
  83. else if (k == 'i') {
  84. if (neighborhood != 3)
  85. neighborhood -= 2;
  86. }
  87. else if (k == 'o')
  88. neighborhood += 2;
  89. else if (k == 'q')
  90. break;
  91. printf("neighborhood = %d, offset = %G\n", neighborhood, offset);
  92. }
  93. cvSaveImage("thres.jpg", thres, &jpg_quality);
  94. cvReleaseImage(&thres);
  95. return 0;
  96. }