Python cv2.boxFilter() Examples

The following are 3 code examples of cv2.boxFilter(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module cv2 , or try the search function .
Example #1
Source File: niblack_thresholding.py    From lpr with Apache License 2.0 5 votes vote down vote up
def niBlackThreshold(  src,  blockSize,  k,  binarizationMethod= 0 ):
    mean = cv2.boxFilter(src,cv2.CV_32F,(blockSize, blockSize),borderType=cv2.BORDER_REPLICATE)
    sqmean = cv2.sqrBoxFilter(src, cv2.CV_32F, (blockSize, blockSize), borderType = cv2.BORDER_REPLICATE)
    variance = sqmean - (mean*mean)
    stddev  = np.sqrt(variance)
    thresh = mean + stddev * float(-k)
    thresh = thresh.astype(src.dtype)
    k = (src>thresh)*255
    k = k.astype(np.uint8)
    return k


# cv2.imshow() 
Example #2
Source File: preprocessing.py    From stytra with GNU General Public License v3.0 5 votes vote down vote up
def _process(
        self,
        im,
        image_scale: Param(0.5, (0.05, 1.0)),
        filter_size: Param(2, (0, 15)),
        color_invert: Param(True),
        clip: Param(140, (0, 255)),
        **extraparams
    ):
        """ Optionally resizes, smooths and inverts the image

        :param im:
        :param state:
        :param filter_size:
        :param image_scale:
        :param color_invert:
        :return:
        """
        if image_scale != 1:
            im = cv2.resize(
                im, None, fx=image_scale, fy=image_scale, interpolation=cv2.INTER_AREA
            )
        if filter_size > 0:
            im = cv2.boxFilter(im, -1, (filter_size, filter_size))
        if color_invert:
            im = 255 - im
        if clip > 0:
            im = np.maximum(im, clip) - clip

        if self.set_diagnostic == "filtered":
            self.diagnostic_image = im

        return NodeOutput([], im) 
Example #3
Source File: KernalFiltering.py    From Finger-Detection-and-Tracking with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def main():
    image = cv2.imread("../data/7.1.01.tiff", 1)

    '''
    # Kernal or Convolution matrix for Identity Filter

    kernal = np.array(([0, 0, 0],
                       [0, 1, 0],
                       [0, 0, 0]), np.float32)

    # Kernal or Convolution matrix for Edge Detection

    kernal = np.array(([-1, -1, -1],
                       [-1, 8, -1],
                       [-1, -1, -1]), np.float32)

    '''

    # Kernal or Convolution matrix for Box BLue Filter

    kernal = np.ones((5, 5), np.uint8) / 25
    output = cv2.filter2D(image, -1, kernal)

    # Low pass filters implementation
    box_blur = cv2.boxFilter(image, -1, (31, 31))
    simple_blur = cv2.blur(image, (21, 21))
    gaussian_blur = cv2.GaussianBlur(image, (51, 51), 0)

    cv2.imshow("Orignal Image", image)
    cv2.imshow("Filtered Image", output)

    cv2.imshow("Box Blur", box_blur)
    cv2.imshow("Simple Blur", simple_blur)
    cv2.imshow("Gaussian Blur", gaussian_blur)

    cv2.waitKey(0)
    cv2.destroyAllWindows()