imconv.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /*
  2. Copyright (C) 2006 Pedro Felzenszwalb
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  14. */
  15. /* image conversion */
  16. #ifndef CONV_H
  17. #define CONV_H
  18. #include <climits>
  19. #include "segmentation/felzenszwalb/image.h"
  20. #include "segmentation/felzenszwalb/imutil.h"
  21. #include "segmentation/felzenszwalb/misc.h"
  22. #define RED_WEIGHT 0.299
  23. #define GREEN_WEIGHT 0.587
  24. #define BLUE_WEIGHT 0.114
  25. namespace felzenszwalb {
  26. static image<uchar> *imageRGBtoGRAY(image<rgb> *input) {
  27. int width = input->width();
  28. int height = input->height();
  29. image<uchar> *output = new image<uchar>(width, height, false);
  30. for (int y = 0; y < height; y++) {
  31. for (int x = 0; x < width; x++) {
  32. imRef(output, x, y) = (uchar)
  33. (imRef(input, x, y).r * RED_WEIGHT +
  34. imRef(input, x, y).g * GREEN_WEIGHT +
  35. imRef(input, x, y).b * BLUE_WEIGHT);
  36. }
  37. }
  38. return output;
  39. }
  40. static image<rgb> *imageGRAYtoRGB(image<uchar> *input) {
  41. int width = input->width();
  42. int height = input->height();
  43. image<rgb> *output = new image<rgb>(width, height, false);
  44. for (int y = 0; y < height; y++) {
  45. for (int x = 0; x < width; x++) {
  46. imRef(output, x, y).r = imRef(input, x, y);
  47. imRef(output, x, y).g = imRef(input, x, y);
  48. imRef(output, x, y).b = imRef(input, x, y);
  49. }
  50. }
  51. return output;
  52. }
  53. static image<float> *imageUCHARtoFLOAT(image<uchar> *input) {
  54. int width = input->width();
  55. int height = input->height();
  56. image<float> *output = new image<float>(width, height, false);
  57. for (int y = 0; y < height; y++) {
  58. for (int x = 0; x < width; x++) {
  59. imRef(output, x, y) = imRef(input, x, y);
  60. }
  61. }
  62. return output;
  63. }
  64. static image<float> *imageINTtoFLOAT(image<int> *input) {
  65. int width = input->width();
  66. int height = input->height();
  67. image<float> *output = new image<float>(width, height, false);
  68. for (int y = 0; y < height; y++) {
  69. for (int x = 0; x < width; x++) {
  70. imRef(output, x, y) = imRef(input, x, y);
  71. }
  72. }
  73. return output;
  74. }
  75. static image<uchar> *imageFLOATtoUCHAR(image<float> *input,
  76. float min, float max) {
  77. int width = input->width();
  78. int height = input->height();
  79. image<uchar> *output = new image<uchar>(width, height, false);
  80. if (max == min)
  81. return output;
  82. float scale = UCHAR_MAX / (max - min);
  83. for (int y = 0; y < height; y++) {
  84. for (int x = 0; x < width; x++) {
  85. uchar val = (uchar)((imRef(input, x, y) - min) * scale);
  86. imRef(output, x, y) = bound(val, (uchar)0, (uchar)UCHAR_MAX);
  87. }
  88. }
  89. return output;
  90. }
  91. static image<uchar> *imageFLOATtoUCHAR(image<float> *input) {
  92. float min, max;
  93. min_max(input, &min, &max);
  94. return imageFLOATtoUCHAR(input, min, max);
  95. }
  96. static image<long> *imageUCHARtoLONG(image<uchar> *input) {
  97. int width = input->width();
  98. int height = input->height();
  99. image<long> *output = new image<long>(width, height, false);
  100. for (int y = 0; y < height; y++) {
  101. for (int x = 0; x < width; x++) {
  102. imRef(output, x, y) = imRef(input, x, y);
  103. }
  104. }
  105. return output;
  106. }
  107. static image<uchar> *imageLONGtoUCHAR(image<long> *input, long min, long max) {
  108. int width = input->width();
  109. int height = input->height();
  110. image<uchar> *output = new image<uchar>(width, height, false);
  111. if (max == min)
  112. return output;
  113. float scale = UCHAR_MAX / (float)(max - min);
  114. for (int y = 0; y < height; y++) {
  115. for (int x = 0; x < width; x++) {
  116. uchar val = (uchar)((imRef(input, x, y) - min) * scale);
  117. imRef(output, x, y) = bound(val, (uchar)0, (uchar)UCHAR_MAX);
  118. }
  119. }
  120. return output;
  121. }
  122. static image<uchar> *imageLONGtoUCHAR(image<long> *input) {
  123. long min, max;
  124. min_max(input, &min, &max);
  125. return imageLONGtoUCHAR(input, min, max);
  126. }
  127. static image<uchar> *imageSHORTtoUCHAR(image<short> *input,
  128. short min, short max) {
  129. int width = input->width();
  130. int height = input->height();
  131. image<uchar> *output = new image<uchar>(width, height, false);
  132. if (max == min)
  133. return output;
  134. float scale = UCHAR_MAX / (float)(max - min);
  135. for (int y = 0; y < height; y++) {
  136. for (int x = 0; x < width; x++) {
  137. uchar val = (uchar)((imRef(input, x, y) - min) * scale);
  138. imRef(output, x, y) = bound(val, (uchar)0, (uchar)UCHAR_MAX);
  139. }
  140. }
  141. return output;
  142. }
  143. static image<uchar> *imageSHORTtoUCHAR(image<short> *input) {
  144. short min, max;
  145. min_max(input, &min, &max);
  146. return imageSHORTtoUCHAR(input, min, max);
  147. }
  148. }//namespace
  149. #endif