Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 
 
 

391 Zeilen
10 KiB

  1. /*
  2. * This file is part of Aptdec.
  3. * Copyright (c) 2004-2009 Thierry Leconte (F4DWV), Xerbo (xerbo@protonmail.com) 2019-2022
  4. *
  5. * Aptdec is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. *
  18. */
  19. #include <stdio.h>
  20. #include <string.h>
  21. #include <math.h>
  22. #include <stdlib.h>
  23. #include "apt.h"
  24. #include "algebra.h"
  25. #include "image.h"
  26. static linear_t compute_regression(float *wedges) {
  27. // { 0.106, 0.215, 0.324, 0.433, 0.542, 0.652, 0.78, 0.87, 0.0 }
  28. const float teleramp[9] = { 31.07, 63.02, 94.96, 126.9, 158.86, 191.1, 228.62, 255.0, 0.0 };
  29. return linear_regression(wedges, teleramp, 9);
  30. }
  31. static float tele[16];
  32. static float Cs;
  33. void apt_histogramEqualise(float **prow, int nrow, int offset, int width){
  34. // Plot histogram
  35. int histogram[256] = { 0 };
  36. for(int y = 0; y < nrow; y++)
  37. for(int x = 0; x < width; x++)
  38. histogram[(int)CLIP(prow[y][x+offset], 0, 255)]++;
  39. // Calculate cumulative frequency
  40. long sum = 0, cf[256] = { 0 };
  41. for(int i = 0; i < 255; i++){
  42. sum += histogram[i];
  43. cf[i] = sum;
  44. }
  45. // Apply histogram
  46. int area = nrow * width;
  47. for(int y = 0; y < nrow; y++){
  48. for(int x = 0; x < width; x++){
  49. int k = (int)prow[y][x+offset];
  50. prow[y][x+offset] = (256.0f/area) * cf[k];
  51. }
  52. }
  53. }
  54. void apt_linearEnhance(float **prow, int nrow, int offset, int width){
  55. // Plot histogram
  56. int histogram[256] = { 0 };
  57. for(int y = 0; y < nrow; y++)
  58. for(int x = 0; x < width; x++)
  59. histogram[(int)CLIP(prow[y][x+offset], 0, 255)]++;
  60. // Find min/max points
  61. int min = -1, max = -1;
  62. for(int i = 5; i < 250; i++){
  63. if(histogram[i]/width/(nrow/255.0) > 0.1){
  64. if(min == -1) min = i;
  65. max = i;
  66. }
  67. }
  68. // Stretch the brightness into the new range
  69. for(int y = 0; y < nrow; y++){
  70. for(int x = 0; x < width; x++){
  71. prow[y][x+offset] = (prow[y][x+offset]-min) / (max-min) * 255.0f;
  72. prow[y][x+offset] = CLIP(prow[y][x+offset], 0.0f, 255.0f);
  73. }
  74. }
  75. }
  76. // Brightness calibrate, including telemetry
  77. void calibrateImage(float **prow, int nrow, int offset, int width, linear_t regr){
  78. offset -= APT_SYNC_WIDTH+APT_SPC_WIDTH;
  79. for (int y = 0; y < nrow; y++) {
  80. for (int x = 0; x < width+APT_SYNC_WIDTH+APT_SPC_WIDTH+APT_TELE_WIDTH; x++) {
  81. float pv = linear_calc(prow[y][x + offset], regr);
  82. prow[y][x + offset] = CLIP(pv, 0, 255);
  83. }
  84. }
  85. }
  86. float teleNoise(float *wedges){
  87. float pattern[9] = { 31.07, 63.02, 94.96, 126.9, 158.86, 191.1, 228.62, 255.0, 0.0 };
  88. float noise = 0;
  89. for(int i = 0; i < 9; i++)
  90. noise += fabs(wedges[i] - pattern[i]);
  91. return noise;
  92. }
  93. // Get telemetry data for thermal calibration
  94. apt_channel_t apt_calibrate(float **prow, int nrow, int offset, int width) {
  95. float teleline[APT_MAX_HEIGHT] = { 0.0 };
  96. float wedge[16];
  97. linear_t regr[APT_MAX_HEIGHT/APT_FRAME_LEN + 1];
  98. int telestart, mtelestart = 0;
  99. int channel = -1;
  100. // The minimum rows required to decode a full frame
  101. if (nrow < APT_CALIBRATION_ROWS) {
  102. fprintf(stderr, "Telemetry decoding error, not enough rows\n");
  103. return APT_CHANNEL_UNKNOWN;
  104. }
  105. // Calculate average of a row of telemetry
  106. for (int y = 0; y < nrow; y++) {
  107. for (int x = 3; x < 43; x++)
  108. teleline[y] += prow[y][x + offset + width];
  109. teleline[y] /= 40.0;
  110. }
  111. /* Wedge 7 is white and 8 is black, this will have the largest
  112. * difference in brightness, this can be used to find the current
  113. * position within the telemetry.
  114. */
  115. float max = 0.0f;
  116. for (int n = nrow / 3 - 64; n < 2 * nrow / 3 - 64; n++) {
  117. float df;
  118. // (sum 4px below) - (sum 4px above)
  119. df = (float)((teleline[n - 4] + teleline[n - 3] + teleline[n - 2] + teleline[n - 1]) -
  120. (teleline[n + 0] + teleline[n + 1] + teleline[n + 2] + teleline[n + 3]));
  121. // Find the maximum difference
  122. if (df > max) {
  123. mtelestart = n;
  124. max = df;
  125. }
  126. }
  127. telestart = (mtelestart + 64) % APT_FRAME_LEN;
  128. // Make sure that theres at least one full frame in the image
  129. if (nrow < telestart + APT_FRAME_LEN) {
  130. fprintf(stderr, "Telemetry decoding error, not enough rows\n");
  131. return APT_CHANNEL_UNKNOWN;
  132. }
  133. // Find the least noisy frame
  134. float minNoise = -1;
  135. int bestFrame = -1;
  136. for (int n = telestart, k = 0; n < nrow - APT_FRAME_LEN; n += APT_FRAME_LEN, k++) {
  137. int j;
  138. for (j = 0; j < 16; j++) {
  139. int i;
  140. wedge[j] = 0.0;
  141. for (i = 1; i < 7; i++)
  142. wedge[j] += teleline[n + j * 8 + i];
  143. wedge[j] /= 6;
  144. }
  145. float noise = teleNoise(wedge);
  146. if(noise < minNoise || minNoise == -1){
  147. minNoise = noise;
  148. bestFrame = k;
  149. // Compute & apply regression on the wedges
  150. regr[k] = compute_regression(wedge);
  151. for (int j = 0; j < 16; j++)
  152. tele[j] = linear_calc(wedge[j], regr[k]);
  153. /* Compare the channel ID wedge to the reference
  154. * wedges, the wedge with the closest match will
  155. * be the channel ID
  156. */
  157. float min = -1;
  158. for (int j = 0; j < 6; j++) {
  159. float df = (float)(tele[15] - tele[j]);
  160. df *= df;
  161. if (df < min || min == -1) {
  162. channel = j;
  163. min = df;
  164. }
  165. }
  166. // Find the brightness of the minute marker, I don't really know what for
  167. Cs = 0.0;
  168. int i, j = n;
  169. for (i = 0, j = n; j < n + APT_FRAME_LEN; j++) {
  170. float csline = 0.0;
  171. for (int l = 3; l < 43; l++)
  172. csline += prow[n][l + offset - APT_SPC_WIDTH];
  173. csline /= 40.0;
  174. if (csline > 50.0) {
  175. Cs += csline;
  176. i++;
  177. }
  178. }
  179. Cs = linear_calc((Cs / i), regr[k]);
  180. }
  181. }
  182. if(bestFrame == -1){
  183. fprintf(stderr, "Something has gone very wrong, please file a bug report.\n");
  184. return APT_CHANNEL_UNKNOWN;
  185. }
  186. calibrateImage(prow, nrow, offset, width, regr[bestFrame]);
  187. return (apt_channel_t)(channel + 1);
  188. }
  189. extern float quick_select(float arr[], int n);
  190. // Biased median denoise, pretyt ugly
  191. #define TRIG_LEVEL 40
  192. void apt_denoise(float **prow, int nrow, int offset, int width){
  193. for(int y = 2; y < nrow-2; y++){
  194. for(int x = offset+1; x < offset+width-1; x++){
  195. if(prow[y][x+1] - prow[y][x] > TRIG_LEVEL ||
  196. prow[y][x-1] - prow[y][x] > TRIG_LEVEL ||
  197. prow[y+1][x] - prow[y][x] > TRIG_LEVEL ||
  198. prow[y-1][x] - prow[y][x] > TRIG_LEVEL){
  199. prow[y][x] = quick_select((float[]){
  200. prow[y+2][x-1], prow[y+2][x], prow[y+2][x+1],
  201. prow[y+1][x-1], prow[y+1][x], prow[y+1][x+1],
  202. prow[y-1][x-1], prow[y-1][x], prow[y-1][x+1],
  203. prow[y-2][x-1], prow[y-2][x], prow[y-2][x+1]
  204. }, 12);
  205. }
  206. }
  207. }
  208. }
  209. #undef TRIG_LEVEL
  210. // Flips a channel, for northbound passes
  211. void apt_flipImage(apt_image_t *img, int width, int offset){
  212. for(int y = 1; y < img->nrow; y++){
  213. for(int x = 1; x < ceil(width / 2.0); x++){
  214. // Flip top-left & bottom-right
  215. float buffer = img->prow[img->nrow - y][offset + x];
  216. img->prow[img->nrow - y][offset + x] = img->prow[y][offset + (width - x)];
  217. img->prow[y][offset + (width - x)] = buffer;
  218. }
  219. }
  220. }
  221. // Calculate crop to reomve noise from the start and end of an image
  222. int apt_cropNoise(apt_image_t *img){
  223. #define NOISE_THRESH 180.0
  224. // Average value of minute marker
  225. float spc_rows[APT_MAX_HEIGHT] = { 0.0 };
  226. int startCrop = 0; int endCrop = img->nrow;
  227. for(int y = 0; y < img->nrow; y++) {
  228. for(int x = 0; x < APT_SPC_WIDTH; x++) {
  229. spc_rows[y] += img->prow[y][x + (APT_CHB_OFFSET - APT_SPC_WIDTH)];
  230. }
  231. spc_rows[y] /= APT_SPC_WIDTH;
  232. // Skip minute markings
  233. if(spc_rows[y] < 10) {
  234. spc_rows[y] = spc_rows[y-1];
  235. }
  236. }
  237. // 3 row average
  238. for(int y = 0; y < img->nrow; y++){
  239. spc_rows[y] = (spc_rows[y+1] + spc_rows[y+2] + spc_rows[y+3])/3;
  240. //img.prow[y][0] = spc_rows[y];
  241. }
  242. // Find ends
  243. for(int y = 0; y < img->nrow-1; y++) {
  244. if(spc_rows[y] > NOISE_THRESH){
  245. endCrop = y;
  246. }
  247. }
  248. for(int y = img->nrow; y > 0; y--) {
  249. if(spc_rows[y] > NOISE_THRESH) {
  250. startCrop = y;
  251. }
  252. }
  253. //printf("Crop rows: %i -> %i\n", startCrop, endCrop);
  254. // Remove the noisy rows at start
  255. for(int y = 0; y < img->nrow-startCrop; y++) {
  256. memmove(img->prow[y], img->prow[y+startCrop], sizeof(float)*APT_PROW_WIDTH);
  257. }
  258. // Ignore the noisy rows at the end
  259. img->nrow = (endCrop - startCrop);
  260. return startCrop;
  261. }
  262. // --- Temperature Calibration --- //
  263. #include "satcal.h"
  264. typedef struct {
  265. float Nbb;
  266. float Cs;
  267. float Cb;
  268. int ch;
  269. } tempparam_t;
  270. // IR channel temperature compensation
  271. static void tempcomp(float t[16], int ch, int satnum, tempparam_t *tpr) {
  272. float Tbb, T[4];
  273. float C;
  274. tpr->ch = ch - 4;
  275. // Compute equivalent T blackbody temperature
  276. for (int n = 0; n < 4; n++) {
  277. float d0, d1, d2;
  278. C = t[9 + n] * 4.0;
  279. d0 = satcal[satnum].d[n][0];
  280. d1 = satcal[satnum].d[n][1];
  281. d2 = satcal[satnum].d[n][2];
  282. T[n] = d0;
  283. T[n] += d1 * C;
  284. C *= C;
  285. T[n] += d2 * C;
  286. }
  287. Tbb = (T[0] + T[1] + T[2] + T[3]) / 4.0;
  288. Tbb = satcal[satnum].rad[tpr->ch].A + satcal[satnum].rad[tpr->ch].B * Tbb;
  289. // Compute blackbody radiance temperature
  290. C = satcal[satnum].rad[tpr->ch].vc;
  291. tpr->Nbb = c1 * C * C * C / (expm1(c2 * C / Tbb));
  292. // Store blackbody count and space
  293. tpr->Cs = Cs * 4.0;
  294. tpr->Cb = t[14] * 4.0;
  295. }
  296. // IR channel temperature calibration
  297. static float tempcal(float Ce, int satnum, tempparam_t * rgpr) {
  298. float Nl, Nc, Ns, Ne;
  299. float T, vc;
  300. Ns = satcal[satnum].cor[rgpr->ch].Ns;
  301. Nl = Ns + (rgpr->Nbb - Ns) * (rgpr->Cs - Ce * 4.0) / (rgpr->Cs - rgpr->Cb);
  302. Nc = satcal[satnum].cor[rgpr->ch].b[0] +
  303. satcal[satnum].cor[rgpr->ch].b[1] * Nl +
  304. satcal[satnum].cor[rgpr->ch].b[2] * Nl * Nl;
  305. Ne = Nl + Nc;
  306. vc = satcal[satnum].rad[rgpr->ch].vc;
  307. T = c2 * vc / log(c1 * vc * vc * vc / Ne + 1.0);
  308. T = (T - satcal[satnum].rad[rgpr->ch].A) / satcal[satnum].rad[rgpr->ch].B;
  309. // Convert to celsius
  310. T -= 273.15;
  311. // Rescale to 0-255 for -100°C to +60°C
  312. T = (T + 100.0) / 160.0 * 255.0;
  313. return T;
  314. }
  315. // Temperature calibration wrapper
  316. void apt_temperature(int satnum, apt_image_t *img, int offset, int width){
  317. tempparam_t temp;
  318. tempcomp(tele, img->chB, satnum - 15, &temp);
  319. for (int y = 0; y < img->nrow; y++) {
  320. for (int x = 0; x < width; x++) {
  321. img->prow[y][x + offset] = (float)tempcal(img->prow[y][x + offset], satnum - 15, &temp);
  322. }
  323. }
  324. }