JZIRC_Lib.c 23.6 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "JZsdkLib.h"


#include "../IRC_data_deal/IRC_data_deal.h"

#include "MediaProc/IRC_funtion/IRC_param.h"
#include "MediaProc/ImageProc/PseudoColor/PseudoColor.h"

#include "MediaProc/ImageProc/BadPixelProc/BadPixelProc.h"
#include "MediaProc/MediaProc_Param.h"
#include "MediaProc/IRC_funtion/IRC_data_deal/IRC_data_deal.h"

/**********************************************
 * 
 *  直方均衡, 提高图像的对比度,以降低过曝和曝光不足问题
 *  从直方图来看,即将 密集的直方图向两边扩散,从而变疏和均匀
 *  https://blog.csdn.net/qq_43416206/article/details/138680036
 * 
 * 
 *  输入源数据 直方图数据 输出承载数组 配置信息
 * *****************************************************/
static T_JZsdkReturnCode StraightBalancing_U16(U16_t *rawData, unsigned int *StraightDraw, U16_t *outData, struct IRC_param *dealInfo )
{
    double result=0;

    for (int i = 0; i < dealInfo->PixelNum; i++)
    {
        result = 0.0;
        for (int temp = 0; temp < rawData[i]; temp++)
        {
            result += StraightDraw[temp];
        }

        result = (result*dealInfo->ExpectedMax) / dealInfo->PixelNum;
        outData[i] =  (U16_t)((int)result); 
    }

    return JZ_ERROR_SYSTEM_MODULE_CODE_SUCCESS;
}



/**********************************************
 * 
 *  直方拉伸
 *  https://blog.csdn.net/qq_43416206/article/details/138680036
 *  输入源数据 直方图数据 输出承载数组 配置信息
 * *****************************************************/
static T_JZsdkReturnCode StraightStretching(U16_t *rawData, unsigned int *StraightDraw, U16_t *outData, struct IRC_param *dealInfo )
{
    int LeftDrop = dealInfo->LeftDrop;
    int RightDrop = dealInfo->RightDrop;
    int hist_cnt = 0;
    int find_min = 0, find_max = 0;
    int LowIndex = 0, HighIndex = 0;
    for (int i = 0; i < dealInfo->ExpectedMax; i++)
    {
        hist_cnt += StraightDraw[i];
        
        if ((hist_cnt >= LeftDrop) && (!find_min))
        {
            
            LowIndex = i;
            //JZSDK_LOG_INFO("lowindex:%d", LowIndex);
            find_min = 1;
        }
        if((hist_cnt >= (dealInfo->PixelNum - RightDrop) )&&(!find_max))
        {
            HighIndex = i;
            //JZSDK_LOG_INFO("HighIndex:%d", HighIndex);
            find_max = 1;
        }
    }

    if (HighIndex == LowIndex)
    {
        return JZ_ERROR_SYSTEM_MODULE_CODE_FAILURE;
    }
    
    for (int i = 0; i < dealInfo->PixelNum; i++)
    {
        if (rawData[i]<= LowIndex) 
        {
            outData[i]= 0;
        }
        else if (rawData[i]>= HighIndex) 
        {
            outData[i]= dealInfo->ExpectedMax;
        }
        else
        { 
            outData[i]= ((rawData[i]- LowIndex)*dealInfo->ExpectedMax) /(HighIndex- LowIndex);
        }
    }
    
    return JZ_ERROR_SYSTEM_MODULE_CODE_SUCCESS;
}


