<commit_before>#include "ndImage_Segmenter_structs.h"
#include "Python.h"
#include "numpy/arrayobject.h"

static PyObject *Segmenter_EdgePreFilter(PyObject *self, PyObject *args)
{

    int lowThreshold;
    int highThreshold;
    int num;
    int nd;
    int type;
    int aperature;
    int half_taps;
    npy_intp *dims;
    unsigned short *fP1;
    double *fP2;
    double *pKernel;
    PyObject *iArray = NULL;
    PyObject *eArray = NULL;
    PyObject *kernel = NULL;

    //
    // pass in 2D LPF coefficients
    if(!PyArg_ParseTuple(args, "iiiOOO", &lowThreshold, &highThreshold, 
			 &half_taps, &kernel, &iArray, &eArray))
	    goto exit;

    fP1  = (unsigned short *)PyArray_DATA(iArray);
    nd   = PyArray_NDIM(iArray);
    dims = PyArray_DIMS(iArray);
    type = PyArray_TYPE(iArray);
    num  = PyArray_SIZE(iArray);
    fP2     = (double *)PyArray_DATA(eArray);
    pKernel = (double *)PyArray_DATA(kernel);
    aperature = PyArray_SIZE(kernel);

    if(!PyArray_ISCONTIGUOUS(iArray) || !PyArray_ISCONTIGUOUS(eArray))
	    goto exit;

    
    if(!NI_EdgePreFilter(num, (int)dims[0], (int)dims[1], lowThreshold, 
		         highThreshold, aperature, half_taps, fP1, fP2, pKernel))
		      
	    goto exit;

exit:

    return PyErr_Occurred() ? NULL : (PyObject*)Py_BuildValue("");

}


static PyObject *Segmenter_SobelImage(PyObject *self, PyObject *args)
{

    int num;
    int nd;
    int type;
    npy_intp *dims;
    double *fP1;
    double *fP2;
    double pAve;
    int minValue;
    int maxValue;
    PyObject *iArray = NULL;
    PyObject *eArray = NULL;

    if(!PyArg_ParseTuple(args, "OO", &iArray, &eArray))
	    goto exit;

    fP1  = (double *)PyArray_DATA(iArray);
    nd   = PyArray_NDIM(iArray);
    dims = PyArray_DIMS(iArray);
    type = PyArray_TYPE(iArray);
    num  = PyArray_SIZE(iArray);
    fP2  = (double *)PyArray_DATA(eArray);

    if(!PyArray_ISCONTIGUOUS(iArray) || !PyArray_ISCONTIGUOUS(eArray))
	    goto exit;

    
    if(!NI_SobelImage(num, (int)dims[0], (int)dims[1], fP1, fP2, &pAve, &minValue, &maxValue))
		      
	    goto exit;

exit:

    return PyErr_Occurred() ? NULL : (PyObject*)Py_BuildValue("dii", pAve, minValue, maxValue);

}


static PyObject *Segmenter_SobelEdges(PyObject *self, PyObject *args)
{

    int num;
    int nd;
    int type;
    npy_intp *dims;
    double *fP1;
    unsigned short *fP2;
    double pAve;
    int minValue;
    int maxValue;
    int mode;
    double sobelLow;
    PyObject *iArray = NULL;
    PyObject *eArray = NULL;

    if(!PyArg_ParseTuple(args, "OOdiiid", &iArray, &eArray, &pAve, &minValue, &maxValue, &mode,
			                  &sobelLow))
	    goto exit;

    fP1  = (double *)PyArray_DATA(iArray);
    nd   = PyArray_NDIM(iArray);
    dims = PyArray_DIMS(iArray);
    type = PyArray_TYPE(iArray);
    num  = PyArray_SIZE(iArray);
    fP2  = (unsigned short *)PyArray_DATA(eArray);

    if(!PyArray_ISCONTIGUOUS(iArray) || !PyArray_ISCONTIGUOUS(eArray))
	    goto exit;

    
    if(!NI_SobelEdge(num, (int)dims[0], (int)dims[1], fP1, fP2, mode, pAve, minValue, maxValue, sobelLow))
	    goto exit;

exit:

    return PyErr_Occurred() ? NULL : (PyObject*)Py_BuildValue("");

}



static PyObject *Segmenter_GetBlobs(PyObject *self, PyObject *args)
{

    int num;
    int nd;
    int type;
    npy_intp *dims;
    unsigned short *fP1;
    unsigned short *fP2;
    int groups;
    PyObject *iArray = NULL;
    PyObject *eArray = NULL;

    if(!PyArg_ParseTuple(args, "OO", &iArray, &eArray))
	    goto exit;

    fP1  = (unsigned short *)PyArray_DATA(iArray);
    nd   = PyArray_NDIM(iArray);
    dims = PyArray_DIMS(iArray);
    type = PyArray_TYPE(iArray);
    num  = PyArray_SIZE(iArray);
    fP2  = (unsigned short *)PyArray_DATA(eArray);

    if(!PyArray_ISCONTIGUOUS(iArray) || !PyArray_ISCONTIGUOUS(eArray))
	    goto exit;

    
    if(!NI_GetBlobs(num, (int)dims[0], (int)dims[1], fP1, fP2, &groups))
	    goto exit;

exit:

    return PyErr_Occurred() ? NULL : (PyObject*)Py_BuildValue("i", groups);

}

