Python image Processing (4): Filter

Source: Internet
Author: User

Happy Shrimp

http://blog.csdn.net/lights_joy/(QQ group: Visual embedlinux Tools 375515651)

Welcome reprint, but please keep the author information


The filter is widely used in image processing, OpenCV There is also a function that directly uses the filter mask (kernel) filter2d , the image and kernel are convolution to obtain the target image. Convolution is an operation between each block of images and an operator (nucleus), and the kernel is a fixed-size array of values.


in fact, in OpenCV Many of the algorithms are implemented by convolution, including some edge detection algorithms. This article is not intended to enumerate the principles of these algorithms, just to examine The implementation of OpenCV, and then try to call these algorithms with Python to see its effect.


The test image used is still it:


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


1. Filterengine


in the OpenCV The operation of the filter in the Filterengine This class to complete, in the source code comments clearly explain the role of this class:


The Main Class for Image Filtering. The class can is used to apply a 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 is returned by various OpenCV functions, such as Cv::createseparab Lelinearfilter (), Cv::createlinearfilter (), Cv::creategaussianfilter (), Cv::createderivfilter (), CV:: Createboxfilter () and Cv::createmorphologyfilter (). Using The class can process large images by parts and build complex pipelines that include filtering as some of the St Ages. If all you need are to apply some pre-defined filtering operation, you may use cv::filter2d (), Cv::erode (), CV::d ilate () et C. Functions that create filterengine internally.

Below is Filterengine A typical way to use:

void Cv::sepfilter2d (Inputarray _src, Outputarray _dst, int ddepth,                      inputarray _kernelx, Inputarray _kernelY, point A Nchor,                      double delta, int bordertype) {    Mat src = _src.getmat (), Kernelx = _kernelx.getmat (), kernely = _kernely.ge Tmat ();    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 the note says, we hardly need to use this class directly, but understand its usage for our understanding and debugging OpenCV is very good.


2. filter2d


Let's try to give the matrix of a filter a look at the effect.

#-*-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 this:



3. fromPythonto theC + +


look from Python of the filter2d to the Filterengine the execution process.


First, filter2d of the wrapper function:

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, &AMP;PYOBJ_DST, &pyobj_anchor, &delta, &bordertype) && pyopencv_to (pyobj_src, SRC, arginfo ("src", 0)) && pyopencv_t        O (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, Kern El, anchor, DelTA, Bordertype));    Return Pyopencv_from (DST); } return NULL;

This function isSwigautomatically generated, simply tofilter2dthisC + +The function is wrapped and processed with input parameters and return values. Then lookcv::filter2dthe implementation process:

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);}

and the above mentioned sepfilter2d The function is very similar!


cv::filter2d is also OpenCV The open one C + + interface.











??

Python image Processing (4): Filter

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.