Problem: Gaussian-Weighted Band Image Processing and Statistics

The problem focuses on developing C++ functions to process specified bands from a binary file—including file location, band reading/flipping, Gaussian-weighted image operations (e.g., contrast adjustment, gamma correction, histogram tasks), and statistical computations (e.g., standard deviation, mean, median) for image analysis.


Input/Output: The problem takes as input a binary file named "AA" in one of three paths ("test_data/AA", "../test_data/AA", "../../test_data/AA"), and task-specific parameters: an integer band_num (specifying the 600x600 band to read), plus optional parameters like double contrast_factor, int brightness_offset, int breakpoint, double slope1, double slope2, double gamma, or a second integer reference_band. It locates the file, reads the specified band (skipping preceding bytes and flipping rows vertically), applies operations (contrast/brightness adjustment, gamma correction, histogram equalization/matching), computes Gaussian-weighted values (center at midpoint, sigma = max(rows, cols)/4), and performs statistical calculations. Outputs include a double (weighted standard deviation, mean, or histogram-matched mean) or an integer (rounded difference, median, max mapped value); returns -1/-1.0 if the file is not found.

Required dependencies:
#include <vector>
#include <string>
#include <fstream>
#include <iostream>
#include <cmath>
#include <algorithm>
#include <iomanip>
#include <cstdlib>
#include <utility>
using namespace std;

Sub-steps:
step_number: 1
step_description_prompt: Please write a C++ function named problem_14_1 that calculates the Gaussian-weighted standard deviation of a specified band from a binary image file. The function takes an integer band_num and returns a double.

Function definition: double problem_14_1(int band_num);

Algorithm flow:
1. Check paths "test_data/AA", "../test_data/AA", "../../test_data/AA" to find the binary file "AA"; if not found, output "Error: Cannot find data file AA" to cerr and return -1.0.
2. Read the band (rows=600, cols=600): skip (band_num-1)*rows*cols bytes from the file start, read each row into a buffer, then assign to band with rows flipped (band[rows-1-i][j] = buf[j]).
3. Compute min and max values of the band.
4. Stretch values using (v - min_val)*255.0/(max_val - min_val + 1).
5. Apply Gaussian weights: center at (rows/2.0, cols/2.0), sigma=max(rows,cols)/4.0; weight=exp(-((i-center_i)^2 + (j-center_j)^2)/(2*sigma*sigma)).
6. Calculate weighted sum, weighted sum of squares, and total weight.
7. Compute weighted mean (weighted_sum/weight_sum if weight_sum>1e-10 else 0.0) and variance (weighted_sum_sq/weight_sum - mean*mean if weight_sum>1e-10 else 0.0); return sqrt(variance).

Use standard C++ libraries. Assume all called functions are defined. Read the file in binary mode and handle row flipping during band loading.
function_header: double problem_14_1(int band_num)
test_cases: // ==================== Main function: Testing ====================
int main() {
    srand(42);
    cout << fixed << setprecision(6);
    cout << "Problem 14.1" << endl;
    
    int correct_count = 0;
    const double tolerance = 1e-5;
    
    // Test 1
    int band1 = 1;

    double result1 = problem_14_1(band1);
    if (result1 < 0) {
        cout << "Wrong (File not found)" << endl;
    } else {
        double correct1 = 7.685921;  // Expected value (updated from actual result)
        if (abs(result1 - correct1) < tolerance) {
            cout << "Correct" << endl;
            correct_count++;
        } else {
            cout << "Wrong" << endl;
        }
    }
// Test 2
    int band2 = 3;

    double result2 = problem_14_1(band2);
    if (result2 < 0) {
        cout << "Wrong (File not found)" << endl;
    } else {
        double correct2 = 13.743327;  // Expected value (updated from actual result)
        if (abs(result2 - correct2) < tolerance) {
            cout << "Correct" << endl;
            correct_count++;
        } else {
            cout << "Wrong" << endl;
        }
    }
// Test 3
    int band3 = 5;

    double result3 = problem_14_1(band3);
    if (result3 < 0) {
        cout << "Wrong (File not found)" << endl;
    } else {
        double correct3 = 21.532844;  // Expected value (updated from actual result)
        if (abs(result3 - correct3) < tolerance) {
            cout << "Correct" << endl;
            correct_count++;
        } else {
            cout << "Wrong" << endl;
        }
    }

    
    if (correct_count == 3) {
        cout << "Completely correct!" << endl;
    }
    
    return 0;
}

