Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
3 | PointedEar | 1 | /*------------------------------------------------------------------------------------------*\ |
2 | This file contains material supporting chapter 3 of the cookbook: |
||
3 | Computer Vision Programming using the OpenCV Library. |
||
4 | by Robert Laganiere, Packt Publishing, 2011. |
||
5 | |||
6 | This program is free software; permission is hereby granted to use, copy, modify, |
||
7 | and distribute this source code, or portions thereof, for any purpose, without fee, |
||
8 | subject to the restriction that the copyright notice may not be removed |
||
9 | or altered from any source or altered source distribution. |
||
10 | The software is released on an as-is basis and without any warranties of any kind. |
||
11 | In particular, the software is not guaranteed to be fault-tolerant or free from failure. |
||
12 | The author disclaims all warranties with regard to this software, any use, |
||
13 | and any consequent failure, is purely the responsibility of the user. |
||
14 | |||
15 | Copyright (C) 2010-2011 Robert Laganiere, www.laganiere.name |
||
16 | \*------------------------------------------------------------------------------------------*/ |
||
17 | |||
18 | #include "colordetector.h" |
||
19 | |||
20 | cv::Mat ColorDetector::process(const cv::Mat &image) { |
||
21 | |||
22 | // re-allocate binary map if necessary |
||
23 | // same size as input image, but 1-channel |
||
24 | result.create(image.rows,image.cols,CV_8U); |
||
25 | |||
26 | // re-allocate intermediate image if necessary |
||
27 | converted.create(image.rows,image.cols,image.type()); |
||
28 | |||
29 | // Converting to Lab color space |
||
30 | cv::cvtColor(image, converted, CV_BGR2Lab); |
||
31 | |||
32 | // get the iterators |
||
33 | cv::Mat_<cv::Vec3b>::iterator it= converted.begin<cv::Vec3b>(); |
||
34 | cv::Mat_<cv::Vec3b>::iterator itend= converted.end<cv::Vec3b>(); |
||
35 | cv::Mat_<uchar>::iterator itout= result.begin<uchar>(); |
||
36 | |||
37 | // for each pixel |
||
38 | for ( ; it!= itend; ++it, ++itout) { |
||
39 | |||
40 | // process each pixel --------------------- |
||
41 | |||
42 | // compute distance from target color |
||
43 | if (getDistance(*it)<minDist) { |
||
44 | |||
45 | *itout= 255; |
||
46 | |||
47 | } else { |
||
48 | |||
49 | *itout= 0; |
||
50 | } |
||
51 | |||
52 | // end of pixel processing ---------------- |
||
53 | } |
||
54 | |||
55 | return result; |
||
56 | } |
||
57 |