Ads removed from the pastie. \o/
Running costs stayed. :-(
Donations welcome!

Bitcoin: 1QC38uPBSy7WSYz1whM5SYTHAdhqkN6o8V
Ethereum: 0x0855778a6181a69cc7d5d08c71c4f5846010eb67
Verge: D8fo1fszrNmUK4kfQW5owhYMv3osPvgn2g
Recent entries
2020-03-31 19:24:18
film complet en hd
2020-03-31 19:15:43
movie
2020-03-31 19:10:34
Multi Video Full Movie Se...
2020-03-31 19:06:00
No title specified
2020-03-31 18:59:56
No title specified
2020-03-31 18:36:07
movie
2020-03-31 17:55:21
https://medium.com/@jeann...
2020-03-31 17:54:38
chjyjhj
2020-03-31 17:48:25
BERKAHBERSEDEKAH
2020-03-31 17:47:47
https://medium.com/@jeann...
Permanent entries
2019-10-14 13:05:09
Shift-JIS to UTF-8 mappin...
2014-10-14 21:12:28
Boost property tree - Ite...
2013-10-18 08:24:21
Resize images in a folder...
2013-04-29 17:23:35
Find bad words in a text,...
2013-02-12 20:26:45
Merge two boost property ...
2012-12-18 16:21:40
OpenCV: Access/modify ima...
2012-11-19 10:48:40
Reading a boost::property...
2012-04-10 00:11:46
Convert hex to string or ...
2012-02-18 00:12:04
Screw up []-operator for ...
2012-02-16 03:02:24
Merge continuous numbers ...
2012-01-18 22:26:01
(PHP) Generate / Create (...
2011-04-19 16:05:25
Calculate one's age in ye...
2011-04-18 15:26:45
(PHP) Generate / Create r...
2010-08-26 15:18:00
Pick the first element (k...
2010-07-28 10:36:45
Quakenet - Snailbot comma...
2010-06-21 21:30:52
Using the MATLAB profiler...
2010-05-31 06:31:14
PHP truncation function
2010-05-06 20:49:26
Quakenet - Fishbot comman...
Paste ID: # lszfp
Date posted: Tue, 18 Dec 2012 16:21:40 +0000
Date of expiration: never (permanent entry)
Description:

OpenCV: Access/modify image with multiple (here: 3) channels

  1. #include <opencv2\opencv.hpp>
  2.  
  3.  
  4. using namespace cv;
  5.  
  6. /**Example that shows how to access an image with three channels using OpenCV version 2.
  7. */
  8. int main()
  9. {
  10.   // Pretend Mat is an image in HSV color space
  11.   Mat hsv( 512, 512, CV_8UC3 );
  12.  
  13.   // Row-wise
  14.   for( int r = 0; r < hsv.rows; ++r )
  15.   {
  16.     // Pointer to beginning of current row
  17.     unsigned char *row = hsv.ptr( r );
  18.  
  19.     // Column-wise
  20.     for( int c = 0; c < hsv.cols; ++c )
  21.     {
  22.       /*
  23.       In order to access a certain channel of a certain pixel
  24.       multiply column's index by count of channels and
  25.       add channel's index as offset. The following sketch shows
  26.       a 1x3 image with 3 channels:
  27.       ---------------------------------------------------
  28.          channel   | 0 : 1 : 2 | 0 : 1 : 2 | 0 : 1 : 2 |
  29.           (e.g.)   | h : s : v | h : s : v | h : s : v |
  30.           (e.g.)   | b : g : r | b : g : r | b : g : r |
  31.       ---------------------------------------------------   . . .
  32.          column    |     0     |     1     |     2     |
  33.       ---------------------------------------------------
  34.       */
  35.       // More or less random values (unintentionally chosen)
  36.       row[3*c]   = 0;   // hue        ::  blue in BGR color space
  37.       row[3*c+1] = r/2; // saturation ::  green in BGR color space
  38.       row[3*c+2] = c/2; // value      ::  red in BGR color space
  39.     }
  40.   }
  41.  
  42.   // Save Mat as image
  43.   // Note that images HSV color space will be stored as they were in BGR color space
  44.   // so colors might be confusing people unfamiliar with HSV color space
  45.   imwrite ( "X:\\hsv.png", hsv );
  46.  
  47.   // Save as an image in BGR color space
  48.   Mat bgr;
  49.   cvtColor( hsv, bgr, CV_HSV2BGR ); // convert to BGR color space
  50.   imwrite ( "X:\\bgr.png", bgr );
  51.  
  52.  
  53.   return 0;
  54. }
You can paste a correction or amendment to the upper paste or a new paste down below.