step_number: 2
step_description_prompt: Please write a C++ function named problem_14_2 that implements calculating the difference between weighted maximum and minimum enhanced pixel values of a specific band from a binary file using Gaussian spatial weighting.
The function definition is int problem_14_2(int band_num, double contrast_factor).
Algorithm flow: 1. Check paths "test_data/AA", "../test_data/AA", "../../test_data/AA" to find the existing binary file; if none found, output "Error: Cannot find data file AA" to cerr and return -1. 2. Initialize a 600x600 vector<vector<unsigned char>> band. 3. Read band data: skip (band_num-1)*600*600 bytes, read each row into a buffer and store in band with reversed row order. 4. Calculate Gaussian weighting: center at (300, 300), sigma = 150. For each pixel, compute enhanced value as clamp((v-127.5)*contrast_factor +127.5 to 0-255, using min(255.0, max(0.0, ...))). Compute weight with exp(-( (i-center_i)^2 + (j-center_j)^2 )/(2*sigma*sigma)). Track weighted_max (enhanced*weight) and its max_weight, weighted_min and its min_weight. 5. Compute max_val = weighted_max/max_weight (if max_weight>1e-10 else 0), min_val = weighted_min/min_weight (if min_weight>1e-10 else 255). 6. Return (int)(max_val - min_val + 0.5).
Assume used functions are already defined. Read file in ios::binary mode.
function_header: int problem_14_2(int band_num, double contrast_factor)
test_cases: // ==================== Main function: Testing ====================
int main() {
    srand(42);
    cout << fixed << setprecision(6);
    cout << "Problem 14.2" << endl;
    
    int correct_count = 0;
    
    // Test 1
    int band1 = 1;
    double contrast1 = 1.5;

    int result1 = problem_14_2(band1, contrast1);
    if (result1 < 0) {
        cout << "Wrong (File not found)" << endl;
    } else {
        int correct1 = 162;  // Expected value (updated from actual result)
        if (result1 == correct1) {
            cout << "Correct" << endl;
            correct_count++;
        } else {
            cout << "Wrong" << endl;
        }
    }
// Test 2
    int band2 = 2;
    double contrast2 = 2.0;

    int result2 = problem_14_2(band2, contrast2);
    if (result2 < 0) {
        cout << "Wrong (File not found)" << endl;
    } else {
        int correct2 = 48;  // Expected value (updated from actual result)
        if (result2 == correct2) {
            cout << "Correct" << endl;
            correct_count++;
        } else {
            cout << "Wrong" << endl;
        }
    }
// Test 3
    int band3 = 3;
    double contrast3 = 2.5;

    int result3 = problem_14_2(band3, contrast3);
    if (result3 < 0) {
        cout << "Wrong (File not found)" << endl;
    } else {
        int correct3 = 126;  // Expected value (updated from actual result)
        if (result3 == correct3) {
            cout << "Correct" << endl;
            correct_count++;
        } else {
            cout << "Wrong" << endl;
        }
    }

    
    if (correct_count == 3) {
        cout << "Completely correct!" << endl;
    }
    
    return 0;
}

step_number: 3
step_description_prompt: Please write a C++ function named problem_14_3 that calculates the Gaussian-weighted mean brightness of a specific band from a binary file after applying a brightness offset.

The function definition is: double problem_14_3(int band_num, int brightness_offset);

Algorithm flow: 1. Check three paths ("test_data/AA", "../test_data/AA", "../../test_data/AA") in order to find the existing binary file. If no file is found, output "Error: Cannot find data file AA" to cerr and return -1.0. 2. Read the specified band (band_num) from the binary file: skip (band_num - 1) * 600 * 600 bytes from the start, read 600x600 unsigned char data, and store it in a 2D vector band with vertical flipping (band[rows-1-i][j] = buf[j]). 3. Compute Gaussian-weighted mean: adjust each pixel's brightness to [0,255] by adding brightness_offset, calculate weight for each pixel using Gaussian function (sigma = 600/4.0) based on squared distance to the center (300,300), sum the product of adjusted brightness and weight, sum all weights, and return the ratio if total weight > 1e-10 else 0.0.

