<meta http-equiv="Content-Type" content="text/html; charset=GB18030"><div><br></div><div><div>How is the lagcorrection factor calculated? I always get some ambiguity when I do matlab calculations. Are there any examples of calculations</div><div><br></div><div>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</div><div><br></div><div>Here is my matlab code I want to implement the fitting coefficient of FSRF But it didn't work out well</div><div><br></div><div>% Using ImageJ for operations:</div><div>% 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</div><div>% 2. Extract the stack for the dark field, convert to 32-bit, draw ROI, use multi-measure, and save as an Excel file</div><div>% 3. Split the stack and merge the stack</div><div>% 4. Combine into one Excel file with open exposure data first and dark field data following</div><div>% 5. Copy the mean to a .txt file</div><div>% Result: mean.txt</div><div><br></div><div>mean2 = table2array(mean); % Convert imported table type to array type</div><div>m = mean2(1);</div><div>n = mean2(2:end);</div><div>nm = n / m;</div><div><br></div><div>% Normalization</div><div>FSRF = nm;</div><div><br></div><div>x = 1:510; % Number of dark field frames</div><div>x = x';</div><div>y = FSRF; % Dark field data</div><div><br></div><div>% Fitting function</div><div>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));</div><div>initialGuess = [7e-3, 0.001, 1e-2, 0.01, 1e-2, 0.08, 3e-2, 0.6]; % Initial values</div><div><br></div><div>paramsFit = lsqcurvefit(fitfunc, initialGuess, x, y);</div><div>B1_fit = paramsFit(1);</div><div>A1_fit = paramsFit(2);</div><div>B2_fit = paramsFit(3);</div><div>A2_fit = paramsFit(4);</div><div>B3_fit = paramsFit(5);</div><div>A3_fit = paramsFit(6);</div><div>B4_fit = paramsFit(7);</div><div>A4_fit = paramsFit(8);</div><div><br></div><div>y_fit = fitfunc(paramsFit, x);</div><div>plot(x, y, 'ro', x, y_fit, 'b-');</div><div>grid on</div><div>legend('Sample Data', 'Fitted Curve');</div><div><br></div><div>save AB.mat A1_fit A2_fit A3_fit A4_fit B1_fit B2_fit B3_fit B4_fit</div><div><br></div><div><br></div><div>Here is the program I used to fit the coefficients in matlab into c++</div><div><br></div><div>// TaoSunLagCorrectionTest.cpp : Defines the entry point for the console application.</div><div>//</div><div><br></div><div>#include "stdafx.h"</div><div>#include "TaoSunLagCorrection.h"</div><div>#include "rtkLagCorrectionImageFilter.h"</div><div>#include <chrono> // For timing</div><div>#include <fstream> // For file operations</div><div><br></div><div>// Suppress fopen warnings</div><div>#pragma warning(disable:4996)</div><div><br></div><div>int main()</div><div>{</div><div>    int height = 1328;</div><div>    int width = 1024;</div><div>    int angles = 360;</div><div><br></div><div>    //-------------------- Read projection data ----------------</div><div>    FILE* file1;</div><div>    const char* filename1 = "E:/TaoSunCUDA12/LagCorrection/data/Stack50-409_110kv 15macu 0.5mm 50hz - CT Phantom - full-fan.raw";</div><div><br></div><div>    // Buffer to store read data</div><div>    float* proj1 = (float*)malloc(height * width * angles * sizeof(float));</div><div>    if (proj1 == NULL)</div><div>    {</div><div>        printf("Memory allocation failed.\n");</div><div>        return -1;</div><div>    }</div><div><br></div><div>    file1 = fopen(filename1, "rb");</div><div>    if (file1 == NULL)</div><div>    {</div><div>        printf("Cannot open file %s\n", filename1);</div><div>        return -1;</div><div>    }</div><div><br></div><div>    // Read data from file into memory (vector)</div><div>    printf("Starting to read projection ... \n");</div><div>    fread(proj1, sizeof(float), height * width * angles, file1);</div><div>    fclose(file1);</div><div><br></div><div><br></div><div>    //--------------------- Correction ----------------------</div><div>    float* projoutput = (float*)malloc(height * width * angles * sizeof(float));</div><div><br></div><div>    // Start timing</div><div>    auto start_time = std::chrono::high_resolution_clock::now();</div><div><br></div><div>    // Lag correction parameters: detector (inherent characteristics), frame rate (default binning mode)</div><div>    printf("Starting lag correction ... \n");</div><div>    float m_A[4] = { 0.7055f, 0.141f, 0.0212f, 0.0033f };</div><div>    float m_B[4] = { 2.911e-3f, 0.4454e-3f, 0.0748e-3f, 0.0042e-3f };</div><div>    taoSun_LagCorrection(projoutput, proj1, m_A, m_B, height, width, angles);</div><div><br></div><div><br></div><div>    // End timing</div><div>    auto end_time = std::chrono::high_resolution_clock::now();</div><div><br></div><div>    // Output elapsed time</div><div>    std::chrono::duration<double> elapsed = end_time - start_time;</div><div>    std::cout << "Total execution time: " << elapsed.count() << " seconds" << std::endl;</div><div><br></div><div>    // After processing all projections, write the large memory data to a file in one go</div><div>    printf("Starting to write corrected projection ... \n");</div><div>    std::ofstream outputFile("E:/TaoSunCUDA12/LagCorrection/data/output0902.raw", std::ios::binary); // Open a new raw file for writing processed data</div><div>    if (!outputFile.is_open())</div><div>    {</div><div>        std::cerr << "Error: Cannot open output raw file!" << std::endl;</div><div>        return EXIT_FAILURE;</div><div>    }</div><div>    size_t totalSize = angles * height * width * sizeof(float);  // Pre-calculate total memory required for all projection data</div><div>    outputFile.write(reinterpret_cast<const char*>(projoutput), totalSize);</div><div>    outputFile.close(); // Close the file  </div><div><br></div><div>    // Release the large allocated memory</div><div>    free(projoutput);</div><div>    free(proj1);</div><div><br></div><div>    std::cout << "\n\nTest PASSED! " << std::endl;</div><div><br></div><div>    return 0;</div><div>}</div><div><br></div></div><div><div style="font-size:14px;font-family:Verdana;color:#000;"><br></div></div>