본문 바로가기

C

c++ sobel operator 구현 코드

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
void convert_greyscale_image_to_sobel(void)
{
    unsigned const int n = 3;
    int sobel_x[n][n] = {-1,-2,-1,0,0,0,1,2,1};
    int sobel_y[n][n] = {-1,0,1,-2,0,2,-1,0,1};
    int R_patch[n][n] = { 00,0,0,0,0,0,0,0};
 
    int patch_y = -1;
    int patch_x = -1;
    for (int i = 0; i < (int)context.image_height; i++)
    {   
        for (int j = 0; j < context.image_width; j++)
        {
            
            for (int patch_y=-1; patch_y < 2; patch_y++)
            {
                for (int patch_x=-1; patch_x < 2; patch_x++)
                {
                    int image_idx_y = i + patch_y;
                    int image_idx_x = j + patch_x;
 
                    if (image_idx_y == -1)
                    {
                        image_idx_y = 0;
                        if (image_idx_x == -1)
                        {
                            image_idx_x = 0;
                        }
                    }
                    if (image_idx_x == -1)
                    {
                        image_idx_x = 0;
                        if (image_idx_y == -1)
                            image_idx_y = 0;
                    }
 
                    if (image_idx_x == context.image_width)
                    {
                        image_idx_x = context.image_width - 1;
 
                        if (image_idx_y == context.image_height)
                        {
                            image_idx_y = context.image_height - 1;
                        }
                            
                    }
                    if (image_idx_y == context.image_height)
                    {
                        image_idx_y = context.image_height - 1;
                        if (image_idx_x == context.image_width)
                        {
                            image_idx_x = context.image_width - 1;
                        }
                    }
                    int offset_patch = (image_idx_y)*context.image_width + (image_idx_x);
                    R_patch[patch_y + 1][patch_x + 1= context.SoA_image_output.R_plane[offset_patch];             
 
                }
            }
            int offset = i*context.image_pitch + 4*j;
            int RGBA_SUMX = 0;
            int RGBA_SUMY = 0;
            for (int y = 0; y < 3; y++
            {
                for (int x = 0; x < 3; x++)
                {
                    RGBA_SUMX += R_patch[y][x] * sobel_x[y][x];
                    RGBA_SUMY += R_patch[y][x] * sobel_y[y][x];
                }
            }
            context.output.image_data[offset] = (BYTE)sqrt(RGBA_SUMX* RGBA_SUMX+ RGBA_SUMY* RGBA_SUMY);
            context.output.image_data[offset + 1= context.output.image_data[offset];
            context.output.image_data[offset + 2= context.output.image_data[offset];
            
        }
    }
}
cs