-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcvtest.cpp
More file actions
77 lines (75 loc) · 2.1 KB
/
Copy pathcvtest.cpp
File metadata and controls
77 lines (75 loc) · 2.1 KB
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
#include <opencv2/core.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main(int argc, char** argv)
{
try {
String imageName("billy.png"); // by default
if (argc > 1)
{
imageName = argv[1];
}
cout << imageName << std::endl;
Mat image;
image = imread(imageName, IMREAD_COLOR); // Read the file
if (image.empty()) // Check for invalid input
{
cout << "Could not open or find the image" << std::endl;
return -1;
}
cvtColor(image, image, COLOR_BGR2GRAY);
int width = image.cols;
int height = image.rows;
double val;
double error;
int pointer = 0;
double* passahead = new double[width + 1];
for (int z = 0; z < (width + 1); z++) {
passahead[z] = 0;
}
for (int y = 0; y < height; y++) {
for (int x = 0; x < (width-1); x++) {
error = passahead[pointer] + image.at<uint8_t>(y, x);
if (error > 127) {
val = 255;
}
else {
val = 0;
}
error -= val;
image.at<uint8_t>(y, x) = val;
pointer = (pointer + 1) % (width + 1);
passahead[pointer] += error * 7 / 16;
passahead[(pointer + width) % (width + 1)] = error * 1 / 16;
passahead[(pointer + width - 2) % (width + 1)] += error * 3 / 16;
passahead[(pointer + width - 1) % (width + 1)] += error * 5 / 16;
}
error = passahead[pointer] + image.at<uint8_t>(y, width-1);
if (error > 127) {
val = 255;
}
else {
val = 0;
}
error -= val;
image.at<uint8_t>(y, width-1) = val;
pointer = (pointer + 1) % (width + 1);
passahead[(pointer + width - 2) % (width + 1)] += error * 3 / 16;
passahead[(pointer + width - 1) % (width + 1)] += error * 5 / 16;
}
delete[] passahead;
//namedWindow("Display window", WINDOW_AUTOSIZE); // Create a window for display.
//imshow("Display window", image); // Show our image inside it.
//waitKey(0); // Wait for a keystroke in the window
imwrite(imageName+"dith.png", image);
return 0;
}
catch (cv::Exception & e)
{
cerr << e.msg << endl; // output exception message
}
}