/*计算直方图均衡化并转换为8位灰度值
	    U16_t in_str 输入的数据
                int in_str_len 输入的数据长度
    灰度0为黑色,灰度255为白色
*/
static T_JZsdkReturnCode JZsdk_vKT(U16_t *in_str, U8_t **out_str, int *out_str_len, struct IRC_param *dealInfo)
{
    //无图像
    if (in_str == NULL || dealInfo == NULL) 
    {  
        return JZ_ERROR_SYSTEM_MODULE_CODE_INVALID_PARAMETER;  
    }  

    // 分配输出结果的内存空间
    *out_str = (unsigned char*)malloc(sizeof(unsigned char) * dealInfo->PixelNum);
    if (*out_str == NULL) {
        // 如果内存分配失败,打印错误信息并返回空指针
        JZSDK_LOG_ERROR("Error: Memory allocation failed\n");
        return JZ_ERROR_SYSTEM_MODULE_CODE_INVALID_PARAMETER;
    }

/**********************************************
 * 
 *  获取图像的直方图
 * 
 * 
 * *****************************************************/

    // 初始化变量
    unsigned int ThisPixelGray = 0;     //当前处理像素点的灰度值
    long SumAllLegalGray = 0;       //存储的总灰度值

    // 分配当前灰度值出现次数的内存空间,并初始化为0
    //用于记录每个灰度值在输入向量中出现的次数。具体来说,它是一个无符号整型数组,
    //其索引表示灰度值,数组中的值表示该灰度值在输入向量中出现的次数。
    //在遍历输入向量时,将每个像素的灰度值记录在相应的 ThisGrayAppendTime 数组中。
    //这是直方图均衡化算法中需要用到的一个重要步骤,用于统计每个灰度值的出现次数 
    //为保证每一个灰度值都被包括,所以用 dealInfo->ExpectedMax 写长度
    unsigned int* ThisGrayAppendTime = (unsigned int*)calloc(dealInfo->ExpectedMax + 1, sizeof(unsigned int));
    if (ThisGrayAppendTime == NULL) {
        JZSDK_LOG_ERROR("Error: Memory allocation failed\n");
        free(*out_str);
        return JZ_ERROR_SYSTEM_MODULE_CODE_INVALID_PARAMETER;
    }

    // 遍历输入向量,统计各灰度值出现次数和总灰度和
    for (int i = 0; i < dealInfo->PixelNum; ++i) 
    {
        //获取本像素点的灰度值,且避免超过最大灰度值
        ThisPixelGray = in_str[i];
        if (ThisPixelGray > dealInfo->ExpectedMax) 
        {
            ThisPixelGray = dealInfo->ExpectedMax;
        }

        //直方图的 x坐标ThisPixelGray 的y值 即次数+1
        ThisGrayAppendTime[ThisPixelGray]++;

        //计算总灰度值
        SumAllLegalGray += ThisPixelGray;
    }

/*
    注:图像对比度:直方图分布越均匀,图像的对比度越高
        图像亮度:  直方图分布越靠右(最大值),则亮度越高
*/

/********************
 * 
 * 直方拉伸
 * 
 * 
 * *****************/
    U16_t StrDraw[dealInfo->PixelNum];
    StraightStretching(in_str, ThisGrayAppendTime, StrDraw, dealInfo);

/****************************
 * 
 *  将14位图,右移为8位图
 * 
 * 
 * *****************************/
   
    // 将均衡化后的灰度值转换为8位,并存储在输出向量中
    //直方图均衡化后的结果进行操作,将每个值转换为8位灰度值。具体操作是将in_str[i]中的值右移(InBits - 8)位
    //然后将结果转换为unsigned char类型,存储到输出向量对应的位置上。这样操作的目的是将原本可能不是8位的灰度值转换为8位灰度值,以便后续的处理和存储。
    for (int i = 0; i < dealInfo->PixelNum; i ++) 
    {
        (*out_str)[i] = (unsigned char)(StrDraw[i] >> (dealInfo->ImgDataBits - 8));
        //(*out_str)[i] = (unsigned char)(in_str[i] >> (dealInfo->ImgDataBits - 8));
    }

    *out_str_len = dealInfo->PixelNum; 
   
   
    // 释放动态分配的内存空间
    free(ThisGrayAppendTime);

    return JZ_ERROR_SYSTEM_MODULE_CODE_SUCCESS;
}


/******
 * 
 *  单点矫正
 *  
 * *****/