Note: Use binary mode to read the file, check if the file is open before operations, and use seekg to skip bytes. The rows and cols are fixed at 600. Use unsigned char for pixel data, and handle memory for buf (allocate with new, delete after use). Output error to cerr if the file is not found.
function_header: double problem_14_3(int band_num, int brightness_offset)
test_cases: // ==================== Main function: Testing ====================
int main() {
    srand(42);
    cout << fixed << setprecision(6);
    cout << "Problem 14.3" << endl;
    
    int correct_count = 0;
    const double tolerance = 1e-5;
    
    // Test 1
    int band1 = 1;
    int brightness1 = 10;

    double result1 = problem_14_3(band1, brightness1);
    if (result1 < 0) {
        cout << "Wrong (File not found)" << endl;
    } else {
        double correct1 = 91.600296;  // Expected value (updated from actual result)
        if (abs(result1 - correct1) < tolerance) {
            cout << "Correct" << endl;
            correct_count++;
        } else {
            cout << "Wrong" << endl;
        }
    }
// Test 2
    int band2 = 2;
    int brightness2 = 20;

    double result2 = problem_14_3(band2, brightness2);
    if (result2 < 0) {
        cout << "Wrong (File not found)" << endl;
    } else {
        double correct2 = 57.389516;  // Expected value (updated from actual result)
        if (abs(result2 - correct2) < tolerance) {
            cout << "Correct" << endl;
            correct_count++;
        } else {
            cout << "Wrong" << endl;
        }
    }
// Test 3
    int band3 = 3;
    int brightness3 = -10;

    double result3 = problem_14_3(band3, brightness3);
    if (result3 < 0) {
        cout << "Wrong (File not found)" << endl;
    } else {
        double correct3 = 32.733612;  // Expected value (updated from actual result)
        if (abs(result3 - correct3) < tolerance) {
            cout << "Correct" << endl;
            correct_count++;
        } else {
            cout << "Wrong" << endl;
        }
    }

    
    if (correct_count == 3) {
        cout << "Completely correct!" << endl;
    }
    
    return 0;
}

step_number: 4
step_description_prompt: Please write a C++ function named problem_14_4 that takes int band_num, int breakpoint, double slope1, double slope2 as parameters and returns an int. This function calculates the weighted median of a specific band from a binary data file with spatial Gaussian weighting.
The function definition is int problem_14_4(int band_num, int breakpoint, double slope1, double slope2).
Algorithm steps: 1. Check three paths ("test_data/AA", "../test_data/AA", "../../test_data/AA") in order to find the existing binary file. If no file is found, output "Error: Cannot find data file AA" to cerr and return -1. 2. Read 600x600 band data: skip (band_num-1)*600*600 bytes from the file start, then read each row, store rows in reverse order (rows-1-i for row index). 3. For each pixel value v: apply piecewise linear transformation (v < breakpoint ? v*slope1 : breakpoint*slope1 + (v-breakpoint)*slope2), clamp the result to 0-255. 4. Calculate Gaussian weight for each pixel: center at (300, 300), sigma = max(600,600)/4, weight = exp(-( (i-300)*(i-300) + (j-300)*(j-300) )/(2*sigma*sigma)). 5. Store each transformed value and its weight in a vector, then sort the vector by transformed value. 6. Compute total weight, iterate through the sorted vector to find the first value where cumulative weight >= total_weight/2 (this is the weighted median). 7. Return the weighted median rounded to the nearest integer (add 0.5 then cast to int).
Note: Functions used (ifstream, vector operations, sort, exp, max, min) are assumed to be available. The binary file reading must handle band skipping and row reversal as specified.
function_header: int problem_14_4(int band_num, int breakpoint, double slope1, double slope2)
test_cases: // ==================== Main function: Testing ====================
int main() {
    srand(42);
    cout << fixed << setprecision(6);
    cout << "Problem 14.4" << endl;
    
    int correct_count = 0;
    
    // Test 1
    int band1 = 1;
    int breakpoint1 = 128;
    double slope1_1 = 1.2, slope2_1 = 0.8;

    int result1 = problem_14_4(band1, breakpoint1, slope1_1, slope2_1);
    if (result1 < 0) {
        cout << "Wrong (File not found)" << endl;
    } else {
        int correct1 = 98;  // Expected value (updated from actual result)
        if (result1 == correct1) {
            cout << "Correct" << endl;
            correct_count++;
        } else {
            cout << "Wrong" << endl;
        }
    }
// Test 2
    int band2 = 2;
    int breakpoint2 = 100;
    double slope1_2 = 1.5, slope2_2 = 0.7;

    int result2 = problem_14_4(band2, breakpoint2, slope1_2, slope2_2);
    if (result2 < 0) {
        cout << "Wrong (File not found)" << endl;
    } else {
        int correct2 = 55;  // Expected value (updated from actual result)
        if (result2 == correct2) {
            cout << "Correct" << endl;
            correct_count++;
        } else {
            cout << "Wrong" << endl;
        }
    }
// Test 3
    int band3 = 3;
    int breakpoint3 = 150;
    double slope1_3 = 1.0, slope2_3 = 1.0;

    int result3 = problem_14_4(band3, breakpoint3, slope1_3, slope2_3);
    if (result3 < 0) {
        cout << "Wrong (File not found)" << endl;
    } else {
        int correct3 = 42;  // Expected value (updated from actual result)
        if (result3 == correct3) {
            cout << "Correct" << endl;
            correct_count++;
        } else {
            cout << "Wrong" << endl;
        }
    }

    
    if (correct_count == 3) {
        cout << "Completely correct!" << endl;
    }
    
    return 0;
}

