00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #pragma once
00022
00023 #include <climits>
00024
00025 #include "image.h"
00026 #include "imutil.h"
00027 #include "misc.h"
00028
00029 namespace distance_transform
00030 {
00031 const double RED_WEIGHT = 0.299;
00032 const double GREEN_WEIGHT = 0.584;
00033 const double BLUE_WEIGHT = 0.114;
00034
00035 static image<uchar> *imageRGBtoGRAY(image<rgb> *input) {
00036 int width = input->width();
00037 int height = input->height();
00038 image<uchar> *output = new image<uchar>(width, height, false);
00039
00040 for (int y = 0; y < height; y++) {
00041 for (int x = 0; x < width; x++) {
00042 imRef(output, x, y) = (uchar)
00043 (imRef(input, x, y).r * RED_WEIGHT +
00044 imRef(input, x, y).g * GREEN_WEIGHT +
00045 imRef(input, x, y).b * BLUE_WEIGHT);
00046 }
00047 }
00048 return output;
00049 }
00050
00051 static image<rgb> *imageGRAYtoRGB(image<uchar> *input) {
00052 int width = input->width();
00053 int height = input->height();
00054 image<rgb> *output = new image<rgb>(width, height, false);
00055
00056 for (int y = 0; y < height; y++) {
00057 for (int x = 0; x < width; x++) {
00058 imRef(output, x, y).r = imRef(input, x, y);
00059 imRef(output, x, y).g = imRef(input, x, y);
00060 imRef(output, x, y).b = imRef(input, x, y);
00061 }
00062 }
00063 return output;
00064 }
00065
00066 static image<float> *imageUCHARtoFLOAT(image<uchar> *input) {
00067 int width = input->width();
00068 int height = input->height();
00069 image<float> *output = new image<float>(width, height, false);
00070
00071 for (int y = 0; y < height; y++) {
00072 for (int x = 0; x < width; x++) {
00073 imRef(output, x, y) = imRef(input, x, y);
00074 }
00075 }
00076 return output;
00077 }
00078
00079 static image<float> *imageINTtoFLOAT(image<int> *input) {
00080 int width = input->width();
00081 int height = input->height();
00082 image<float> *output = new image<float>(width, height, false);
00083
00084 for (int y = 0; y < height; y++) {
00085 for (int x = 0; x < width; x++) {
00086 imRef(output, x, y) = imRef(input, x, y);
00087 }
00088 }
00089 return output;
00090 }
00091
00092 static image<uchar> *imageFLOATtoUCHAR(image<float> *input,
00093 float min, float max) {
00094 int width = input->width();
00095 int height = input->height();
00096 image<uchar> *output = new image<uchar>(width, height, false);
00097
00098 if (max == min)
00099 return output;
00100
00101 float scale = UCHAR_MAX / (max - min);
00102 for (int y = 0; y < height; y++) {
00103 for (int x = 0; x < width; x++) {
00104 uchar val = (uchar)((imRef(input, x, y) - min) * scale);
00105 imRef(output, x, y) = bound(val, (uchar)0, (uchar)UCHAR_MAX);
00106 }
00107 }
00108 return output;
00109 }
00110
00111 static image<uchar> *imageFLOATtoUCHAR(image<float> *input) {
00112 float min, max;
00113 min_max(input, &min, &max);
00114 return imageFLOATtoUCHAR(input, min, max);
00115 }
00116
00117 static image<long> *imageUCHARtoLONG(image<uchar> *input) {
00118 int width = input->width();
00119 int height = input->height();
00120 image<long> *output = new image<long>(width, height, false);
00121
00122 for (int y = 0; y < height; y++) {
00123 for (int x = 0; x < width; x++) {
00124 imRef(output, x, y) = imRef(input, x, y);
00125 }
00126 }
00127 return output;
00128 }
00129
00130 static image<uchar> *imageLONGtoUCHAR(image<long> *input, long min, long max) {
00131 int width = input->width();
00132 int height = input->height();
00133 image<uchar> *output = new image<uchar>(width, height, false);
00134
00135 if (max == min)
00136 return output;
00137
00138 float scale = UCHAR_MAX / (float)(max - min);
00139 for (int y = 0; y < height; y++) {
00140 for (int x = 0; x < width; x++) {
00141 uchar val = (uchar)((imRef(input, x, y) - min) * scale);
00142 imRef(output, x, y) = bound(val, (uchar)0, (uchar)UCHAR_MAX);
00143 }
00144 }
00145 return output;
00146 }
00147
00148 static image<uchar> *imageLONGtoUCHAR(image<long> *input) {
00149 long min, max;
00150 min_max(input, &min, &max);
00151 return imageLONGtoUCHAR(input, min, max);
00152 }
00153
00154 static image<uchar> *imageSHORTtoUCHAR(image<short> *input,
00155 short min, short max) {
00156 int width = input->width();
00157 int height = input->height();
00158 image<uchar> *output = new image<uchar>(width, height, false);
00159
00160 if (max == min)
00161 return output;
00162
00163 float scale = UCHAR_MAX / (float)(max - min);
00164 for (int y = 0; y < height; y++) {
00165 for (int x = 0; x < width; x++) {
00166 uchar val = (uchar)((imRef(input, x, y) - min) * scale);
00167 imRef(output, x, y) = bound(val, (uchar)0, (uchar)UCHAR_MAX);
00168 }
00169 }
00170 return output;
00171 }
00172
00173 static image<uchar> *imageSHORTtoUCHAR(image<short> *input) {
00174 short min, max;
00175 min_max(input, &min, &max);
00176 return imageSHORTtoUCHAR(input, min, max);
00177 }
00178
00179 }