static T_JZsdkReturnCode JZIrcLib_SPC(U16_t *ImageData,struct IRC_param *dealInfo) 
{
    //无图像
    if (ImageData == NULL || dealInfo == NULL) 
    {  
        return JZ_ERROR_SYSTEM_MODULE_CODE_INVALID_PARAMETER;  
    }  

    // 应用校正参数 到图像数据
    for (int i = 0; i < dealInfo->PixelNum; i++) 
    {    
        double temp = ( ((dealInfo->SPC_Slope[i] * ImageData[i]) + (dealInfo->SPC_Diff[i] + ImageData[i])) / 2);
        if (temp <= dealInfo->ExpectedMax && temp >= 0)
        {
            ImageData[i] = (int)temp;
        }
        if (ImageData[i] < 0) 
        {  
            ImageData[i] = 0;  
        } 
        else if (ImageData[i] > dealInfo->ExpectedMax) 
        {  
            ImageData[i] = dealInfo->ExpectedMax;  
        }  
    }  

    //JZSDK_LOG_INFO("单点校正");

    return JZ_ERROR_SYSTEM_MODULE_CODE_SUCCESS;
}

/*******************
 * 
 *  单点校正斜率计算
 * 
 * 
 * *******************/
static T_JZsdkReturnCode JZIrcLib_SPC_Slope_Calculation(struct IRC_param *dealInfo)
{
    if (dealInfo->SPC_mode == 0)
    {
        //采用出厂模式的斜率
        if (dealInfo->TPC_mode == 0 && dealInfo->Factory_LowT_Mark_Data != NULL)
        {
            IRC_SPC_ParamCorrect(dealInfo, dealInfo->Factory_LowT_Mark_Data);
        }
        //采用手动模式的斜率
        else
        {
            IRC_SPC_ParamCorrect(dealInfo, dealInfo->LowT_NineFrame_Avg);
        }
    }
    //都不满足的情况下,采用默认的斜率
    else
    {
        IRC_SPC_ParamCorrect(dealInfo, dealInfo->SPC_Mark_Data);
    }
}

/******
 * 
 *  两点矫正
 *  
 *  
 * *****/
T_JZsdkReturnCode JZIrcLib_TPC(U16_t *ImageData,struct IRC_param *dealInfo) 
{
    //无图像
    if (ImageData == NULL || dealInfo == NULL) 
    {  
        return JZ_ERROR_SYSTEM_MODULE_CODE_INVALID_PARAMETER;  
    } 
  
    // 应用两点校正公式  
    for (int i = 0; i < dealInfo->PixelNum; i++) 
    {  
        ImageData[i] = (int)(dealInfo->TPC_Gain[i] * ImageData[i] + dealInfo->TPC_Offset[i]);  
        if (ImageData[i] < 0) 
        {  
            ImageData[i] = 0;  
        } 
        else if (ImageData[i] > dealInfo->ExpectedMax) 
        {  
            ImageData[i] = dealInfo->ExpectedMax;  
        }  
    }  

    //JZSDK_LOG_INFO("两点校正");
  
    return JZ_ERROR_SYSTEM_MODULE_CODE_SUCCESS; // 返回校正后的灰度数组  
}

/*******************
 * 
 *  两点校正斜率计算
 * 
 * 
 * *******************/
