OPENCV Learning notes the use of policy patterns in 15_ algorithm design

Source: Internet
Author: User

Building a bug-free (bug free) application is just the beginning. What are you really want are an application, and the programmers working with you (team) would be able to easily adapt and Evolve (Modify and upgrade) as new requirements come in (with new requirements coming in, facing new requirements).

Basically (overall), a design pattern is a sound (reliable), reusable solution (reusable) to a generic problem this occurs frequently in s Oftware designing. Many software patterns has been introduced and well documented. Good programmers should build a working knowledge of these existing patterns.

The objective of the strategy [? Str?t?d?i] Design pattern (policy design mode) is to encapsulate (encapsulation) an algorithm in (algorithm) a class.

Example

Build a simple algorithm to identify all pixels in an image that have a certain color. The algorithm must enter an image and a color, and return a binary image showing pixels with a specified face. The tolerance (tolerance) with which we want to accept a color would be another parameter to be specified (specified, designation) before running the Algorithm [?? LG?R?E?M].

Once the algorithm is encapsulated into a class using the policy design pattern, the algorithm can be deployed by creating an instance of the class, typically created at the time of program initialization. When a constructor is run, an instance of the class initializes various parameters of the algorithm with default values so that it can immediately enter the available state.

Let's start with a example on what it can be deployed (deployment) and used.

#include"Colordetector.h"#include<vector>intColordetector::getdistancetotargetcolor (Constvec3b& color)Const{    returngetcolordistance (color, target);}intColordetector::getcolordistance (Constvec3b& Color1,Constvec3b& Color2)Const{    returnABS (color1[0]-color2[0]) + ABS (color1[1]-color2[1]) + ABS (color1[2]-color2[2]);}voidColordetector::setcolordistancethreshold (intdistance) {    if(Distance <0) Distance=0; MaxDist=distance;}intColordetector::getcolordistancethreshold ()Const {    returnmaxDist;}voidColordetector::settargetcolor (Uchar Blue, Uchar Green, Uchar red) {target= Vec3b (blue, green, red);//vec3b Color}voidcolordetector::settargetcolor (vec3b color) {target=color;} Vec3b Colordetector::gettargetcolor ()Const {    returnTarget;} Mat colordetector::p rocess (ConstCv::mat &image) {      //re-allocate binary Map If necessary same size as input image, but 1-channelresult.create (Image.size (), cv_8u); //Get the iteratorsMat_<cv::vec3b>::const_iterator it= image.begin<vec3b>(); Mat_<cv::vec3b>::const_iterator itend= image.end<vec3b>(); Mat_<uchar>::iterator itout= result.begin<uchar>();  for(; it!= itend; ++it, + +itout) {          //compute distance from target color          if(Getdistancetotargetcolor (*it) <maxDist) {              *itout=255; }          Else {              *itout=0; }      }      returnresult;}
main.cpp
#if!defined Colordetect#defineColordetect#include<opencv2/core/core.hpp>#include<opencv2/imgproc/imgproc.hpp>#include<iostream>using namespacestd;using namespaceCV;classColordetector {Private:      intMaxDist;//Minimum acceptable distanceVEC3B Target;//Target ColorMat result;//image containing resulting binary map   Public:      /*one reason to use the policy design pattern is to make the algorithm's deployment as simple as possible. The simplest constructor is an empty function.      It creates an instance of an algorithm class and is in a valid state.      We then initialize all the input parameters in the constructor and set them to the default values. */      //empty constructor Default parameter initialization here//colordetector (): mindist {target[0]=target[1]=target[2]=0;}Colordetector (): MaxDist (255), Target (0,0,0){}//constructor Function      /*at this point, the user who creates the algorithm class can immediately invoke the processing method and pass in a valid image, and then get a valid output. This is another purpose of the strategy pattern, which is to ensure that the algorithm works as long as the parameters are correct.      Can be made up of multiple constructors. */      //computes the distance from target color.           intGetdistancetotargetcolor (Constvec3b& color)Const;//no{}      /*vec3b Target; Target color int colordetector::getdistancetotargetcolor (const vec3b& color) const{return GetC      Olordistance (color, target);      } void Colordetector::settargetcolor (vec3b color) {target = color; }      */                  intGetcolordistance (Constvec3b& Color1,Constvec3b& Color2)Const; //Processes the image. Returns a 1-channel binary image.Mat Process (ConstMat &image); /*------Users obviously want to use personalization, you can use the appropriate design methods and methods to achieve this function. -----*/      voidSetcolordistancethreshold (intdistance); //Gets The color distance threshold      intGetcolordistancethreshold ()Const; /*two definitions of the Settargetcolor method are provided.      The first version represents three color components with three parameters, and the second version saves the color values with CV::VEC3B.      To make the algorithm class easier to use, the user only needs to select the most appropriate set-valued function. */      //sets the color to be detected      voidSettargetcolor (Uchar Blue, Uchar Green, Uchar red); //sets the color to be detected      voidsettargetcolor (vec3b color); //Gets The color to be detectedVec3b Gettargetcolor ()Const;}; //semicolons Need#endif
Colordetector.h
#include"Colordetector.h"#include<vector>intColordetector::getdistancetotargetcolor (Constvec3b& color)Const{    returngetcolordistance (color, target);}intColordetector::getcolordistance (Constvec3b& Color1,Constvec3b& Color2)Const{    returnABS (color1[0]-color2[0]) + ABS (color1[1]-color2[1]) + ABS (color1[2]-color2[2]);}voidColordetector::setcolordistancethreshold (intdistance) {    if(Distance <0) Distance=0; MaxDist=distance;}intColordetector::getcolordistancethreshold ()Const {    returnmaxDist;}voidColordetector::settargetcolor (Uchar Blue, Uchar Green, Uchar red) {target= Vec3b (blue, green, red);//vec3b Color}voidcolordetector::settargetcolor (vec3b color) {target=color;} Vec3b Colordetector::gettargetcolor ()Const {    returnTarget;} Mat colordetector::p rocess (ConstCv::mat &image) {      //re-allocate binary Map If necessary same size as input image, but 1-channelresult.create (Image.size (), cv_8u); //Get the iteratorsMat_<cv::vec3b>::const_iterator it= image.begin<vec3b>(); Mat_<cv::vec3b>::const_iterator itend= image.end<vec3b>(); Mat_<uchar>::iterator itout= result.begin<uchar>();  for(; it!= itend; ++it, + +itout) {          //compute distance from target color          if(Getdistancetotargetcolor (*it) <maxDist) {              *itout=255; }          Else {              *itout=0; }      }      returnresult;}
Colordetector.cpp

OPENCV Learning notes the use of policy patterns in 15_ algorithm design

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.