一起学opencv (五) 图片的数据计较,类的应用
来源:互联网 发布:linux grub命令进win 编辑:程序博客网 时间:2024/06/07 06:45
简单的比较彩色图数据RGB值分别于设定的三个色度值比较,大则输出白色,小则输出黑色值。
colordetector.h
#if !defined COLORDETECT#define COLORDETECT#include <opencv2/core/core.hpp>class ColorDetector {private:// minimum acceptable distanceint minDist;// target colorcv::Vec3b target;// image containing resulting binary mapcv::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 constructorColorDetector() : minDist(100) {// default parameter initialization heretarget[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 thresholdint getColorDistanceThreshold() const {return minDist;}// Sets the color to be detectedvoid setTargetColor(unsigned char red, unsigned char green, unsigned char blue) {target[2] = red;target[1] = green;target[0] = blue;}// Sets the color to be detectedvoid setTargetColor(cv::Vec3b color) {target = color;}// Gets the color to be detectedcv::Vec3b getTargetColor() const {return target;}// Processes the image. Returns a 1-channel binary image.cv::Mat process(const cv::Mat &image);};#endif
类函数cv::Mat process(const cv::Mat &image)在头文件
colordetector.cpp
#include "colordetector.h"cv::Mat ColorDetector::process(const cv::Mat &image) {// re-allocate binary map if necessary// same size as input image, but 1-channelresult.create(image.rows, image.cols, CV_8U);// get the iteratorscv::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 pixelfor (; it != itend; ++it, ++itout) {// process each pixel ---------------------// compute distance from target colorif (getDistance(*it)<minDist) {*itout = 255;}else {*itout = 0;}// end of pixel processing ----------------}return result;}
主程序
#include <opencv2/core/core.hpp>#include <opencv2/highgui/highgui.hpp>#include "colordetector.h"int main(){// Create image processor objectColorDetector cdetect;// Read input imagecv::Mat image = cv::imread("bfg.jpg");if (!image.data)return 0;// set input parameterscdetect.setTargetColor(130, 130, 90); // here blue sky// Read image, process it and display the resultcv::Mat image2 = cdetect.process(image);cv::namedWindow("result");cv::imshow("result",image2);cv::imwrite("saved.jpg", image2);cv::waitKey();return 0;}结果
1 0
- 一起学opencv (五) 图片的数据计较,类的应用
- 透视学的应用(五)
- [一起学Hive]之五-Hive的视图和分区
- [一起学Hive]之五-Hive的视图和分区
- [一起学Hive]之五-Hive的视图和分区
- 一起学opencv2 (四) 图片的整合
- 计较,是贫穷的开始
- 计较,是麻烦的开始
- 计较,是贫穷的开始
- 初学opencv-显示图片的简单应用
- 一起学WEB(五) 更加清晰的组织你的网站资源
- 一起学Windows phone7开发(五.一个时钟的例子)
- 一起学Windows phone7开发(五.一个时钟的例子)
- 一起学mini2440裸机开发(五)--定时器0的基础实验
- 青风和大家一起学Stellaris系列ARM——五.看门狗的使用
- 一起学mini2440裸机开发(五)--定时器0的基础实验
- 一起学Netty(五)之 初识ByteBuf和ByteBuf的常用API
- 一起学Netty(五)之 初识ByteBuf和ByteBuf的常用API
- Linux 内存管理
- dubbox拦截器配置
- Comparable和Comparator的使用(自定义对类的比较)
- Linux系统中切换用户身份su与sudo的用法与实例
- 基于labwindows 应用PDFlib自动生成PDF文件
- 一起学opencv (五) 图片的数据计较,类的应用
- JQuery监听页面滚动总结
- android开发,绘制圆形图片并添加文字居中显示
- Android 3D旋转 Layout
- 如何使用virtualenvwrapper切换不同版本的python
- Android,PullToRefreshListView,addHeaderView错误!
- Handler内存泄漏分析及解决
- XML布局文件出错问题解决办法
- JavaScript trim 实现(去除字符串首尾指定字符)