05-13 07:28
Notice
Recent Posts
Recent Comments
관리 메뉴

Scientific Computing & Data Science

[OpenCV] Reducing Color Space (Quantization) 본문

Programming/OpenCV

[OpenCV] Reducing Color Space (Quantization)

cinema4dr12 2015. 2. 22. 23:02

landscape.jpg

colorReduce.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include "stdafx.h"
#include "opencv2\opencv.hpp"
 
/////////////////////////////////////////////////////////////////////////
// colorReduce
void colorReduce( cv::Mat &image, int div = 64 ) {
    int nl = image.rows; // number of lines
    
    // total number of elements per line
    int nc = image.cols * image.channels();
 
    for ( int j = 0; j < nl; j++ ) {
        // get the address of row j
        uchar* data = image.ptr<uchar>( j );
        
        for ( int i = 0; i < nc ; i++ ) {
            // process each pixel ---------------------
            data[ i ] = data[ i ] / div*div + div/2;
            // end of pixel processing ----------------
        } // end of line
    }
}
 
/////////////////////////////////////////////////////////////////////////
// _tmain
int _tmain( int argc, _TCHAR* argv[] )
{
    // open the image
    cv::Mat image = cv::imread( "../landscape.jpg" );
    
    // process the image
    colorReduce( image );
    
    // display image
    cv::namedWindow( "Image" );
    cv::imshow( "Image", image );
 
    cv::waitKey( 5000 );
 
    return 0;
}
cs

Result


Comments