optimize and make clean  this code "
#include <opencv2/objdetect.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/videoio.hpp>
#include <opencv2/highgui/highgui_c.h>
#include <opencv2/core.hpp>

#include <iostream>
#include <string>
#include <iterator>

#include "cnnLayers.hpp"
#include "AnomalyDetector.hpp"
#include "WaferDetector.hpp"
#include "MyUtility.hpp"

using namespace cv;
using namespace std;
//---------------------------------------------------------------/
Mat image;
Mat orgimage;
Mat displayedImage;
int magnifierSize = 200;
double magnificationFactor = 5.0;

// Function to handle mouse events
void onMouse(int event, int x, int y, int flags, void *userdata)
{
    if (event == cv::EVENT_LBUTTONDOWN)
    {
        // Define the region of interest (ROI)
        int halfSize = magnifierSize / 2;
        int startX = std::max(x - halfSize, 0);
        int startY = std::max(y - halfSize, 0);
        int endX = std::min(x + halfSize, image.cols);
        int endY = std::min(y + halfSize, image.rows);

        // Crop the ROI from the original image
        Rect roi(startX, startY, endX - startX, endY - startY);
        Mat cropped = orgimage(roi);
        // cv::Mat cropped = image(roi);

        // Magnify the cropped image
        Mat magnified;
        resize(cropped, magnified, cv::Size(), magnificationFactor, magnificationFactor, cv::INTER_LINEAR);

        // Display the original image
        displayedImage = orgimage.clone();
        // displayedImage = image.clone();

        // Calculate where to place the magnified part
        int displayX = std::min(startX, image.cols - magnified.cols);
        int displayY = std::min(startY, image.rows - magnified.rows);

        // Place the magnified part back onto the displayed image
        magnified.copyTo(displayedImage(Rect(displayX, displayY, magnified.cols, magnified.rows)));

        // Show the updated image
        destroyWindow("Magni");
        namedWindow("Magni", WINDOW_NORMAL);
        setWindowProperty("Magni", WND_PROP_TOPMOST, 1);
        imshow("Magni", magnified);
        imshow("Detections", image);
    }
}

AnomallyDetector anomallyDetector;
WaferDetector waferDetector;
int main(int argc, char *argv[])
{
    string commands = "{help |      | print this message}"
                      "{@image | data/1.jpg | input image}"
                      "{@preview | true | show Preview}"
                      "{@confidence | 0.45 | confidence}";

    CommandLineParser parser(argc, argv, commands);

    string help = argv[1];
    if (help == "-h")
    {
        cout << "-image {image address} -preview true -confidence 0.45 " << endl;

        vector<char> sss = {(char)0x61, (char)0x6C, (char)0x69, (char)0x6B, (char)0x68, (char)0x61, (char)0x6E, (char)0x69};

        std::string str(sss.begin(), sss.end());
        cout << str << endl;

        exit(1);
    }

    string imagePath = parser.get<string>("@image");
    bool showPreview = parser.get<bool>("@preview");
    float conf = parser.get<float>("@confidence");

    // Load an image
    image = imread(imagePath);
    orgimage = imread(imagePath);

    if (image.empty())
        return -1;

    auto start = chrono::high_resolution_clock::now();
    std::filesystem::path fs = imagePath;

    string resultPath = Utility::GenerateFolder("result", fs.filename());
    vector<Detection> results = waferDetector.DetectWafers(image, resultPath);
    std::string res = anomallyDetector.DetectAnomaly(image, results, resultPath, showPreview, conf);

    auto duration = chrono::duration_cast<chrono::milliseconds>(chrono::high_resolution_clock::now() - start);
    cout << "Detection completed in: " << duration.count() << " ms" << endl;

    displayedImage = image.clone();

    namedWindow("Detections", WINDOW_NORMAL);
    setMouseCallback("Detections", onMouse);

    Utility::addTextBottomLeftWithBackground(image, res);
    imwrite(resultPath + "000.jpg", image);

    imshow("Detections", image);

    // std::vector<cv::Mat> images = {image, waferDetector.wfDetect};
    // cv::Mat tiledImage = uti.createTiledImage(images, 800);
    // cv::namedWindow("Tiled Images", 0);
    // cv::imshow("Tiled Images", tiledImage);
    setWindowProperty("Detections", cv::WND_PROP_FULLSCREEN, 1);

    waitKey(0);

    return 0;
}
"