05-05 10:44
Notice
Recent Posts
Recent Comments
관리 메뉴

Scientific Computing & Data Science

[OpenCV] 원본 이미지 상에 컬러 입히기 본문

Programming/OpenCV

[OpenCV] 원본 이미지 상에 컬러 입히기

cinema4dr12 2015. 7. 8. 22:58

이미지를 읽어온 후 일부 영역에 지정된 컬러를 입히는 예제코드입니다.

Example Code

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
42
43
44
45
46
47
48
49
#include "stdafx.h"
#include <opencv2/opencv.hpp>
 
#include <stdlib.h>
#include <stdio.h>
 
using namespace std;
using namespace cv;
 
/** @function main */
int main( int argc, char** argv )
{
    // Read an image
    cv::Mat image = cv::imread( {YOUR_IMAGE_PATH}, 1 );
 
    uchar *data = image.data;
    uchar *blue, *green, *red;
 
    blue = nullptr;
    green = nullptr;
    red = nullptr;
 
    int nRow = image.rows;
    int nCol = image.cols;
 
    int gray = 0;
 
    // fills colors in the specified range
    forint j = 0 ; j < nRow - 1 ; j++ ) {
        gray = (int)( 255 * j / (nRow - 1) );
 
        forint i = 0 ; i < nCol - 1 ; i++ ) {
            image.at<Vec3b>( j, i )[0= gray;
            image.at<Vec3b>( j, i )[1= abs( 255 - gray );
            image.at<Vec3b>( j, i )[2= i % 255;
        }
    }
    
    // creates window
    namedWindow( "Image"1 );
 
    // show stuff
    imshow( "Image", image );
 
    // Wait until user press some key
    waitKey();
 
    return 0;
}
cs

 

Comments