OpenCV2计算机视觉应用编程手册(自学版)初级六

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> 

using namespace std;// 使用STD

using namespace 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 cv::Mat &image) {

  // re-allocate binary map if necessary

  // same size as input image, but 1-channel

  result.create(image.rows,image.cols,CV_8U);//

 

  // 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>();

  cv::Mat_<uchar>::iterator itout= result.begin<uchar>();//输出图像的首地址

 

  // for each pixel

  for ( ; it!= itend; ++it, ++itout) {

        

// process each pixel ---------------------

 

  // compute distance from target color

  if (getDistance(*it)<minDist) {

 

  *itout= 255;

 

  } else {

 

  *itout= 0;

  }

 

        // end of pixel processing ----------------

  }

 

  return result;

}


3---------detector.h文件

类的定义,私有部分,公有部分,处理函数等。

#if !defined COLORDETECT

#define COLORDETECT

 

#include <opencv2/opencv.hpp> 

 

// 声明类。。

class ColorDetector {

 

  private://私有部分

 

  // minimum acceptable distance

  int minDist; //设置最小值

 

  // target color

  cv::Vec3b target; //目标颜色,三维向量

 

  // image containing resulting binary map

  cv::Mat result;//处理结果

 

  // 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[0]-target[0])+

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 distance) {

 

  if (distance<0)

  distance=0;

  minDist= distance;

  }

 

  // Gets the color distance threshold 得到设置的距离

  int getColorDistanceThreshold() const {

 

  return minDist;

  }

 

  // Sets the color to be detected 设置需要比较的像素的值// 这个地方需要注意了BGR B->target[0]

  void setTargetColor(unsigned char red, unsigned char green, unsigned char blue) {

  target[2]= red;

  target[1]= green;

  target[0]= blue;

  }

  // Sets the color to be detected 通过向量的方法设置目标颜色

  void setTargetColor(cv::Vec3b color) {

 

  target= color;

  }

 

  // Gets the color to be detected 通过向量的方法获得目标颜色

  cv::Vec3b getTargetColor() const {

  return target;

  }

  // Processes the image. Returns a 1-channel binary image.声明虚函数

  cv::Mat process(const cv::Mat &image);

};

#endif

 




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值