Effect: Extract the Blue sky part
#include <opencv2/core/core.hpp>#include<opencv2/imgproc/imgproc.hpp>#include<opencv2/highgui/highgui.hpp>using namespaceCV;using namespacestd;classcolordetector{Private: intmindist; VEC3B Target; Mat result; Public: Colordetector (): Mindist ( -) {target[0] = target[1] = target[2] =0; } //set the color distance threshold voidSetcolordistancethreshold (intdistance) { if(Distance <0) Distance=0; Mindist=distance; } //The Get Set method sets the variables inside the private intGetcolordistancethreshold ()Const{ returnmindist; } //set the color to be detected voidSettargetcolor (unsignedCharRed, unsignedCharGreen, unsignedCharBlue) {target[2] =Red; target[1] =Green; target[0] =Blue; } voidsettargetcolor (vec3b color) {target=color; } vec3b Gettargetcolor ()Const{ returnTarget; } //calculates the distance from the target color, using the block distance intGetdistance (Constvec3b& color)Const{ returnABS (color[0]-target[0]) +ABS (color[1]-target[1]) +ABS (color[2]-target[2]); } Mat Colordetector::p rocess (ConstMat &image) {result.create (image.rows,image.cols, cv_8u); Mat_<vec3b>::const_iterator it = image.begin<vec3b>(); Mat_<vec3b>::const_iterator itend = image.end<vec3b>(); Mat_<uchar>::iterator itout = result.begin<uchar>(); for(; it! = Itend; ++it, + +)itout) { if(Getdistance (*it) <mindist) { *itout =255; } Else{ *itout =0; } } returnresult; }};intMain () {//Creating ObjectsColordetector Cdector; Mat Image= Imread ("boldt.jpg"); if(!image.data) {return 0; } Cdector.settargetcolor ( the, the, the); Namedwindow ("result"); Imshow ("result", cdector.process (image)); Waitkey (0); return 0;}
Write a good C + + code, using strategy mode