static PyObject *Segmenter_GetBlobRegions(PyObject *self, PyObject *args)
{


    int num;
    int nd;
    int type;
    npy_intp *dims;
    npy_intp *objNumber;
    unsigned short *fP1;
    PyObject  *iArray = NULL;
    PyObject  *nArray = NULL;
    objStruct *myData;

    if(!PyArg_ParseTuple(args, "OO", &iArray, &nArray))
	    goto exit;

    if(!PyArray_ISCONTIGUOUS(iArray) || !PyArray_ISCONTIGUOUS(nArray))
	    goto exit;

    	//
	//   PyArray_ContiguousFromObject or PyArray_ContiguousFromAny to be explored 
	//   for non-contiguous
	//

	
    // pointer to the edge-labeled image
    nd   = PyArray_NDIM(iArray);
    dims = PyArray_DIMS(iArray);
    type = PyArray_TYPE(iArray);
    num  = PyArray_SIZE(iArray);
    fP1  = (unsigned short *)PyArray_DATA(iArray);

    // the object descriptor array that was allocated from numpy
    objNumber = PyArray_DIMS(nArray); // this is the number of labels in the edge image
    myData = (objStruct*)PyArray_DATA(nArray);

    if(!NI_GetBlobRegions((int)dims[0], (int)dims[1], (int)objNumber[0], fP1, myData))
	    goto exit;

exit:

    return PyErr_Occurred() ? NULL : (PyObject*)Py_BuildValue("");

}


static PyObject *Segmenter_ThinFilter(PyObject *self, PyObject *args)<commit_msg>Added support for 3D blob extraction and improve the current 2D blob.<commit_after>#include "ndImage_Segmenter_structs.h"
#include "Python.h"
#include "numpy/arrayobject.h"

static PyObject *Segmenter_EdgePreFilter(PyObject *self, PyObject *args)
{

    int lowThreshold;
    int highThreshold;
    int num;
    int nd;
    int type;
    int aperature;
    int half_taps;
    npy_intp *dims;
    unsigned short *fP1;
    double *fP2;
    double *pKernel;
    PyObject *iArray = NULL;
    PyObject *eArray = NULL;
    PyObject *kernel = NULL;

    //
    // pass in 2D LPF coefficients
    if(!PyArg_ParseTuple(args, "iiiOOO", &lowThreshold, &highThreshold, 
			 &half_taps, &kernel, &iArray, &eArray))
	    goto exit;

    fP1  = (unsigned short *)PyArray_DATA(iArray);
    nd   = PyArray_NDIM(iArray);
    dims = PyArray_DIMS(iArray);
    type = PyArray_TYPE(iArray);
    num  = PyArray_SIZE(iArray);
    fP2     = (double *)PyArray_DATA(eArray);
    pKernel = (double *)PyArray_DATA(kernel);
    aperature = PyArray_SIZE(kernel);

    if(!PyArray_ISCONTIGUOUS(iArray) || !PyArray_ISCONTIGUOUS(eArray))
	    goto exit;

    
    if(!NI_EdgePreFilter(num, (int)dims[0], (int)dims[1], lowThreshold, 
		         highThreshold, aperature, half_taps, fP1, fP2, pKernel))
		      
	    goto exit;

exit:

    return PyErr_Occurred() ? NULL : (PyObject*)Py_BuildValue("");

}


static PyObject *Segmenter_SobelImage(PyObject *self, PyObject *args)
{

    int num;
    int nd;
    int type;
    npy_intp *dims;
    double *fP1;
    double *fP2;
    double pAve;
    int minValue;
    int maxValue;
    PyObject *iArray = NULL;
    PyObject *eArray = NULL;

    if(!PyArg_ParseTuple(args, "OO", &iArray, &eArray))
	    goto exit;

    fP1  = (double *)PyArray_DATA(iArray);
    nd   = PyArray_NDIM(iArray);
    dims = PyArray_DIMS(iArray);
    type = PyArray_TYPE(iArray);
    num  = PyArray_SIZE(iArray);
    fP2  = (double *)PyArray_DATA(eArray);

    if(!PyArray_ISCONTIGUOUS(iArray) || !PyArray_ISCONTIGUOUS(eArray))
	    goto exit;

    
    if(!NI_SobelImage(num, (int)dims[0], (int)dims[1], fP1, fP2, &pAve, &minValue, &maxValue))
		      
	    goto exit;

exit:

    return PyErr_Occurred() ? NULL : (PyObject*)Py_BuildValue("dii", pAve, minValue, maxValue);

}


