2014/02/20

CImg로 이미지 불러와서 CUDA로 처리하기 첫스텝

 CImg를 이용해서 이미지를 불러오니 너무 편하더군요. 나중에 어떻게 될런지는 모르겠지만요. ㅋ


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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include <iostream>
#include <CImg.h>
#include <cuda.h>
using namespace cimg_library;
using namespace std;
 
//블럭 사이즈는 16x16 = 256. 블럭 하나의 한계치가 얼마인지 모르지만, 16x16이 안전할 듯
//한계치는 천천히 배워가면 됨.
//depth와 spectrum이 추가되어서 혹시나해서 12로 바꿈
//그러다 다시 16으로 바꿈. 그래도 이미지 값은 꽤 큼
#define BLOCK_SIZE 16
 
// 20140220 복사는 되는데, 자료값이 엉망이 됨. ㅋㅋㅋ
 
__global__ void add(float * input, float * output, unsigned int W, unsigned H, unsigned int D, unsigned int S )
{
    //개별 블럭의 좌표
    unsigned int tx = threadIdx.x;
    unsigned int ty = threadIdx.y;
 
    //전체 이미지의 좌표
    unsigned int x = tx + blockDim.x * blockIdx.x;
    unsigned int y = ty + blockDim.y * blockIdx.y;
 
    if( x < W && y < H  )
    {
        output[ x + W * y ] = input[ x + W * y ];
        __syncthreads();
    }
}
 
int main()
{
    // GPU 메모리
    //unsigned char *devInputImage, *devOutputImage;
    float *devInputImage, *devOutputImage;
 
    // 읽어온 이미지의 넓이과 높이, 색깔 채널, CImg에서는 이미지 자료에 Depth. Spectrum, share가 들어갑니다.
    unsigned int ImageWidth, ImageHeight, ImageDepth, ImageSpectrum;
    unsigned int ImageSize;
    bool ImageShared;
 
    // 이미지 읽어오기
    CImg<unsigned char> image("images.jpg");
 
    //원본 이미지 값을 가져옵니다.
    ImageWidth = image._width;
    ImageHeight = image._height;
    ImageDepth = image._depth;
    ImageSpectrum = image._spectrum;
    ImageShared = image._is_shared;
 
    //복사할 자리 만들기
    CImg<unsigned char> visu( ImageWidth, ImageHeight, ImageDepth, ImageSpectrum, ImageShared);
 
    //이미지 크기 좀 알자
    cout << "Image Width: " << ImageWidth << endl;
    cout << "Image Height: " << ImageHeight << endl;
    cout << "Image Depth: " << ImageDepth << endl;
    cout << "Image Spectrum: " << ImageSpectrum << endl;
 
    //이미지 크기
    //이것만 해도 될 것을. ㅋㅋ
    ImageSize = image.size();
    cout << "Image Size: " << ImageSize << endl;
 
    // GPU에 자리 마련하기, 크기는 이미지 크기 x 색상 x unsigned char
    // 크기에는 주의, 원본 크기 그대로 가져오는 게 중요
    cudaMalloc((void**)&devInputImage, ImageSize * sizeof(unsigned char) );
    cudaMalloc((void**)&devOutputImage, ImageSize * sizeof(unsigned char) );
 
    // 자리도 마련했으니 데이터를 복사해줌.
    cudaMemcpy( devInputImage, image, ImageSize * sizeof(unsigned char), cudaMemcpyHostToDevice);
 
    // 블럭의 크기 지정
    dim3    dimBlocksize( BLOCK_SIZE, BLOCK_SIZE );
    dim3    dimGridsize( ceil((ImageWidth-1)/BLOCK_SIZE) + 1, ceil((ImageHeight-1)/BLOCK_SIZE) + 1 );
 
    add<<< dimGridsize, dimBlocksize >>>( devInputImage, devOutputImage, ImageWidth, ImageHeight, ImageDepth, ImageSpectrum );
 
    //일이 끝났으니 결과물을 CPU로 복사함.
    //결과물을 GPU에서 CPU로 복사
    cudaMemcpy(visu, devOutputImage, ImageSize * sizeof(unsigned char), cudaMemcpyDeviceToHost);
 
    //결과물 눈으로 확인
    CImgDisplay main_disp(image,"Original"), copy_disp(visu,"Copy");
 
    while( !main_disp.is_closed() && !copy_disp.is_closed() )
        main_disp.wait();
 
    // 위에 GPU에 마련한 자리 해소. 그때 그때 해놓는 게 편할 듯
    cudaFree( devInputImage );
    cudaFree( devOutputImage );
 
    return 0;
 
}
 

 수많은 삽질을 겪고 나서 겨우 이미지 복사에 성공했습니다. 물론 그냥

 < 복사 CImg = 원본 CImg >

해도 되지만, 일단 GPU에 넣고 나서 다시 빼오는 게 목적이라서요. 이 다음에는 원본 이미지를 GPU에서 매트릭스 처리 하는 걸 해볼랍니다.

댓글 없음: