Python Image Processing (4): filter, python Image Processing

Source: Internet
Author: User

Python Image Processing (4): filter, python Image Processing

Happy shrimp

Http://blog.csdn.net/lights_joy/ (QQ group: Visual EmbedLinux Tools 375515651)

Reprinted, but keep the author information


Filters are widely used in image processing. OpenCV also uses the filter mask (CORE) function filter2D to perform convolution between the image and the core to obtain the target image. Convolution is an operation between each image block and an operator (CORE), and a core is a fixed-size numerical array.


In fact, many algorithms in OpenCV are implemented by convolution, including some edge detection algorithms. This article does not intend to list the principles of these algorithms. It just examines the implementation of OpenCV, and then tries to use Python to call these algorithms to see their results.


The test image used is still:


Because our ultimate goal is to identify weeds and cotton plants in this image!


1. FilterEngine


The filtering operation in OpenCV is completed by the FilterEngine class. The function of this class is clearly described in the source code comment:


The Main Class for Image Filtering. The class can be used to apply an arbitrary filtering operation to an image. It contains all the necessary intermediate buffers, it computes extrapolated values of the "virtual" pixels outside of the image etc. Pointers to the initialized cv::FilterEngine instances are returned by various OpenCV functions, such as cv::createSeparableLinearFilter(), cv::createLinearFilter(), cv::createGaussianFilter(), cv::createDerivFilter(), cv::createBoxFilter() and cv::createMorphologyFilter(). Using the class you can process large images by parts and build complex pipelines that include filtering as some of the stages. If all you need is to apply some pre-defined filtering operation, you may use cv::filter2D(), cv::erode(), cv::dilate() etc. functions that create FilterEngine internally.

The following is a typical use of FilterEngine:

void cv::sepFilter2D( InputArray _src, OutputArray _dst, int ddepth,                      InputArray _kernelX, InputArray _kernelY, Point anchor,                      double delta, int borderType ){    Mat src = _src.getMat(), kernelX = _kernelX.getMat(), kernelY = _kernelY.getMat();    if( ddepth < 0 )        ddepth = src.depth();    _dst.create( src.size(), CV_MAKETYPE(ddepth, src.channels()) );    Mat dst = _dst.getMat();    Ptr<FilterEngine> f = createSeparableLinearFilter(src.type(),        dst.type(), kernelX, kernelY, anchor, delta, borderType & ~BORDER_ISOLATED );    f->apply(src, dst, Rect(0,0,-1,-1), Point(), (borderType & BORDER_ISOLATED) != 0 );}

Of course, as mentioned in the comment, we almost do not need to use this class directly, but understanding its usage is very good for us to understand and debug OpenCV.


2. filter2D


Next, let's try to show the effect to the matrix of a filter.

# -*- coding: utf-8 -*- import cv2import numpy as np# filter2Dsrc = cv2.imread('f:\\tmp\\cotton.jpg')cv2.imshow('src', src)kernel = np.array([ [-1, -1, -1],                    [-1,  8, -1],                    [-1, -1, -1] ])dst = cv2.filter2D(src, -1, kernel)cv2.imshow('dst', dst)cv2.waitKey()

The result is as follows:



3. From Python to C ++


Look at the execution process from Python filter2D to FilterEngine.


The first is the wrapper function of filter2D:

static PyObject* pyopencv_filter2D(PyObject* , PyObject* args, PyObject* kw){    PyObject* pyobj_src = NULL;    Mat src;    PyObject* pyobj_dst = NULL;    Mat dst;    int ddepth=0;    PyObject* pyobj_kernel = NULL;    Mat kernel;    PyObject* pyobj_anchor = NULL;    Point anchor=Point(-1,-1);    double delta=0;    int borderType=BORDER_DEFAULT;    const char* keywords[] = { "src", "ddepth", "kernel", "dst", "anchor", "delta", "borderType", NULL };    if( PyArg_ParseTupleAndKeywords(args, kw, "OiO|OOdi:filter2D", (char**)keywords, &pyobj_src, &ddepth, &pyobj_kernel, &pyobj_dst, &pyobj_anchor, &delta, &borderType) &&        pyopencv_to(pyobj_src, src, ArgInfo("src", 0)) &&        pyopencv_to(pyobj_dst, dst, ArgInfo("dst", 1)) &&        pyopencv_to(pyobj_kernel, kernel, ArgInfo("kernel", 0)) &&        pyopencv_to(pyobj_anchor, anchor, ArgInfo("anchor", 0)) )    {        ERRWRAP2( cv::filter2D(src, dst, ddepth, kernel, anchor, delta, borderType));        return pyopencv_from(dst);    }    return NULL;}

This function is automatically generated by swig. It simply wraps the C ++ function filter2D and processes the input parameters and return values. Next, let's look at the implementation process of cv: filter2D:

void cv::filter2D( InputArray _src, OutputArray _dst, int ddepth,                   InputArray _kernel, Point anchor,                   double delta, int borderType ){    Mat src = _src.getMat(), kernel = _kernel.getMat();.....    Ptr<FilterEngine> f = createLinearFilter(src.type(), dst.type(), kernel,                                             anchor, delta, borderType & ~BORDER_ISOLATED );    f->apply(src, dst, Rect(0,0,-1,-1), Point(), (borderType & BORDER_ISOLATED) != 0 );}

It is very similar to the previously mentioned sepFilter2D function!


Cv: filter2D is also a C ++ interface opened by OpenCV.











Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.