-
Notifications
You must be signed in to change notification settings - Fork 37
/
1359-1
45 lines (27 loc) · 837 Bytes
/
1359-1
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
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main()
{
Mat img_color = imread("image.jpg", IMREAD_COLOR);
int height = img_color.rows;
int width = img_color.cols;
Mat img_grayscale(height, width, CV_8UC1);
for (int y = 0; y < height; y++) {
uchar* pointer_input = img_color.ptr<uchar>(y);
uchar* pointer_ouput = img_grayscale.ptr<uchar>(y);
for (int x = 0; x < width; x++) {
// 컬러영상의 경우 픽셀값 읽어오기
uchar b = pointer_input[x * 3 + 0];
uchar g = pointer_input[x * 3 + 1];
uchar r = pointer_input[x * 3 + 2];
//그레이스케일의 경우 픽셀값 저장하기
pointer_ouput[x] = (r + g + b) / 3.0;
}
}
imshow("color", img_color);
imshow("grayscale", img_grayscale);
waitKey(0);
return 0;
}