static T_JZsdkReturnCode JZIrcLib_TPC_Slope_Calculation(struct IRC_param *dealInfo)
{
    //无图像
    if (dealInfo == NULL) 
    {  
        return JZ_ERROR_SYSTEM_MODULE_CODE_INVALID_PARAMETER;  
    } 
  
    // 如果tpc的斜率和tpc的截距都为0,重新计算斜率与斜距
    int allZeroSlope_flag = 1, allZeroDiff_flag = 1;  
    for (int i = 0; i < dealInfo->PixelNum; i++) 
    {  
        if (dealInfo->TPC_Gain[i] != 0) 
        {  
            allZeroSlope_flag = 0;  
        }  

        if (dealInfo->TPC_Offset[i] != 0) 
        {  
            allZeroDiff_flag = 0;  
        }  
    }  
  
    //如果出现tpc的斜率和tpc的截距都为0,重新计算斜率与斜距
    if (allZeroSlope_flag && allZeroDiff_flag) 
    {  
        double AvgSingleFrame_LowT = 0, AvgSingleFrame_HighT = 0;  

        for (int i = 0; i < dealInfo->PixelNum; i++) 
        {  
            AvgSingleFrame_LowT += dealInfo->LowT_NineFrame_Avg[i];  
            AvgSingleFrame_HighT += dealInfo->HighT_NineFrame_Avg[i];  
        }  

        AvgSingleFrame_LowT = AvgSingleFrame_LowT / dealInfo->PixelNum;  
        AvgSingleFrame_HighT = AvgSingleFrame_HighT / dealInfo->PixelNum;  
  
        for (int i = 0; i < dealInfo->PixelNum; i++) 
        {  
            if (dealInfo->HighT_NineFrame_Avg[i] > dealInfo->LowT_NineFrame_Avg[i]) 
            {  
                dealInfo->TPC_Gain[i] = (AvgSingleFrame_HighT - AvgSingleFrame_LowT) / (dealInfo->HighT_NineFrame_Avg[i] - dealInfo->LowT_NineFrame_Avg[i]);  
            } 
            else 
            {  
                dealInfo->TPC_Gain[i] = 0;  
            }  
            dealInfo->TPC_Offset[i] = AvgSingleFrame_LowT - dealInfo->TPC_Gain[i] * dealInfo->LowT_NineFrame_Avg[i];  
        }  
    }  
}

/*******************
 * 
 *  两点校正斜率计算
 * 
 * 
 * *******************/
T_JZsdkReturnCode JZIrcLib_TPC_Slope_Calculation2(struct IRC_param *dealInfo)
{
    //判断是否存在结构体
    if (dealInfo == NULL)
    {
        return JZ_ERROR_SYSTEM_MODULE_CODE_INVALID_PARAMETER;
    }

    //计算像元平均响应
    double AvgSingleFrame_LowT = 0, AvgSingleFrame_HighT = 0;  

    for (int i = 0; i < dealInfo->PixelNum; i++) 
    {  
        AvgSingleFrame_LowT += dealInfo->LowT_NineFrame_Avg[i];  
        AvgSingleFrame_HighT += dealInfo->HighT_NineFrame_Avg[i];  
    }  

    AvgSingleFrame_HighT = AvgSingleFrame_HighT / dealInfo->PixelNum;  
    AvgSingleFrame_LowT = AvgSingleFrame_LowT / dealInfo->PixelNum;

    for (int i = 0; i < dealInfo->PixelNum; i++) 
    {  
        if (dealInfo->HighT_NineFrame_Avg[i] != dealInfo->LowT_NineFrame_Avg[i]) 
        {  
            dealInfo->TPC_Gain[i] = (AvgSingleFrame_HighT - AvgSingleFrame_LowT) / (dealInfo->HighT_NineFrame_Avg[i] - dealInfo->LowT_NineFrame_Avg[i]);
            dealInfo->TPC_Offset[i] = AvgSingleFrame_LowT - dealInfo->TPC_Gain[i] * dealInfo->LowT_NineFrame_Avg[i];    
        } 
        else 
        {  
            dealInfo->TPC_Gain[i] = 1;  
            dealInfo->TPC_Offset[i] = 0;  
        }  
    } 

    return JZ_ERROR_SYSTEM_MODULE_CODE_SUCCESS;
}


/*计算直方图均衡化并转换为8位灰度值
	    U16_t in_str 输入的数据
                int in_str_len 输入的数据长度
    灰度0为黑色,灰度255为白色
*/
static T_JZsdkReturnCode JZIrcLib_vKT(U16_t *in_str, U8_t **out_str, int *out_str_len, struct IRC_param *dealInfo)
{
    //无图像
    if (in_str == NULL || dealInfo == NULL) 
    {  
        return JZ_ERROR_SYSTEM_MODULE_CODE_INVALID_PARAMETER;  
    }  

    // 分配输出结果的内存空间
    *out_str = (unsigned char*)malloc(sizeof(unsigned char) * dealInfo->PixelNum);
    if (*out_str == NULL) {
        // 如果内存分配失败,打印错误信息并返回空指针
        JZSDK_LOG_ERROR("Error: Memory allocation failed\n");
        return JZ_ERROR_SYSTEM_MODULE_CODE_INVALID_PARAMETER;
    }

/**********************************************
 * 
 *  获取图像的直方图
 * 
 * 
 * *****************************************************/

    // 初始化变量
    unsigned int ThisPixelGray = 0;     //当前处理像素点的灰度值
    long SumAllLegalGray = 0;       //存储的总灰度值

    // 分配当前灰度值出现次数的内存空间,并初始化为0
    //用于记录每个灰度值在输入向量中出现的次数。具体来说,它是一个无符号整型数组,
    //其索引表示灰度值,数组中的值表示该灰度值在输入向量中出现的次数。
    //在遍历输入向量时,将每个像素的灰度值记录在相应的 ThisGrayAppendTime 数组中。
    //这是直方图均衡化算法中需要用到的一个重要步骤,用于统计每个灰度值的出现次数 
    //为保证每一个灰度值都被包括,所以用 dealInfo->ExpectedMax 写长度
    unsigned int* ThisGrayAppendTime = (unsigned int*)calloc(dealInfo->ExpectedMax + 1, sizeof(unsigned int));
    if (ThisGrayAppendTime == NULL) {
        JZSDK_LOG_ERROR("Error: Memory allocation failed\n");
        free(*out_str);
        return JZ_ERROR_SYSTEM_MODULE_CODE_INVALID_PARAMETER;
    }

    // 遍历输入向量,统计各灰度值出现次数和总灰度和
    for (int i = 0; i < dealInfo->PixelNum; ++i) 
    {
        //获取本像素点的灰度值,且避免超过最大灰度值
        ThisPixelGray = in_str[i];
        if (ThisPixelGray > dealInfo->ExpectedMax) 
        {
            ThisPixelGray = dealInfo->ExpectedMax;
        }

        //直方图的 x坐标ThisPixelGray 的y值 即次数+1
        ThisGrayAppendTime[ThisPixelGray]++;

        //计算总灰度值
        SumAllLegalGray += ThisPixelGray;
    }

/*
    注:图像对比度:直方图分布越均匀,图像的对比度越高
        图像亮度:  直方图分布越靠右(最大值),则亮度越高
*/

/**********************************************************************************************************/

    // 初始化边界值和阈值增益
    unsigned int LowLimit = 0; //用于指定直方图均衡化的最低灰度值范围,通常为0。
    unsigned int HighLimit = 0; //用于指定直方图均衡化的最高灰度值范围,通常为最大灰度值。
    unsigned int VMax = dealInfo->ExpectedMax; //表示输入图像中可能的最大灰度值。
    unsigned int VMin = 0; //通常为0,表示输入图像中可能的最小灰度值。
    unsigned int ThresholdLimits = dealInfo->Gain * dealInfo->ImgDataBits; //阈值参数,是一个根据增益和位深度计算得出的阈值,用于在直方图均衡化时进行限制,确保输出图像的对比度合适。
    double AvgGray = SumAllLegalGray / (dealInfo->PixelNum * 1.0); //平均灰度值,表示输入图像中所有合法灰度值的平均值,用于直方图均衡化算法中计算每个像素点的增益值。

    // 计算灰度直方图的边界值,找到合适的灰度范围用于直方图均衡化
    for (int i = 0; i < dealInfo->ExpectedMax; ++i) 
    {
        if (LowLimit < dealInfo->LeftDrop) {
            LowLimit += ThisGrayAppendTime[i];
            VMin = i;
        }
        if (HighLimit < (dealInfo->PixelNum - dealInfo->RightDrop)) 
        {
            HighLimit += ThisGrayAppendTime[i];
            VMax = i;
        }
    }

    //(ThresholdLimits / 12) 是一个阈值的限制
    //根据增益和位深度计算得出的阈值,用于在直方图均衡化时进行限制,确保输出图像的对比度合适

    //将 VMax 增加 (ThresholdLimits / 12) 的目的是为了保证直方图均衡化后的最高灰度值范围与阈值限制的要求相符,
    //从而确保输出图像的对比度得到合适的控制,并且不会超出预期的范围。
    if (VMax < dealInfo->ExpectedMax - (ThresholdLimits / 12)) 
    {
        VMax += (ThresholdLimits / 12); //最大范围右移 1/12个阈值
    }

    //ThresholdDiff 阈值差异,计算阈值和灰度均值之间的差值
    //ThresholdDiff 被计算为 VMax - VMin + (ThresholdLimits / 6.0)
    //代表最高和最低灰度值范围之间的差距,再加上阈值的六分之一
    //ThresholdDiff 用于衡量最大最小灰度范围的宽度,以确定是否需要对像素进行进一步处理。
    double ThresholdDiff = VMax - VMin + (ThresholdLimits / 6.0);

    int LowThreshold = 0;
    if (AvgGray > (ThresholdDiff / 2.0)) 
    {
        //a. 如果 AvgGray 大于 ThresholdDiff / 2.0,则将 LowThreshold 的值设为 AvgGray - (ThresholdDiff / 2.0) 
        //的无符号整数部分。让 LowThreshold 在保证图像对比度的同时,尽可能地靠近平均灰度值 AvgGray。
        LowThreshold = (unsigned int)(AvgGray - (ThresholdDiff / 2.0));
    } 
    else 
    {
        //如果 AvgGray 小于等于 ThresholdDiff / 2.0,则将 LowThreshold 的值设为零。
        //避免 LowThreshold 取负值,确保不会对图像的过滤效果产生不良影响。
        LowThreshold = 0;
    }

    //重新计算阈值差值
    ThresholdDiff = VMax - LowThreshold;

    //避免阈值差值低于阈值限制,
    if (ThresholdDiff < ThresholdLimits) 
    {
        ThresholdDiff = ThresholdLimits;
        
        //根据新的阈值差值,重新计算 低阈值    
        LowThreshold = (unsigned int)(AvgGray - ThresholdDiff / 2.0);
    
        //最大灰度值与低阈值加上阈值限制之间的差异,如果仍然满足条件
        if (VMax > LowThreshold + ThresholdLimits) 
        {
            //重新计算阈值差值
            ThresholdDiff = VMax - LowThreshold;
        }
    }

    // 对输入向量进行直方图均衡化处理,并转换为8位灰度值
    for (int i = 0; i < dealInfo->PixelNum; ++i) 
    {
        //如果该像素点的灰度值小于最小阈值,使其等于最小阈值
        //确保直方图均衡化的值不会低于设定的阈值,从而保证图像的对比度和亮度的均衡。
        if (in_str[i] < LowThreshold) 
        {
            in_str[i] = LowThreshold;
        }

        //原始直方图值in_str[i]减去低阈值LowThreshold
        //然后乘以一个缩放系数(dealInfo->ExpectedMax / (ThresholdDiff * 1.0)),将结果赋值回in_str[i]。
        //目的将直方图的灰度值映射到更广的范围,以增强图像的对比度和细节。
        in_str[i] = (unsigned int)((in_str[i] - LowThreshold) * (dealInfo->ExpectedMax / (ThresholdDiff * 1.0)));

        //避免超出灰度最大值
        if (in_str[i] > dealInfo->ExpectedMax) 
        {
            in_str[i] = dealInfo->ExpectedMax;
        }
    }

    // 将均衡化后的灰度值转换为8位,并存储在输出向量中
    //直方图均衡化后的结果进行操作,将每个值转换为8位灰度值。具体操作是将in_str[i]中的值右移(InBits - 8)位
    //然后将结果转换为unsigned char类型,存储到输出向量对应的位置上。这样操作的目的是将原本可能不是8位的灰度值转换为8位灰度值,以便后续的处理和存储。
    for (int i = 0; i < dealInfo->PixelNum; i += 4) {
        (*out_str)[i]     = (unsigned char)(in_str[i] >> (dealInfo->ImgDataBits - 8));
        (*out_str)[i + 1] = (unsigned char)(in_str[i + 1] >> (dealInfo->ImgDataBits - 8));
        (*out_str)[i + 2] = (unsigned char)(in_str[i + 2] >> (dealInfo->ImgDataBits - 8));
        (*out_str)[i + 3] = (unsigned char)(in_str[i + 3] >> (dealInfo->ImgDataBits - 8));
    }

    *out_str_len = dealInfo->PixelNum;

    // 释放动态分配的内存空间
    free(ThisGrayAppendTime);

    return JZ_ERROR_SYSTEM_MODULE_CODE_SUCCESS;
}



T_JZsdkReturnCode JZIrcLib_DataDeal(U16_t* U16_data, unsigned int U16_dataSize, 
                                U8_t **RGB_data, unsigned int *RGB_dataSize,      
                                struct IRC_param *dealInfo)
{
    T_JZsdkReturnCode ret;

    //注册一个处理用的16位数据
    U16_t *u16_CorrentData = (U16_t *)malloc(dealInfo->PixelNum * sizeof(U16_t));
    if (u16_CorrentData == NULL)
    {
        return JZ_ERROR_SYSTEM_MODULE_CODE_INVALID_PARAMETER;
    }

    //将16位数据拷贝到处理用的16位数据
    memcpy(u16_CorrentData, U16_data, dealInfo->PixelNum * sizeof(U16_t));

    //如果打开了单点纠正模式
    if (dealInfo->FrameCorrectMode == IRC_CORRCTION_SPC)
    {
        //计算单点校正的斜率
        JZIrcLib_SPC_Slope_Calculation(dealInfo);

        //通过斜率进行单点校正
        ret = JZIrcLib_SPC(u16_CorrentData, dealInfo);
        if (ret != JZ_ERROR_SYSTEM_MODULE_CODE_SUCCESS)
        {
            JZSDK_LOG_ERROR("两点校正失败");
        }
    }

    // 如果打开了两点校正
    if (dealInfo->FrameCorrectMode == IRC_CORRCTION_TPC)
    {
        //对数据进行两点校正
        ret = JZIrcLib_TPC(u16_CorrentData, dealInfo);
        if (ret != JZ_ERROR_SYSTEM_MODULE_CODE_SUCCESS)
        {
            JZSDK_LOG_ERROR("两点校正失败");
        }
    }

    U8_t *GrayImage = NULL;
    int GrayImageLen = 0;

    //直方图均衡化
    JZIrcLib_vKT(u16_CorrentData, &GrayImage, &GrayImageLen, dealInfo);

    //图像输出模式
    // 图像输出模式
    switch (dealInfo->OutputPixelColorMode)
    {
        case 0: // 默认输出模式
        {
            // 灰度图转rgb888
            IRC_GrayTo_RGB(GrayImage, RGB_data, RGB_dataSize, dealInfo);
        }
        break;

        case 1: // 伪彩输出模式
        {
            // 灰度图转伪彩rgb888
            ret = PseudoColor_Gray2Rgb(GrayImage, RGB_data, RGB_dataSize, dealInfo->PixelNum);
            if (ret != JZ_ERROR_SYSTEM_MODULE_CODE_SUCCESS)
            {
                IRC_GrayTo_RGB(GrayImage, RGB_data, RGB_dataSize, dealInfo);
            }
        }
        break;

        case 2: // 气体色彩增强输出模式
        {
            // 转为rgb
            IRC_GrayTo_RGB(GrayImage, RGB_data, RGB_dataSize, dealInfo);

            // 灰度图转气体增强rgb888
            IRC_DynamicGasesColorEnhance(*RGB_data, U16_data, dealInfo);
        }
        break;

        default:
            if (GrayImage != NULL)
            {
                free(GrayImage);
                GrayImage = NULL;
            }

            if (u16_CorrentData != NULL)
            {
                free(u16_CorrentData);
                u16_CorrentData = NULL;
            }
            return JZ_ERROR_SYSTEM_MODULE_CODE_FAILURE;
        break;
    }

    if (GrayImage != NULL)
    {
        free(GrayImage);
        GrayImage = NULL;
    }

    if (u16_CorrentData != NULL)
    {
        free(u16_CorrentData);
        u16_CorrentData = NULL;
    }

    return JZ_ERROR_SYSTEM_MODULE_CODE_SUCCESS;
}