OpenCV2计算机视觉应用编程手册(自学版)初级六
第六个opencv程序---类的学习 天空区域识别、图像分割
如果没接触过类最好稍微读一下C++中类的介绍,不用很深,一点知识就够了。整个封装做到比较完美(对于我这个入门的来说)。
本部分主要包含三个文件,sys_main.c文件,detector.cpp文件,detector.h文件sys_main.c文件就是我们的主文件,detector.h文件是我们声明的类的存放文件,detector.cpp文件,是声明的类的虚函数定义文件。
1---------sys_main.c文件
用类声明一个对象,然后初始化对象,并调用对象的函数进行处理。
/*
第六个opencv程序---类的学习,识别天空区域,图像分割
时间:2014年12月1日22:17:53
整理:天空之恋
测试类的使用,使用C++中的类处理
*/
#include"stdafx.h"
#include<iostream>
#include"detector.h"
#include<opencv2/opencv.hpp>
usingnamespace std;// 使用STD
usingnamespace cv;// 使用名字空间
int main()
{
// Create image processor object
ColorDetector cdetect;// 声明图像处理的类对象
// Read input image
cv::Mat image= cv::imread("F:\\house.jpg");
if (!image.data)
return 0;
// set input parameters
cdetect.setTargetColor(130,190,230); // here blue sky这个像素代表的是天空区域的值,这样我们把每个点
//的像素与这个点进行比较,最后我们就可以
//识别出天空区域
// Read image, process it and display the result
cv::namedWindow("result");
cv::imshow("result",cdetect.process(image));//调用类封装中的处理函数process
cv::waitKey();
return 0;
}
2--------detector.cpp文件
对声明的类的虚函数process进行重新定义,实现计算距离的功能。
#include "detector.h"
// 虚函数的重定义,虚函数可以使我们使用同一个名,然后执行不同的操作
cv::Mat ColorDetector::process(const
// re-allocate binary map if necessary
// same size as input image, but 1-channel
//
// get the iterators 使用迭代器访问图像的像素
cv::Mat_<cv::Vec3b>::const_iterator it= image.begin<cv::Vec3b>();
cv::Mat_<cv::Vec3b>::const_iterator itend= image.end<cv::Vec3b>();
//输出图像的首地址
// for each pixel
for
// process each pixel ---------------------
// compute distance from target color
if
*itout= 255;
else
*itout= 0;
}
// end of pixel processing ----------------
}
return
}
3---------detector.h文件
类的定义,私有部分,公有部分,处理函数等。
#ifdefined
#define COLORDETECT
#include<opencv2/opencv.hpp>
// 声明类。。
class ColorDetector {
private://私有部分
// minimum acceptable distance
int minDist; //设置最小值
// target color
//目标颜色,三维向量
// image containing resulting binary map
//处理结果
// inline private member function
// Computes the distance from target color. 计算与目标的距离
int getDistance(const cv::Vec3b& color) const
// return static_cast<int>(cv::norm<int,3>(cv::Vec3i(color[0]-target[0],color[1]-target[1],color[2]-target[2])));
return
abs(color[1]-target[1])+
abs(color[2]-target[2]);
}
public://共有部分
// empty constructor 构造函数
ColorDetector() : minDist(100) {
// default parameter initialization here
target[0]= target[1]= target[2]= 0;
}
// Getters and setters
// Sets the color distance threshold.
// Threshold must be positive, otherwise distance threshold
// is set to 0. 设置距离
void setColorDistanceThreshold(int
if
distance=0;
minDist= distance;
}
// Gets the color distance threshold 得到设置的距离
int getColorDistanceThreshold() const
return
}
// Sets the color to be detected 设置需要比较的像素的值// 这个地方需要注意了BGR B->target[0]
void setTargetColor(unsigned char red, unsigned char green, unsigned char
target[2]= red;
target[1]= green;
target[0]= blue;
}
// Sets the color to be detected 通过向量的方法设置目标颜色
void
target= color;
}
// Gets the color to be detected 通过向量的方法获得目标颜色
const
return
}
// Processes the image. Returns a 1-channel binary image.声明虚函数
const
};
#endif