step_number: 5
step_description_prompt: Please write a C++ function named problem_14_5 that takes int band_num and double gamma as parameters, and returns an int. It calculates a weighted median of gamma-corrected pixel values from a specified band in a binary file, with Gaussian spatial weighting.

Function definition: int problem_14_5(int band_num, double gamma);

Algorithm flow: Check three paths ("test_data/AA", "../test_data/AA", "../../test_data/AA") to find the existing file. If no path exists, output "Error: Cannot find data file AA" to cerr and return -1. Read 600x600 pixel data: skip (band_num - 1) * 600 * 600 bytes from the file start, read each row into a buffer, then assign to band in reverse row order (band[rows-1 -i][j] = buf[j]). For each pixel, apply gamma correction as 255 * pow(v/255.0, gamma), clamp to 0-255. Calculate Gaussian weight using distance to center (300, 300) and sigma = 150 (max(600,600)/4). Collect pairs of (corrected value, weight). Sort pairs by corrected value. Compute total weight, then find the weighted median (first value where cumulative weight >= total_weight/2). Return the rounded weighted median (add 0.5 before casting to int).

Assume required headers (iostream, fstream, vector, algorithm, cmath) and functions (ifstream::open, seekg, read, close, vector operations, sort, exp, pow, max, min) are already available.
function_header: int problem_14_5(int band_num, double gamma)
test_cases: // ==================== Main function: Testing ====================
int main() {
    srand(42);
    cout << fixed << setprecision(6);
    cout << "Problem 14.5" << endl;
    
    int correct_count = 0;
    
    // Test 1
    int band1 = 1;
    double gamma1 = 1.2;

    int result1 = problem_14_5(band1, gamma1);
    if (result1 < 0) {
        cout << "Wrong (File not found)" << endl;
    } else {
        int correct1 = 65;  // Expected value (updated from actual result)
        if (result1 == correct1) {
            cout << "Correct" << endl;
            correct_count++;
        } else {
            cout << "Wrong" << endl;
        }
    }
// Test 2
    int band2 = 2;
    double gamma2 = 1.5;

    int result2 = problem_14_5(band2, gamma2);
    if (result2 < 0) {
        cout << "Wrong (File not found)" << endl;
    } else {
        int correct2 = 14;  // Expected value (updated from actual result)
        if (result2 == correct2) {
            cout << "Correct" << endl;
            correct_count++;
        } else {
            cout << "Wrong" << endl;
        }
    }
// Test 3
    int band3 = 3;
    double gamma3 = 0.8;

    int result3 = problem_14_5(band3, gamma3);
    if (result3 < 0) {
        cout << "Wrong (File not found)" << endl;
    } else {
        int correct3 = 60;  // Expected value (updated from actual result)
        if (result3 == correct3) {
            cout << "Correct" << endl;
            correct_count++;
        } else {
            cout << "Wrong" << endl;
        }
    }

    
    if (correct_count == 3) {
        cout << "Completely correct!" << endl;
    }
    
    return 0;
}