static PyObject *Segmenter_SobelEdges(PyObject *self, PyObject *args)
{

    int num;
    int nd;
    int type;
    npy_intp *dims;
    double *fP1;
    unsigned short *fP2;
    double pAve;
    int minValue;
    int maxValue;
    int mode;
    double sobelLow;
    PyObject *iArray = NULL;
    PyObject *eArray = NULL;

    if(!PyArg_ParseTuple(args, "OOdiiid", &iArray, &eArray, &pAve, &minValue, &maxValue, &mode,
			                  &sobelLow))
	    goto exit;

    fP1  = (double *)PyArray_DATA(iArray);
    nd   = PyArray_NDIM(iArray);
    dims = PyArray_DIMS(iArray);
    type = PyArray_TYPE(iArray);
    num  = PyArray_SIZE(iArray);
    fP2  = (unsigned short *)PyArray_DATA(eArray);

    if(!PyArray_ISCONTIGUOUS(iArray) || !PyArray_ISCONTIGUOUS(eArray))
	    goto exit;

    
    if(!NI_SobelEdge(num, (int)dims[0], (int)dims[1], fP1, fP2, mode, pAve, minValue, maxValue, sobelLow))
	    goto exit;

exit:

    return PyErr_Occurred() ? NULL : (PyObject*)Py_BuildValue("");

}



static PyObject *Segmenter_GetBlobs(PyObject *self, PyObject *args)
{

    int num;
    int nd;
    int type;
    int mask;
    npy_intp *dims;
    unsigned short *fP1;
    unsigned short *fP2;
    int groups;
    PyObject *iArray = NULL;
    PyObject *eArray = NULL;

    if(!PyArg_ParseTuple(args, "OOi", &iArray, &eArray, &mask))
	    goto exit;

    fP1  = (unsigned short *)PyArray_DATA(iArray);
    nd   = PyArray_NDIM(iArray);
    dims = PyArray_DIMS(iArray);
    type = PyArray_TYPE(iArray);
    num  = PyArray_SIZE(iArray);
    fP2  = (unsigned short *)PyArray_DATA(eArray);

    if(!PyArray_ISCONTIGUOUS(iArray) || !PyArray_ISCONTIGUOUS(eArray))
	    goto exit;

    
    if(nd == 2){ 
        if(!NI_GetBlobs2D(num, (int)dims[0], (int)dims[1], fP1, fP2, &groups, mask))
	    goto exit;
    }
    else if(nd == 3){ 
        if(!NI_GetBlobs3D(num, (int)dims[0], (int)dims[1], (int)dims[2], fP1, fP2, 
			  &groups, mask))
	    goto exit;
    }

exit:

    return PyErr_Occurred() ? NULL : (PyObject*)Py_BuildValue("i", groups);

}

static PyObject *Segmenter_GetBlobRegions(PyObject *self, PyObject *args)
{


    int num;
    int nd;
    int type;
    npy_intp *dims;
    npy_intp *objNumber;
    unsigned short *fP1;
    PyObject  *iArray = NULL;
    PyObject  *nArray = NULL;
    objStruct *myData;

    if(!PyArg_ParseTuple(args, "OO", &iArray, &nArray))
	    goto exit;

    if(!PyArray_ISCONTIGUOUS(iArray) || !PyArray_ISCONTIGUOUS(nArray))
	    goto exit;

    	//
	//   PyArray_ContiguousFromObject or PyArray_ContiguousFromAny to be explored 
	//   for non-contiguous
	//

	
    // pointer to the edge-labeled image
    nd   = PyArray_NDIM(iArray);
    dims = PyArray_DIMS(iArray);
    type = PyArray_TYPE(iArray);
    num  = PyArray_SIZE(iArray);
    fP1  = (unsigned short *)PyArray_DATA(iArray);

    // the object descriptor array that was allocated from numpy
    objNumber = PyArray_DIMS(nArray); // this is the number of labels in the edge image
    myData = (objStruct*)PyArray_DATA(nArray);

    /* need to pass in 2D/3D flag and mask. NI_GetBlobRegions will call
     * 2D or 3D blob_extraction  */

    if(!NI_GetBlobRegions((int)dims[0], (int)dims[1], (int)objNumber[0], fP1, myData))
	    goto exit;

exit:

    return PyErr_Occurred() ? NULL : (PyObject*)Py_BuildValue("");

}


static PyObject *Segmenter_ThinFilter(PyObject *self, PyObject *args)
{'fraction_non_alphanumeric': 0.125906212719097, 'fraction_numerical': 0.008534459025419841, 'mean_word_length': 3.2437694704049846, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7545955', 'n_tokens_mistral': 4236, 'n_tokens_neox': 4147, 'n_words': 894}