How is the lagcorrection factor calculated? I always get some ambiguity when I 
do matlab calculations. Are there any examples of calculations


I tried to implement it myself with matlab but I didn't get very good results. 
Do I need to normalize the input data in the program


Here is my matlab code I want to implement the fitting coefficient of FSRF But 
it didn't work out well


% Using ImageJ for operations:
% 1. Extract the last 10 frames of the open exposure, convert to 32-bit, 
compute the average, draw ROI, measure, and save as an Excel file
% 2. Extract the stack for the dark field, convert to 32-bit, draw ROI, use 
multi-measure, and save as an Excel file
% 3. Split the stack and merge the stack
% 4. Combine into one Excel file with open exposure data first and dark field 
data following
% 5. Copy the mean to a .txt file
% Result: mean.txt


mean2 = table2array(mean); % Convert imported table type to array type
m = mean2(1);
n = mean2(2:end);
nm = n / m;


% Normalization
FSRF = nm;


x = 1:510; % Number of dark field frames
x = x';
y = FSRF; % Dark field data


% Fitting function
fitfunc = @(params, x) (params(1) * exp(-params(2) * x) + params(3) * 
exp(-params(4) * x) + params(5) * exp(-params(6) * x) + params(7) * 
exp(-params(8) * x));
initialGuess = [7e-3, 0.001, 1e-2, 0.01, 1e-2, 0.08, 3e-2, 0.6]; % Initial 
values


paramsFit = lsqcurvefit(fitfunc, initialGuess, x, y);
B1_fit = paramsFit(1);
A1_fit = paramsFit(2);
B2_fit = paramsFit(3);
A2_fit = paramsFit(4);
B3_fit = paramsFit(5);
A3_fit = paramsFit(6);
B4_fit = paramsFit(7);
A4_fit = paramsFit(8);


y_fit = fitfunc(paramsFit, x);
plot(x, y, 'ro', x, y_fit, 'b-');
grid on
legend('Sample Data', 'Fitted Curve');


save AB.mat A1_fit A2_fit A3_fit A4_fit B1_fit B2_fit B3_fit B4_fit




Here is the program I used to fit the coefficients in matlab into c++


// TaoSunLagCorrectionTest.cpp : Defines the entry point for the console 
application.
//


#include "stdafx.h"
#include "TaoSunLagCorrection.h"
#include "rtkLagCorrectionImageFilter.h"
#include <chrono&gt; // For timing
#include <fstream&gt; // For file operations


// Suppress fopen warnings
#pragma warning(disable:4996)


int main()
{
&nbsp; &nbsp; int height = 1328;
&nbsp; &nbsp; int width = 1024;
&nbsp; &nbsp; int angles = 360;


&nbsp; &nbsp; //-------------------- Read projection data ----------------
&nbsp; &nbsp; FILE* file1;
&nbsp; &nbsp; const char* filename1 = 
"E:/TaoSunCUDA12/LagCorrection/data/Stack50-409_110kv 15macu 0.5mm 50hz - CT 
Phantom - full-fan.raw";


&nbsp; &nbsp; // Buffer to store read data
&nbsp; &nbsp; float* proj1 = (float*)malloc(height * width * angles * 
sizeof(float));
&nbsp; &nbsp; if (proj1 == NULL)
&nbsp; &nbsp; {
&nbsp; &nbsp; &nbsp; &nbsp; printf("Memory allocation failed.\n");
&nbsp; &nbsp; &nbsp; &nbsp; return -1;
&nbsp; &nbsp; }


&nbsp; &nbsp; file1 = fopen(filename1, "rb");
&nbsp; &nbsp; if (file1 == NULL)
&nbsp; &nbsp; {
&nbsp; &nbsp; &nbsp; &nbsp; printf("Cannot open file %s\n", filename1);
&nbsp; &nbsp; &nbsp; &nbsp; return -1;
&nbsp; &nbsp; }


&nbsp; &nbsp; // Read data from file into memory (vector)
&nbsp; &nbsp; printf("Starting to read projection ... \n");
&nbsp; &nbsp; fread(proj1, sizeof(float), height * width * angles, file1);
&nbsp; &nbsp; fclose(file1);




&nbsp; &nbsp; //--------------------- Correction ----------------------
&nbsp; &nbsp; float* projoutput = (float*)malloc(height * width * angles * 
sizeof(float));


&nbsp; &nbsp; // Start timing
&nbsp; &nbsp; auto start_time = std::chrono::high_resolution_clock::now();


&nbsp; &nbsp; // Lag correction parameters: detector (inherent 
characteristics), frame rate (default binning mode)
&nbsp; &nbsp; printf("Starting lag correction ... \n");
&nbsp; &nbsp; float m_A[4] = { 0.7055f, 0.141f, 0.0212f, 0.0033f };
&nbsp; &nbsp; float m_B[4] = { 2.911e-3f, 0.4454e-3f, 0.0748e-3f, 0.0042e-3f };
&nbsp; &nbsp; taoSun_LagCorrection(projoutput, proj1, m_A, m_B, height, width, 
angles);




&nbsp; &nbsp; // End timing
&nbsp; &nbsp; auto end_time = std::chrono::high_resolution_clock::now();


&nbsp; &nbsp; // Output elapsed time
&nbsp; &nbsp; std::chrono::duration<double&gt; elapsed = end_time - start_time;
&nbsp; &nbsp; std::cout << "Total execution time: " << elapsed.count() << " 
seconds" << std::endl;


&nbsp; &nbsp; // After processing all projections, write the large memory data 
to a file in one go
&nbsp; &nbsp; printf("Starting to write corrected projection ... \n");
&nbsp; &nbsp; std::ofstream 
outputFile("E:/TaoSunCUDA12/LagCorrection/data/output0902.raw", 
std::ios::binary); // Open a new raw file for writing processed data
&nbsp; &nbsp; if (!outputFile.is_open())
&nbsp; &nbsp; {
&nbsp; &nbsp; &nbsp; &nbsp; std::cerr << "Error: Cannot open output raw file!" 
<< std::endl;
&nbsp; &nbsp; &nbsp; &nbsp; return EXIT_FAILURE;
&nbsp; &nbsp; }
&nbsp; &nbsp; size_t totalSize = angles * height * width * sizeof(float);&nbsp; 
// Pre-calculate total memory required for all projection data
&nbsp; &nbsp; outputFile.write(reinterpret_cast<const char*&gt;(projoutput), 
totalSize);
&nbsp; &nbsp; outputFile.close(); // Close the file&nbsp; 


&nbsp; &nbsp; // Release the large allocated memory
&nbsp; &nbsp; free(projoutput);
&nbsp; &nbsp; free(proj1);


&nbsp; &nbsp; std::cout << "\n\nTest PASSED! " << std::endl;


&nbsp; &nbsp; return 0;
}
_______________________________________________
Rtk-users mailing list
rtk-us...@openrtk.org
https://www.creatis.insa-lyon.fr/mailman/listinfo/rtk-users

Reply via email to