step_number: 6
step_description_prompt: Please write a C++ function named problem_14_6 that takes an integer band_num and returns an integer. This function reads a specific band from a binary file, performs spatially weighted histogram equalization on the band data, and returns the maximum value after equalization.
Function definition: int problem_14_6(int band_num);
Algorithm flow: 1. Check paths "test_data/AA", "../test_data/AA", "../../test_data/AA" in order to find the existing binary file; if no file is found, output "Error: Cannot find data file AA" to cerr and return -1. 2. Initialize a 600x600 vector of unsigned char for band data. 3. Read band data: calculate skip bytes as (band_num-1)*600*600, seek to that position in the file. Read each row into a buffer, then assign to band in reverse row order (band[599 - i][j] = buf[j] for row i from file). 4. Compute spatially weighted histogram: center at (300.0, 300.0), sigma = 150. For each pixel, calculate weight as exp(-(distance to center squared)/(2*sigma*sigma)), accumulate into histogram and total_weight. 5. Compute cumulative distribution function (CDF) from the histogram. 6. Create intensity mapping using CDF normalized by total_weight (handle total_weight near zero). 7. Apply mapping to band data, track the maximum value of the new pixels. 8. Return the maximum value.
Note: Use ifstream with ios::binary for file operations. Assume exp, max, and vector operations are available. Reverse row order when reading band data. Use Gaussian weight for spatial weighting. Output error to cerr if file not found.
function_header: int problem_14_6(int band_num)
test_cases: // ==================== Main function: Testing ====================
int main() {
    srand(42);
    cout << fixed << setprecision(6);
    cout << "Problem 14.6" << endl;
    
    int correct_count = 0;
    
    // Test 1
    int band1 = 1;

    int result1 = problem_14_6(band1);
    if (result1 < 0) {
        cout << "Wrong (File not found)" << endl;
    } else {
        int correct1 = 254;  // Expected value (updated from actual result)
        if (result1 == correct1) {
            cout << "Correct" << endl;
            correct_count++;
        } else {
            cout << "Wrong" << endl;
        }
    }
// Test 2
    int band2 = 3;

    int result2 = problem_14_6(band2);
    if (result2 < 0) {
        cout << "Wrong (File not found)" << endl;
    } else {
        int correct2 = 254;  // Expected value (updated from actual result)
        if (result2 == correct2) {
            cout << "Correct" << endl;
            correct_count++;
        } else {
            cout << "Wrong" << endl;
        }
    }
// Test 3
    int band3 = 5;

    int result3 = problem_14_6(band3);
    if (result3 < 0) {
        cout << "Wrong (File not found)" << endl;
    } else {
        int correct3 = 254;  // Expected value (updated from actual result)
        if (result3 == correct3) {
            cout << "Correct" << endl;
            correct_count++;
        } else {
            cout << "Wrong" << endl;
        }
    }

    
    if (correct_count == 3) {
        cout << "Completely correct!" << endl;
    }
    
    return 0;
}

step_number: 7
step_description_prompt: Please write a C++ function named problem_14_7 that takes two int parameters (source_band, reference_band) and returns a double. The function computes a Gaussian-weighted histogram-matching result between two bands from a binary file.

Function definition: double problem_14_7(int source_band, int reference_band);

