Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <opencv2/highgui/highgui.hpp>
- using namespace std;
- using namespace cv;
- int main()
- {
- Mat src = imread("aaa.png", CV_LOAD_IMAGE_GRAYSCALE);
- src.convertTo(src, CV_32FC1);
- Mat res(src.rows + 4, src.cols + 4, CV_32FC1, Scalar(0));
- Mat srcROI(res, Rect(2, 2, src.cols, src.rows));
- src.copyTo(srcROI);
- for (int i = 2; i < res.rows - 2; ++i)
- {
- for (int j = 2; j < res.cols - 2; ++j)
- {
- float err = 0;
- if (res.ptr<float>(i)[j] < 128)
- {
- err = res.ptr<float>(i)[j];
- res.ptr<float>(i)[j] = 0;
- }
- else
- {
- err = (res.ptr<float>(i)[j] - 255);
- res.ptr<float>(i)[j] = 255;
- }
- res.ptr<float>(i)[j + 1] += (err * 8 / 42.);
- res.ptr<float>(i)[j + 2] += (err * 4 / 42.);
- res.ptr<float>(i + 1)[j - 2] += (err * 2 / 42.);
- res.ptr<float>(i + 1)[j - 1] += (err * 4 / 42.);
- res.ptr<float>(i + 1)[j] += (err * 8 / 42.);
- res.ptr<float>(i + 1)[j + 1] += (err * 4 / 42.);
- res.ptr<float>(i + 1)[j + 2] += (err * 2 / 42.);
- res.ptr<float>(i + 2)[j - 2] += (err * 1 / 42.);
- res.ptr<float>(i + 2)[j - 1] += (err * 2 / 42.);
- res.ptr<float>(i + 2)[j] += (err * 4 / 42.);
- res.ptr<float>(i + 2)[j + 1] += (err * 2 / 42.);
- res.ptr<float>(i + 2)[j + 2] += (err * 1 / 42.);
- }
- }
- srcROI.convertTo(srcROI, CV_8UC1);
- imwrite("ED_1976.jpg", srcROI);
- }
Add Comment
Please, Sign In to add comment