#include "remove_border_main.h"
#include "remove_border.h"
#include <opencv2/imgproc.hpp>

using namespace std;

void remove_border_main(vector<SealInfo>& seal_set, bool is_debug) {
    int i = 0;
    
    for (auto& seal : seal_set) {
        if (seal.adj_img.empty()) {
            cout << "警告: seal.adj_img为空，跳过处理" << endl;
            continue;
        }
        
        cv::Mat gray;
        cv::cvtColor(seal.adj_img, gray, cv::COLOR_BGR2GRAY);
        
        cv::Mat mask;
        ShapeInfo new_shapeinfo;
        cv::RotatedRect inner_border;
        
        try {
            cv::Mat rm_bd_img = remove_border(gray, seal.shape_info, mask, new_shapeinfo, inner_border, 255);
            
            seal.rm_bd_img = rm_bd_img;
            seal.shape_info = new_shapeinfo;
            
            if (is_debug) {
                string window_name = "extracted_img_" + to_string(i);
                cout << "Debug: Processed seal " << i << " (" << window_name << ")" << endl;
            }
        } catch (const exception& e) {
            cout << "处理印章时出错: " << e.what() << endl;
        } catch (...) {
            cout << "处理印章时出错: 未知异常" << endl;
        }
        
        i++;
    }
    
    if (is_debug) {
        cout << "Debug: Processing completed" << endl;
    }
}