Algorithm flow:
1. Iterate through paths {"test_data/AA", "../test_data/AA", "../../test_data/AA"}; open each in binary mode, if accessible, close it and set filepath to this path, break. If filepath is empty, output "Error: Cannot find data file AA" to cerr and return -1.0.
2. Read source band: rows=600, cols=600. Initialize source as vector<vector<unsigned char>>(rows, vector<unsigned char>(cols, 0)). Open filepath as file1 (binary), skip (source_band-1)*rows*cols bytes via seekg. Allocate unsigned char* buf = new unsigned char[cols]; for i=0 to rows-1: read cols bytes into buf (cast to char*), copy buf[j] to source[rows-1-i][j]. Delete buf, close file1.
3. Read reference band similarly: initialize reference as 600x600 vector, open filepath as file2, skip (reference_band-1)*rows*cols bytes, read rows into buf, copy to reference[rows-1-i][j]. Delete buf, close file2.
4. Compute source's Gaussian-weighted histogram H and CDF S: center_i=rows/2.0, center_j=cols/2.0, sigma=max(rows,cols)/4.0. For each pixel, calculate weight=exp(-((i-center_i)^2+(j-center_j)^2)/(2*sigma²)), accumulate H[v] and total_weight_src. Compute S (S[0]=H[0], S[i]=S[i-1]+H[i]), normalize S to [0,255] (divide by total_weight_src or 1 if <1e-10).
5. Compute reference's Gaussian-weighted histogram G and CDF GS similarly.
6. Create SG: for each i (0-255), find j (0-255) minimizing |S[i]-GS[j]|, set SG[i]=j.
7. Compute final result: for each pixel, get matched_val=SG[source[i][j]], calculate weight as step4, accumulate weighted_sum=matched_val*weight and weight_sum. Return weighted_sum/weight_sum (0 if weight_sum<1e-10).
function_header: double problem_14_7(int source_band, int reference_band)
test_cases: // ==================== Main function: Testing ====================
int main() {
    srand(42);
    cout << fixed << setprecision(6);
    cout << "Problem 14.7" << endl;
    
    int correct_count = 0;
    const double tolerance = 1e-5;
    
    // Test 1
    int source1 = 1, reference1 = 2;

    double result1 = problem_14_7(source1, reference1);
    if (result1 < 0) {
        cout << "Wrong (File not found)" << endl;
    } else {
        double correct1 = 37.279443;  // Expected value (updated from actual result)
        if (abs(result1 - correct1) < tolerance) {
            cout << "Correct" << endl;
            correct_count++;
        } else {
            cout << "Wrong" << endl;
        }
    }
// Test 2
    int source2 = 2, reference2 = 3;

    double result2 = problem_14_7(source2, reference2);
    if (result2 < 0) {
        cout << "Wrong (File not found)" << endl;
    } else {
        double correct2 = 43.257028;  // Expected value (updated from actual result)
        if (abs(result2 - correct2) < tolerance) {
            cout << "Correct" << endl;
            correct_count++;
        } else {
            cout << "Wrong" << endl;
        }
    }
// Test 3
    int source3 = 3, reference3 = 4;

    double result3 = problem_14_7(source3, reference3);
    if (result3 < 0) {
        cout << "Wrong (File not found)" << endl;
    } else {
        double correct3 = 45.834086;  // Expected value (updated from actual result)
        if (abs(result3 - correct3) < tolerance) {
            cout << "Correct" << endl;
            correct_count++;
        } else {
            cout << "Wrong" << endl;
        }
    }

    
    if (correct_count == 3) {
        cout << "Completely correct!" << endl;
    }
    
    return 0;
}

step_number: 8
step_description_prompt: Please write a C++ function named problem_14_8 that takes an integer band_num as input and returns a double, which calculates a score by integrating results from multiple band enhancement operations. The function is defined as double problem_14_8(int band_num);. First, search for the file "AA" in paths {"test_data/AA", "../test_data/AA", "../../test_data/AA"}; if not found, output "Error: Cannot find data file AA" to cerr and return -1.0. Then read the specified band (band_num) from the binary file with size 600x600 (rows=600, cols=600), reverse the row order (store as rows-1-i) into a 2D vector band (vector<vector<unsigned char>>). Next, perform six operations: 1) Weighted linear stretch (compute min/max of band, stretch value, weighted mean/variance, linear_stretch=sqrt(variance)). 2) Contrast enhancement (factor=1.5, compute max-min of enhanced values as contrast). 3) Weighted brightness adjustment (offset=10, compute weighted mean of adjusted values as brightness). 4) Multi-segment S-curve transformation (breakpoint=128, slope1=1.2, slope2=0.8, take median of transformed values as s_curve). 5) Adaptive gamma correction (gamma=1.2, take median of corrected values as gamma_val). 6) Weighted histogram equalization (compute weighted histogram, cumulative distribution, mapping, take max mapped value as histogram_eq). Normalize each result (linear_stretch/73, contrast/255, brightness/255, s_curve/255, gamma_val/255, histogram_eq/255), then calculate the final score with weights 0.2, 0.2, 0.15, 0.15, 0.15, 0.15 respectively and return it. Use ios::binary for file operations; skip (band_num-1)*rows*cols bytes when reading the band. Assume other called functions are already defined.</think>Please write a C++ function named problem_14_8 that takes an integer band_num as input and returns a double, which calculates a score by integrating results from multiple band enhancement operations. The function is defined as double problem_14_8(int band_num);. First, search for the file "AA" in paths {"test_data/AA", "../test_data/AA", "../../test_data/AA"}; if not found, output "Error: Cannot find data file AA" to cerr and return -1.0. Then read the specified band (band_num) from the binary file with size 600x600 (rows=600, cols=600), reverse the row order (store as rows-1-i) into a 2D vector band (vector<vector<unsigned char>>). Next, perform six operations: 1) Weighted linear stretch (compute min/max of band, stretch value, weighted mean/variance, linear_stretch=sqrt(variance)). 2) Contrast enhancement (factor=1.5, compute max-min of enhanced values as contrast). 3) Weighted brightness adjustment (offset=10, compute weighted mean of adjusted values as brightness). 4) Multi-segment S-curve transformation (breakpoint=128, slope1=1.2, slope2=0.8, take median of transformed values as s_curve). 5) Adaptive gamma correction (gamma=1.2, take median of corrected values as gamma_val). 6) Weighted histogram equalization (compute weighted histogram, cumulative distribution, mapping, take max mapped value as histogram_eq). Normalize each result (linear_stretch/73, contrast/255, brightness/255, s_curve/255, gamma_val/255, histogram_eq/255), then calculate the final score with weights 0.2, 0.2, 0.15, 0.15, 0.15, 0.15 respectively and return it. Use ios::binary for file operations; skip (band_num-1)*rows*cols bytes when reading the band. Assume other called functions are already defined.
function_header: double problem_14_8(int band_num)
test_cases: // ==================== Main function: Testing ====================
int main() {
    srand(42);
    cout << fixed << setprecision(6);
    cout << "Problem 14.8" << endl;
    
    int correct_count = 0;
    const double tolerance = 1e-5;
    
    // Test 1
    int band1 = 1;

    double result1 = problem_14_8(band1);
    if (result1 < 0) {
        cout << "Wrong (File not found)" << endl;
    } else {
        double correct1 = 0.520234;  // Expected value (updated from actual result)
        if (abs(result1 - correct1) < tolerance) {
            cout << "Correct" << endl;
            correct_count++;
        } else {
            cout << "Wrong" << endl;
        }
    }
// Test 2
    int band2 = 3;

    double result2 = problem_14_8(band2);
    if (result2 < 0) {
        cout << "Wrong (File not found)" << endl;
    } else {
        double correct2 = 0.450830;  // Expected value (updated from actual result)
        if (abs(result2 - correct2) < tolerance) {
            cout << "Correct" << endl;
            correct_count++;
        } else {
            cout << "Wrong" << endl;
        }
    }
// Test 3
    int band3 = 5;

    double result3 = problem_14_8(band3);
    if (result3 < 0) {
        cout << "Wrong (File not found)" << endl;
    } else {
        double correct3 = 0.516944;  // Expected value (updated from actual result)
        if (abs(result3 - correct3) < tolerance) {
            cout << "Correct" << endl;
            correct_count++;
        } else {
            cout << "Wrong" << endl;
        }
    }

    
    if (correct_count == 3) {
        cout << "Completely correct!" << endl;
    }
    
    return 0;
}

step_number: 9
step_description_prompt: Please write a C++ function named problem_14_9 that takes two integers (band1, band2) as parameters and returns a double. The function calculates the absolute difference between the scores of two specified bands from a binary data file, where each score is a weighted sum of six normalized features (linear stretch, contrast, brightness, S-curve transformation, gamma correction, weighted histogram equalization).

Function definition: double problem_14_9(int band1, int band2);

Algorithm flow: 1. Search for the binary data file "AA" in paths {"test_data/AA", "../test_data/AA", "../../test_data/AA"}. If not found, output "Error: Cannot find data file AA" to stderr and return -1.0. 2. Read the specified bands (band1, band2) from the file: each band is a 600x600 unsigned char matrix. For each band, skip (band-1)*600*600 bytes from the start, read row by row, and reverse the row order (store row i as rows-1-i). 3. For each band, compute six features: a) Linear stretch: Calculate weighted mean and variance of stretched values (stretched = (v - min_val) * 255.0 / (max_val - min_val + 1)) using Gaussian weight (sigma = max(600,600)/4, weight = exp(-( (i - 300.0)^2 + (j - 300.0)^2 ) / (2*sigma*sigma))), then take the square root of variance. b) Contrast: Enhance contrast with factor 1.5 (enhanced = min(255.0, max(0.0, (v - 127.5)*1.5 + 127.5))), then compute max - min of enhanced values. c) Brightness: Adjust brightness by adding 10 (clipped to 0-255), then calculate weighted mean using the same Gaussian weight. d) S-curve transformation: Transform values with slope 1.2 for v < 128 and 0.8 for v >= 128 (clipped to 0-255), sort the transformed values, take the median. e) Gamma correction: Apply gamma = 1.2 (corrected = 255.0 * pow(v / 255.0, 1.2), clipped to 0-255), sort the corrected values, take the median. f) Weighted histogram equalization: Compute weighted histogram (using Gaussian weight), cumulative distribution function, map values to 0-255, then take the max mapped value. 4. Normalize each feature: linear stretch by dividing by 73.0, others by 255.0. Compute the score for each band as weighted sum (0.2 for linear stretch, 0.2 for contrast, 0.15 for brightness, 0.15 for S-curve, 0.15 for gamma, 0.15 for histogram equalization). 5. Return the absolute difference between the two scores.

Note: Use binary mode to read the file. For each band, use ifstream with ios::binary, seekg to skip the correct number of bytes, read each row into a buffer, then reverse the row order when storing into the matrix. Use vectors to store matrices and intermediate values. Handle division by zero by checking if the weight sum is greater than 1e-10 before dividing.
function_header: double problem_14_9(int band1, int band2)
test_cases: // ==================== Main function: Testing ====================
int main() {
    srand(42);
    cout << fixed << setprecision(6);
    cout << "Problem 14.9" << endl;
    
    int correct_count = 0;
    const double tolerance = 1e-5;
    
    // Test 1
    int band1_1 = 1, band2_1 = 2;

    double result1 = problem_14_9(band1_1, band2_1);
    if (result1 < 0) {
        cout << "Wrong (File not found)" << endl;
    } else {
        double correct1 = 0.185438;  // Expected value (updated from actual result)
        if (abs(result1 - correct1) < tolerance) {
            cout << "Correct" << endl;
            correct_count++;
        } else {
            cout << "Wrong" << endl;
        }
    }
// Test 2
    int band1_2 = 2, band2_2 = 3;

    double result2 = problem_14_9(band1_2, band2_2);
    if (result2 < 0) {
        cout << "Wrong (File not found)" << endl;
    } else {
        double correct2 = 0.116034;  // Expected value (updated from actual result)
        if (abs(result2 - correct2) < tolerance) {
            cout << "Correct" << endl;
            correct_count++;
        } else {
            cout << "Wrong" << endl;
        }
    }
// Test 3
    int band1_3 = 3, band2_3 = 4;

    double result3 = problem_14_9(band1_3, band2_3);
    if (result3 < 0) {
        cout << "Wrong (File not found)" << endl;
    } else {
        double correct3 = 0.019231;  // Expected value (updated from actual result)
        if (abs(result3 - correct3) < tolerance) {
            cout << "Correct" << endl;
            correct_count++;
        } else {
            cout << "Wrong" << endl;
        }
    }

    
    if (correct_count == 3) {
        cout << "Completely correct!" << endl;
    }
    
    return 0;
}

General solution:

General tests: