Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <opencv2/core.hpp>
- #include <opencv2/imgcodecs.hpp>
- #include <opencv2/highgui.hpp>
- #include <opencv2/imgproc.hpp>
- #include <iostream>
- #include <fstream>
- #include <string>
- #include <cmath>
- #include <conio.h>
- #include <fcntl.h>
- #include <io.h>
- using namespace std;
- using namespace cv;
- int Des[2100][2100];
- double Err[2100][2100];
- int quiz73()
- {
- Mat image = imread("../image/aerk.jpg", IMREAD_GRAYSCALE);
- uchar **pointer = (uchar**)malloc(sizeof(uchar*)*image.rows);
- for (int i = 0; i < image.rows; i++)
- pointer[i] = image.ptr<uchar>(i);
- for (int i = 0; i < image.rows; i++)
- for (int j = 0; j < image.cols; j++)
- Des[i][j] = 1;
- for (int i = 0; i < image.rows*image.cols / 2; i++)
- {
- int x = rand() % image.cols, y = rand() % image.rows;
- if (Des[y][x] == 1)
- {
- Des[y][x] = 0;
- pointer[y][x] = 255;
- }
- else
- i--;
- }
- Mat resoult(image.rows, image.cols, CV_8UC1, Scalar(255));
- uchar **res = (uchar**)malloc(sizeof(uchar*)*image.rows);
- for (int i = 0; i < image.rows; i++)
- res[i] = resoult.ptr<uchar>(i);
- for (int i = 0; i < image.rows; i++)
- for (int j = 0; j < image.cols; j++)
- res[i][j] = rand() % 256;
- double Esum = 0, Eold, temp, lambda = 0.000000002, cdf[257], T = 0.00001;
- int start, stop,s=0;
- for (int i = 0; i < image.rows; i++)
- for (int j = 0; j < image.cols; j++)
- {
- Err[i][j] = (res[i][j] - pointer[i][j])*(res[i][j] - pointer[i][j])*Des[i][j];
- if (i + 1 < image.rows) Err[i][j] += lambda*(res[i + 1][j] - res[i][j])*(res[i + 1][j] - res[i][j]);
- if (j + 1 < image.cols) Err[i][j] += lambda*(res[i][j + 1] - res[i][j])*(res[i][j + 1] - res[i][j]);
- Esum += Err[i][j];
- }
- int n = 0;
- do
- {
- string cd = "../resoult/image" + to_string(n) + ".jpg";
- n++;
- imwrite(cd, resoult);
- printf("%lf\t%lf\n", Esum, T);
- Eold = Esum;
- for (int i = 0; i < image.rows; i++)
- for (int j = 0; j < image.cols; j++)
- {
- cdf[0] = 0;
- for (s = 0; s < 256; s++)
- {
- temp = (s - pointer[i][j])*(s - pointer[i][j])*Des[i][j];
- if (i + 1 < image.rows) temp += lambda*(res[i + 1][j] - s)*(res[i + 1][j] - s);
- if (j + 1 < image.cols) temp += lambda*(res[i][j + 1] - s)*(res[i][j + 1] - s);
- cdf[s] += pow(2.71828182, -(temp / T));
- cdf[s + 1] = cdf[s];
- }
- for (s = 0; s < 256; s++) cdf[s] /= cdf[255];
- temp = (rand() % 1000)/1000.0;
- for (s = 0; s < 254 && cdf[s] < temp; s++);
- res[i][j] = s;
- }
- Esum = 0;
- for (int i = 0; i < image.rows - 1; i++)
- for (int j = 0; j < image.cols - 1; j++)
- {
- Err[i][j] = (res[i][j] - pointer[i][j])*(res[i][j] - pointer[i][j])*Des[i][j];
- if (i + 1 < image.rows) Err[i][j] += lambda*(res[i + 1][j] - res[i][j])*(res[i + 1][j] - res[i][j]);
- if (j + 1 < image.cols) Err[i][j] += lambda*(res[i][j + 1] - res[i][j])*(res[i][j + 1] - res[i][j]);
- Esum += Err[i][j];
- }
- T *= 0.9;
- } while (Esum != Eold);
- imshow("Image2", resoult);
- imshow("Image1", image);
- waitKey(0);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement