-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhw1.cpp
46 lines (46 loc) · 1.33 KB
/
hw1.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
42
43
44
45
46
// author: Alex Hung
#include <opencv/cv.hpp>
#include <iostream>
#include <exception>
using namespace cv;
using namespace std;
void bgr2gray(Mat& src, Mat& tgt) {
/// Convert RGB to Grey-level
/// I = (R+G+B)/3
assert(src.rows == tgt.rows);
assert(src.cols == tgt.cols);
for (int i = 0; i < src.cols; i++) { // for each rows
for (int j = 0; j < src.rows; j++) { // for each cols
tgt.at<uchar>(j, i) =
(src.at<Vec3b>(j, i)[0] + src.at<cv::Vec3b>(j, i)[1] +
src.at<cv::Vec3b>(j, i)[2]) /
3;
}
}
}
int main(int argc, char* argv[]) {
if (argc < 3) {
cout << "usage: ./hw1 input-img output-color-img output-greylevel-img\n"
<< "eg: ./hw1 img/lena512color.tiff img/lena512color.jpg "
<< "img/lena512bw.tiff\n";
exit(1);
}
try {
Mat img = imread(argv[1], CV_LOAD_IMAGE_COLOR);
imwrite(argv[2], img);
Mat grey_img(img.rows, img.cols, CV_8UC1);
///// cvtColor(img, grey_img, CV_BGR2GRAY);
bgr2gray(img, grey_img);
imwrite(argv[3], grey_img);
#ifdef SHOW_IMG_WINDOW
namedWindow("Input image", CV_WINDOW_AUTOSIZE);
namedWindow("Gray image", CV_WINDOW_AUTOSIZE);
imshow("Input image", img);
imshow("Gray image", grey_img);
waitKey(0);
#endif
} catch (exception& e) {
cerr << "exception caught: " << e.what() << '\n';
}
return 0;
}