Problem: Gaussian-weighted Binary Image Band Analysis

The problem involves locating the binary file AA, reading its specified 600x600 band (correcting BMP's bottom-top storage), and computing Gaussian-weighted metrics—sum, mean, standard deviation, and peak value—for the band, returning each metric as a designated output type.


Input/Output: The problem takes an integer band_num and reads a binary file "AA" from possible paths (test_data/AA, ../test_data/AA, etc.), where each band has 600x600 pixels stored in bottom-top order. It reads the specified band by skipping (band_num - 1) * 600 * 600 bytes and corrects the storage order by flipping rows. The problem computes four outputs: 1) a long long distance-weighted sum using the weight function 1 + 0.1 * exp(-( (i - 300)^2 + (j - 300)^2 ) / (2 * 10000)), 2) a double Gaussian-weighted mean with sigma = 150, 3) a double square root of the Gaussian-weighted variance (sigma=150), and 4) an integer peak pixel value from Gaussian-weighted detection (sigma=150). The outputs are the metrics for the specified band.

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

Sub-steps:
step_number: 1
step_description_prompt: Please write a C++ function named problem_3_1 that takes an int band_num as input and returns a long long. The function locates a binary file named "AA" from three possible paths ("test_data/AA", "../test_data/AA", "../../test_data/AA"); if none exist, output "Error: Cannot find data file AA" to cerr and return -1. The function signature is long long problem_3_1(int band_num). It reads a 600x600 band by skipping (band_num - 1) * 600 * 600 bytes from the file start, then reads each row into a buffer and assigns buffer[j] to band[599 - i][j] (to correct BMP's bottom-to-top storage). For each pixel (i,j), calculate weight as 1.0 + 0.1 * exp(-((i - 300) * (i - 300) + (j - 300) * (j - 300)) / 20000.0), multiply by band[i][j], cast to long long, accumulate to sum, and return the sum.
function_header: long problem_3_1(int band_num)
test_cases: // ==================== Main function: Testing ====================
int main() {
    srand(42);
    cout << fixed << setprecision(6);
    cout << "Problem 3.1" << endl;
    
    int correct_count = 0;
    
    // Test 1
    int band1 = 1;
    long long result1 = problem_3_1(band1);
    if (result1 < 0) {
        cout << "Wrong (File not found)" << endl;
    } else {
        long long correct1 = 29982424LL;  // Expected value (updated from actual result)
        if (result1 == correct1) {
            cout << "Correct" << endl;
            correct_count++;
        } else {
            cout << "Wrong" << endl;
        }
    }

    // Test 2
    int band2 = 3;
    long long result2 = problem_3_1(band2);
    if (result2 < 0) {
        cout << "Wrong (File not found)" << endl;
    } else {
        long long correct2 = 15929687LL;  // Expected value (updated from actual result)
        if (result2 == correct2) {
            cout << "Correct" << endl;
            correct_count++;
        } else {
            cout << "Wrong" << endl;
        }
    }

    // Test 3
    int band3 = 5;
    long long result3 = problem_3_1(band3);
    if (result3 < 0) {
        cout << "Wrong (File not found)" << endl;
    } else {
        long long correct3 = 21024381LL;  // 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: 2
step_description_prompt: Please write a C++ function named problem_3_2 which takes an integer band_num as input and returns a double. The function finds the AA file from possible paths, reads the specified band data, computes the Gaussian-weighted mean of the band data, and returns the result.
Function definition: double problem_3_2(int band_num);
Algorithm flow:
1. Check possible paths {"test_data/AA", "../test_data/AA", "../../test_data/AA"} to find the existing AA file (test with ifstream in binary mode). If no path exists, output "Error: Cannot find data file AA" to cerr and return -1.0.
2. Read the band data (600 rows × 600 columns) from the AA file in binary mode: Skip (band_num - 1) * 600 * 600 bytes using seekg. Read each row into an unsigned char buffer of size 600, then store rows in reverse order (band[rows - 1 - i][j] = buffer[j]) as BMP stores data from bottom to top.
3. Compute the Gaussian-weighted mean: Center at (300, 300) (half of 600), sigma = max(600, 600) / 4.0 = 150.0. For each pixel (i,j), calculate weight as exp(-((i - 300)*(i - 300) + (j - 300)*(j - 300)) / (2.0 * 150.0 * 150.0)). Accumulate sum of band[i][j] * weight and sum of weights.
4. Return sum / weight_sum if weight_sum > 1e-10, otherwise return 0.0.
Note: Use ifstream for file operations, ensure binary reading, and handle row reversal. Assume other called functions are pre-defined.
function_header: double problem_3_2(int band_num)
test_cases: // ==================== Main function: Testing ====================
int main() {
    srand(42);
    cout << fixed << setprecision(6);
    cout << "Problem 3.2" << endl;
    
    int correct_count = 0;
    const double tolerance = 1e-5;
    
    // Test 1
    int band1 = 1;
    double result1 = problem_3_2(band1);
    if (result1 < 0) {
        cout << "Wrong (File not found)" << endl;
    } else {
        double correct1 = 81.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 = 3;
    double result2 = problem_3_2(band2);
    if (result2 < 0) {
        cout << "Wrong (File not found)" << endl;
    } else {
        double correct2 = 42.733612;  // 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_3_2(band3);
    if (result3 < 0) {
        cout << "Wrong (File not found)" << endl;
    } else {
        double correct3 = 56.514335;  // 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: 3
step_description_prompt: Please write a C++ function named problem_3_3 that takes an integer band_num and returns a double, which calculates the weighted standard deviation of a specific band from the binary file AA.
The function definition is: double problem_3_3(int band_num);
Algorithm flow: 1. Search for the binary file AA in paths: "test_data/AA", "substep/test_data/AA", "../test_data/AA", "../../test_data/AA", "AA". If not found, output "Error: Cannot find data file AA" to cerr and return -1.0. 2. Read the band_num-th band (600x600 unsigned char pixels) from the file: skip (band_num-1)*600*600 bytes via seekg, read each row into a buffer, then store rows in reverse order (BMP bottom-to-top) into a 2D vector band (600x600). 3. Compute weighted mean: center at (300,300), sigma = 150 (max(600,600)/4), weight as exp(-((i-300)^2 + (j-300)^2)/(2*sigma²)). Calculate sum of band[i][j]*weight and weight_sum, mean = sum/weight_sum (avoid division by zero with 1e-10). 4. Compute weighted variance: sum weight*(band[i][j]-mean)², divide by weight_sum (check 1e-10). 5. Return sqrt(variance).
Use ifstream with ios::binary for file operations, vector for storage, and standard math functions like exp, sqrt. Assume necessary headers (<fstream>, <vector>, <cmath>, <iostream>) are included.
function_header: double problem_3_3(int band_num)
test_cases: // ==================== Main function: Testing ====================
int main() {
    srand(42);
    cout << fixed << setprecision(6);
    cout << "Problem 3.3" << endl;
    
    int correct_count = 0;
    const double tolerance = 1e-5;
    
    // Test 1
    int band1 = 1;
    double result1 = problem_3_3(band1);
    if (result1 < 0) {
        cout << "Wrong (File not found)" << endl;
    } else {
        double correct1 = 6.661131;  // 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_3_3(band2);
    if (result2 < 0) {
        cout << "Wrong (File not found)" << endl;
    } else {
        double correct2 = 9.701172;  // 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_3_3(band3);
    if (result3 < 0) {
        cout << "Wrong (File not found)" << endl;
    } else {
        double correct3 = 21.617286;  // 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_3_4 that takes an integer band_num as input and returns an integer, which implements finding the pixel value at the Gaussian-weighted peak location of a specified band from a binary file AA. The function definition is int problem_3_4(int band_num);. Algorithm steps: 1. Check possible paths ("test_data/AA", "../test_data/AA", "../../test_data/AA") to find the existing AA file in binary mode. 2. If no file is found, output "Error: Cannot find data file AA" to cerr and return -1. 3. Read the specified band from AA: image size is 600x600, skip (band_num - 1)*600*600 bytes from the start, read each row into a buffer, then store rows in reverse order (rows-1-i) to handle BMP's bottom-to-top storage. 4. Perform Gaussian-weighted peak detection: center at (300.0, 300.0), sigma = 600/4.0, compute weight as exp(-((i-300.0)^2 + (j-300.0)^2)/(2*sigma*sigma)), calculate weighted value as band[i][j] * weight, find the maximum weighted value and return the corresponding band[i][j]. Read AA in binary mode.
function_header: int problem_3_4(int band_num)
test_cases: // ==================== Main function: Testing ====================
int main() {
    srand(42);
    cout << fixed << setprecision(6);
    cout << "Problem 3.4" << endl;
    
    int correct_count = 0;
    
    // Test 1
    int band1 = 1;
    int result1 = problem_3_4(band1);
    if (result1 < 0) {
        cout << "Wrong (File not found)" << endl;
    } else {
        int correct1 = 151;  // 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_3_4(band2);
    if (result2 < 0) {
        cout << "Wrong (File not found)" << endl;
    } else {
        int correct2 = 109;  // 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_3_4(band3);
    if (result3 < 0) {
        cout << "Wrong (File not found)" << endl;
    } else {
        int correct3 = 195;  // 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;
}

General solution:

General tests: