16acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn/*M///////////////////////////////////////////////////////////////////////////////////////
26acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn//
36acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn//  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
46acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn//
56acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn//  By downloading, copying, installing or using the software you agree to this license.
66acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn//  If you do not agree to this license, do not download, install,
76acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn//  copy or use the software.
86acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn//
96acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn//
106acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn//                        Intel License Agreement
116acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn//                For Open Source Computer Vision Library
126acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn//
136acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn// Copyright (C) 2000, Intel Corporation, all rights reserved.
146acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn// Third party copyrights are property of their respective owners.
156acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn//
166acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn// Redistribution and use in source and binary forms, with or without modification,
176acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn// are permitted provided that the following conditions are met:
186acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn//
196acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn//   * Redistribution's of source code must retain the above copyright notice,
206acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn//     this list of conditions and the following disclaimer.
216acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn//
226acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn//   * Redistribution's in binary form must reproduce the above copyright notice,
236acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn//     this list of conditions and the following disclaimer in the documentation
246acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn//     and/or other materials provided with the distribution.
256acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn//
266acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn//   * The name of Intel Corporation may not be used to endorse or promote products
276acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn//     derived from this software without specific prior written permission.
286acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn//
296acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn// This software is provided by the copyright holders and contributors "as is" and
306acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn// any express or implied warranties, including, but not limited to, the implied
316acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn// warranties of merchantability and fitness for a particular purpose are disclaimed.
326acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn// In no event shall the Intel Corporation or contributors be liable for any direct,
336acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn// indirect, incidental, special, exemplary, or consequential damages
346acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn// (including, but not limited to, procurement of substitute goods or services;
356acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn// loss of use, data, or profits; or business interruption) however caused
366acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn// and on any theory of liability, whether in contract, strict liability,
376acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn// or tort (including negligence or otherwise) arising in any way out of
386acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn// the use of this software, even if advised of the possibility of such damage.
396acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn//
406acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn//M*/
416acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
426acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn#include "_cv.h"
436acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
446acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Rennstatic CvStatus CV_STDCALL
456acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius RennicvThresh_8u_C1R( const uchar* src, int src_step, uchar* dst, int dst_step,
466acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                  CvSize roi, uchar thresh, uchar maxval, int type )
476acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn{
486acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    int i, j;
496acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    uchar tab[256];
506acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
516acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    switch( type )
526acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    {
536acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    case CV_THRESH_BINARY:
546acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        for( i = 0; i <= thresh; i++ )
556acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            tab[i] = 0;
566acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        for( ; i < 256; i++ )
576acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            tab[i] = maxval;
586acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        break;
596acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    case CV_THRESH_BINARY_INV:
606acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        for( i = 0; i <= thresh; i++ )
616acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            tab[i] = maxval;
626acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        for( ; i < 256; i++ )
636acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            tab[i] = 0;
646acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        break;
656acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    case CV_THRESH_TRUNC:
666acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        for( i = 0; i <= thresh; i++ )
676acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            tab[i] = (uchar)i;
686acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        for( ; i < 256; i++ )
696acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            tab[i] = thresh;
706acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        break;
716acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    case CV_THRESH_TOZERO:
726acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        for( i = 0; i <= thresh; i++ )
736acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            tab[i] = 0;
746acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        for( ; i < 256; i++ )
756acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            tab[i] = (uchar)i;
766acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        break;
776acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    case CV_THRESH_TOZERO_INV:
786acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        for( i = 0; i <= thresh; i++ )
796acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            tab[i] = (uchar)i;
806acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        for( ; i < 256; i++ )
816acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            tab[i] = 0;
826acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        break;
836acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    default:
846acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        return CV_BADFLAG_ERR;
856acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    }
866acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
876acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    for( i = 0; i < roi.height; i++, src += src_step, dst += dst_step )
886acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    {
896acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        for( j = 0; j <= roi.width - 4; j += 4 )
906acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        {
916acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            uchar t0 = tab[src[j]];
926acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            uchar t1 = tab[src[j+1]];
936acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
946acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            dst[j] = t0;
956acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            dst[j+1] = t1;
966acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
976acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            t0 = tab[src[j+2]];
986acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            t1 = tab[src[j+3]];
996acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
1006acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            dst[j+2] = t0;
1016acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            dst[j+3] = t1;
1026acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        }
1036acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
1046acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        for( ; j < roi.width; j++ )
1056acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            dst[j] = tab[src[j]];
1066acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    }
1076acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
1086acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    return CV_NO_ERR;
1096acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn}
1106acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
1116acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
1126acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Rennstatic CvStatus CV_STDCALL
1136acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius RennicvThresh_32f_C1R( const float *src, int src_step, float *dst, int dst_step,
1146acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                   CvSize roi, float thresh, float maxval, int type )
1156acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn{
1166acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    int i, j;
1176acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    const int* isrc = (const int*)src;
1186acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    int* idst = (int*)dst;
1196acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    Cv32suf v;
1206acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    int iThresh, iMax;
1216acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
1226acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    v.f = thresh; iThresh = CV_TOGGLE_FLT(v.i);
1236acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    v.f = maxval; iMax = v.i;
1246acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
1256acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    src_step /= sizeof(src[0]);
1266acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    dst_step /= sizeof(dst[0]);
1276acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
1286acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    switch( type )
1296acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    {
1306acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    case CV_THRESH_BINARY:
1316acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        for( i = 0; i < roi.height; i++, isrc += src_step, idst += dst_step )
1326acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        {
1336acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            for( j = 0; j < roi.width; j++ )
1346acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            {
1356acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                int temp = isrc[j];
1366acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                idst[j] = ((CV_TOGGLE_FLT(temp) <= iThresh) - 1) & iMax;
1376acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            }
1386acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        }
1396acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        break;
1406acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
1416acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    case CV_THRESH_BINARY_INV:
1426acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        for( i = 0; i < roi.height; i++, isrc += src_step, idst += dst_step )
1436acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        {
1446acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            for( j = 0; j < roi.width; j++ )
1456acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            {
1466acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                int temp = isrc[j];
1476acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                idst[j] = ((CV_TOGGLE_FLT(temp) > iThresh) - 1) & iMax;
1486acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            }
1496acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        }
1506acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        break;
1516acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
1526acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    case CV_THRESH_TRUNC:
1536acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        for( i = 0; i < roi.height; i++, src += src_step, dst += dst_step )
1546acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        {
1556acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            for( j = 0; j < roi.width; j++ )
1566acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            {
1576acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                float temp = src[j];
1586acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
1596acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                if( temp > thresh )
1606acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                    temp = thresh;
1616acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                dst[j] = temp;
1626acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            }
1636acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        }
1646acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        break;
1656acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
1666acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    case CV_THRESH_TOZERO:
1676acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        for( i = 0; i < roi.height; i++, isrc += src_step, idst += dst_step )
1686acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        {
1696acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            for( j = 0; j < roi.width; j++ )
1706acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            {
1716acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                int temp = isrc[j];
1726acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                idst[j] = ((CV_TOGGLE_FLT( temp ) <= iThresh) - 1) & temp;
1736acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            }
1746acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        }
1756acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        break;
1766acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
1776acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    case CV_THRESH_TOZERO_INV:
1786acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        for( i = 0; i < roi.height; i++, isrc += src_step, idst += dst_step )
1796acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        {
1806acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            for( j = 0; j < roi.width; j++ )
1816acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            {
1826acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                int temp = isrc[j];
1836acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                idst[j] = ((CV_TOGGLE_FLT( temp ) > iThresh) - 1) & temp;
1846acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            }
1856acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        }
1866acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        break;
1876acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
1886acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    default:
1896acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        return CV_BADFLAG_ERR;
1906acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    }
1916acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
1926acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    return CV_OK;
1936acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn}
1946acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
1956acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
1966acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Rennstatic double
1976acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius RennicvGetThreshVal_Otsu( const CvHistogram* hist )
1986acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn{
1996acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    double max_val = 0;
2006acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
2016acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    CV_FUNCNAME( "icvGetThreshVal_Otsu" );
2026acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
2036acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    __BEGIN__;
2046acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
2056acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    int i, count;
2066acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    const float* h;
2076acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    double sum = 0, mu = 0;
2086acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    bool uniform = false;
2096acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    double low = 0, high = 0, delta = 0;
2106acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    float* nu_thresh = 0;
2116acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    double mu1 = 0, q1 = 0;
2126acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    double max_sigma = 0;
2136acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
2146acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    if( !CV_IS_HIST(hist) || CV_IS_SPARSE_HIST(hist) || hist->mat.dims != 1 )
2156acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        CV_ERROR( CV_StsBadArg,
2166acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        "The histogram in Otsu method must be a valid dense 1D histogram" );
2176acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
2186acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    count = hist->mat.dim[0].size;
2196acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    h = (float*)cvPtr1D( hist->bins, 0 );
2206acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
2216acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    if( !CV_HIST_HAS_RANGES(hist) || CV_IS_UNIFORM_HIST(hist) )
2226acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    {
2236acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        if( CV_HIST_HAS_RANGES(hist) )
2246acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        {
2256acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            low = hist->thresh[0][0];
2266acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            high = hist->thresh[0][1];
2276acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        }
2286acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        else
2296acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        {
2306acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            low = 0;
2316acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            high = count;
2326acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        }
2336acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
2346acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        delta = (high-low)/count;
2356acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        low += delta*0.5;
2366acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        uniform = true;
2376acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    }
2386acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    else
2396acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        nu_thresh = hist->thresh2[0];
2406acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
2416acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    for( i = 0; i < count; i++ )
2426acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    {
2436acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        sum += h[i];
2446acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        if( uniform )
2456acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            mu += (i*delta + low)*h[i];
2466acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        else
2476acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            mu += (nu_thresh[i*2] + nu_thresh[i*2+1])*0.5*h[i];
2486acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    }
2496acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
2506acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    sum = fabs(sum) > FLT_EPSILON ? 1./sum : 0;
2516acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    mu *= sum;
2526acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
2536acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    mu1 = 0;
2546acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    q1 = 0;
2556acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
2566acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    for( i = 0; i < count; i++ )
2576acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    {
2586acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        double p_i, q2, mu2, val_i, sigma;
2596acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
2606acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        p_i = h[i]*sum;
2616acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        mu1 *= q1;
2626acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        q1 += p_i;
2636acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        q2 = 1. - q1;
2646acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
2656acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        if( MIN(q1,q2) < FLT_EPSILON || MAX(q1,q2) > 1. - FLT_EPSILON )
2666acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            continue;
2676acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
2686acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        if( uniform )
2696acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            val_i = i*delta + low;
2706acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        else
2716acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            val_i = (nu_thresh[i*2] + nu_thresh[i*2+1])*0.5;
2726acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
2736acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        mu1 = (mu1 + val_i*p_i)/q1;
2746acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        mu2 = (mu - q1*mu1)/q2;
2756acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        sigma = q1*q2*(mu1 - mu2)*(mu1 - mu2);
2766acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        if( sigma > max_sigma )
2776acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        {
2786acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            max_sigma = sigma;
2796acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            max_val = val_i;
2806acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        }
2816acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    }
2826acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
2836acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    __END__;
2846acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
2856acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    return max_val;
2866acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn}
2876acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
2886acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
2896acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius RennicvAndC_8u_C1R_t icvAndC_8u_C1R_p = 0;
2906acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius RennicvCompareC_8u_C1R_cv_t icvCompareC_8u_C1R_cv_p = 0;
2916acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius RennicvThreshold_GTVal_8u_C1R_t icvThreshold_GTVal_8u_C1R_p = 0;
2926acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius RennicvThreshold_GTVal_32f_C1R_t icvThreshold_GTVal_32f_C1R_p = 0;
2936acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius RennicvThreshold_LTVal_8u_C1R_t icvThreshold_LTVal_8u_C1R_p = 0;
2946acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius RennicvThreshold_LTVal_32f_C1R_t icvThreshold_LTVal_32f_C1R_p = 0;
2956acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
2966acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius RennCV_IMPL double
2976acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius RenncvThreshold( const void* srcarr, void* dstarr, double thresh, double maxval, int type )
2986acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn{
2996acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    CvHistogram* hist = 0;
3006acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
3016acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    CV_FUNCNAME( "cvThreshold" );
3026acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
3036acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    __BEGIN__;
3046acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
3056acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    CvSize roi;
3066acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    int src_step, dst_step;
3076acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    CvMat src_stub, *src = (CvMat*)srcarr;
3086acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    CvMat dst_stub, *dst = (CvMat*)dstarr;
3096acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    CvMat src0, dst0;
3106acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    int coi1 = 0, coi2 = 0;
3116acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    int ithresh, imaxval, cn;
3126acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    bool use_otsu;
3136acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
3146acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    CV_CALL( src = cvGetMat( src, &src_stub, &coi1 ));
3156acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    CV_CALL( dst = cvGetMat( dst, &dst_stub, &coi2 ));
3166acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
3176acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    if( coi1 + coi2 )
3186acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        CV_ERROR( CV_BadCOI, "COI is not supported by the function" );
3196acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
3206acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    if( !CV_ARE_CNS_EQ( src, dst ) )
3216acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        CV_ERROR( CV_StsUnmatchedFormats, "Both arrays must have equal number of channels" );
3226acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
3236acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    cn = CV_MAT_CN(src->type);
3246acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    if( cn > 1 )
3256acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    {
3266acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        src = cvReshape( src, &src0, 1 );
3276acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        dst = cvReshape( dst, &dst0, 1 );
3286acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    }
3296acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
3306acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    use_otsu = (type & ~CV_THRESH_MASK) == CV_THRESH_OTSU;
3316acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    type &= CV_THRESH_MASK;
3326acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
3336acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    if( use_otsu )
3346acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    {
3356acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        float _ranges[] = { 0, 256 };
3366acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        float* ranges = _ranges;
3376acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        int hist_size = 256;
3386acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        void* srcarr0 = src;
3396acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
3406acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        if( CV_MAT_TYPE(src->type) != CV_8UC1 )
3416acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            CV_ERROR( CV_StsNotImplemented, "Otsu method can only be used with 8uC1 images" );
3426acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
3436acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        CV_CALL( hist = cvCreateHist( 1, &hist_size, CV_HIST_ARRAY, &ranges ));
3446acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        cvCalcArrHist( &srcarr0, hist );
3456acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        thresh = cvFloor(icvGetThreshVal_Otsu( hist ));
3466acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    }
3476acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
3486acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    if( !CV_ARE_DEPTHS_EQ( src, dst ) )
3496acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    {
3506acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        if( CV_MAT_TYPE(dst->type) != CV_8UC1 )
3516acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            CV_ERROR( CV_StsUnsupportedFormat, "In case of different types destination should be 8uC1" );
3526acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
3536acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        if( type != CV_THRESH_BINARY && type != CV_THRESH_BINARY_INV )
3546acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            CV_ERROR( CV_StsBadArg,
3556acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            "In case of different types only CV_THRESH_BINARY "
3566acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            "and CV_THRESH_BINARY_INV thresholding types are supported" );
3576acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
3586acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        if( maxval < 0 )
3596acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        {
3606acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            CV_CALL( cvSetZero( dst ));
3616acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        }
3626acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        else
3636acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        {
3646acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            CV_CALL( cvCmpS( src, thresh, dst, type == CV_THRESH_BINARY ? CV_CMP_GT : CV_CMP_LE ));
3656acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            if( maxval < 255 )
3666acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                CV_CALL( cvAndS( dst, cvScalarAll( maxval ), dst ));
3676acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        }
3686acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        EXIT;
3696acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    }
3706acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
3716acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    if( !CV_ARE_SIZES_EQ( src, dst ) )
3726acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        CV_ERROR( CV_StsUnmatchedSizes, "" );
3736acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
3746acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    roi = cvGetMatSize( src );
3756acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    if( CV_IS_MAT_CONT( src->type & dst->type ))
3766acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    {
3776acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        roi.width *= roi.height;
3786acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        roi.height = 1;
3796acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        src_step = dst_step = CV_STUB_STEP;
3806acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    }
3816acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    else
3826acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    {
3836acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        src_step = src->step;
3846acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        dst_step = dst->step;
3856acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    }
3866acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
3876acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    switch( CV_MAT_DEPTH(src->type) )
3886acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    {
3896acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    case CV_8U:
3906acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
3916acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        ithresh = cvFloor(thresh);
3926acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        imaxval = cvRound(maxval);
3936acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        if( type == CV_THRESH_TRUNC )
3946acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            imaxval = ithresh;
3956acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        imaxval = CV_CAST_8U(imaxval);
3966acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
3976acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        if( ithresh < 0 || ithresh >= 255 )
3986acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        {
3996acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            if( type == CV_THRESH_BINARY || type == CV_THRESH_BINARY_INV ||
4006acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                ((type == CV_THRESH_TRUNC || type == CV_THRESH_TOZERO_INV) && ithresh < 0) ||
4016acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                (type == CV_THRESH_TOZERO && ithresh >= 255) )
4026acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            {
4036acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                int v = type == CV_THRESH_BINARY ? (ithresh >= 255 ? 0 : imaxval) :
4046acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                        type == CV_THRESH_BINARY_INV ? (ithresh >= 255 ? imaxval : 0) :
4056acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                        type == CV_THRESH_TRUNC ? imaxval : 0;
4066acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
4076acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                cvSet( dst, cvScalarAll(v) );
4086acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                EXIT;
4096acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            }
4106acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            else
4116acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            {
4126acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                cvCopy( src, dst );
4136acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                EXIT;
4146acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            }
4156acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        }
4166acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
4176acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        if( type == CV_THRESH_BINARY || type == CV_THRESH_BINARY_INV )
4186acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        {
4196acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            if( icvCompareC_8u_C1R_cv_p && icvAndC_8u_C1R_p )
4206acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            {
4216acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                IPPI_CALL( icvCompareC_8u_C1R_cv_p( src->data.ptr, src_step,
4226acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                    (uchar)ithresh, dst->data.ptr, dst_step, roi,
4236acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                    type == CV_THRESH_BINARY ? cvCmpGreater : cvCmpLessEq ));
4246acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
4256acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                if( imaxval < 255 )
4266acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                    IPPI_CALL( icvAndC_8u_C1R_p( dst->data.ptr, dst_step,
4276acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                    (uchar)imaxval, dst->data.ptr, dst_step, roi ));
4286acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                EXIT;
4296acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            }
4306acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        }
4316acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        else if( type == CV_THRESH_TRUNC || type == CV_THRESH_TOZERO_INV )
4326acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        {
4336acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            if( icvThreshold_GTVal_8u_C1R_p )
4346acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            {
4356acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                IPPI_CALL( icvThreshold_GTVal_8u_C1R_p( src->data.ptr, src_step,
4366acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                    dst->data.ptr, dst_step, roi, (uchar)ithresh,
4376acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                    (uchar)(type == CV_THRESH_TRUNC ? ithresh : 0) ));
4386acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                EXIT;
4396acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            }
4406acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        }
4416acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        else
4426acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        {
4436acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            assert( type == CV_THRESH_TOZERO );
4446acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            if( icvThreshold_LTVal_8u_C1R_p )
4456acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            {
4466acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                ithresh = cvFloor(thresh+1.);
4476acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                ithresh = CV_CAST_8U(ithresh);
4486acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                IPPI_CALL( icvThreshold_LTVal_8u_C1R_p( src->data.ptr, src_step,
4496acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                    dst->data.ptr, dst_step, roi, (uchar)ithresh, 0 ));
4506acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                EXIT;
4516acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            }
4526acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        }
4536acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
4546acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        icvThresh_8u_C1R( src->data.ptr, src_step,
4556acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                          dst->data.ptr, dst_step, roi,
4566acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                          (uchar)ithresh, (uchar)imaxval, type );
4576acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        break;
4586acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    case CV_32F:
4596acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
4606acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        if( type == CV_THRESH_TRUNC || type == CV_THRESH_TOZERO_INV )
4616acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        {
4626acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            if( icvThreshold_GTVal_32f_C1R_p )
4636acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            {
4646acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                IPPI_CALL( icvThreshold_GTVal_32f_C1R_p( src->data.fl, src_step,
4656acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                    dst->data.fl, dst_step, roi, (float)thresh,
4666acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                    type == CV_THRESH_TRUNC ? (float)thresh : 0 ));
4676acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                EXIT;
4686acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            }
4696acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        }
4706acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        else if( type == CV_THRESH_TOZERO )
4716acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        {
4726acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            if( icvThreshold_LTVal_32f_C1R_p )
4736acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            {
4746acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                IPPI_CALL( icvThreshold_LTVal_32f_C1R_p( src->data.fl, src_step,
4756acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                    dst->data.fl, dst_step, roi, (float)(thresh*(1 + FLT_EPSILON)), 0 ));
4766acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                EXIT;
4776acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            }
4786acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        }
4796acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
4806acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        icvThresh_32f_C1R( src->data.fl, src_step, dst->data.fl, dst_step, roi,
4816acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                           (float)thresh, (float)maxval, type );
4826acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        break;
4836acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    default:
4846acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        CV_ERROR( CV_BadDepth, cvUnsupportedFormat );
4856acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    }
4866acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
4876acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    __END__;
4886acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
4896acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    if( hist )
4906acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        cvReleaseHist( &hist );
4916acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
4926acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    return thresh;
4936acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn}
4946acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
4956acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn/* End of file. */
496