Advertisement
Guest User

Untitled

a guest
Mar 26th, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.99 KB | None | 0 0
  1. #include <opencv2/core.hpp>
  2. #include <opencv2/imgcodecs.hpp>
  3. #include <opencv2/highgui.hpp>
  4. #include <opencv2/imgproc.hpp>
  5. #include <iostream>
  6. #include <fstream>
  7. #include <string>
  8. #include <cmath>
  9. #include <conio.h>
  10. #include <fcntl.h>
  11. #include <io.h>
  12.  
  13. using namespace std;
  14. using namespace cv;
  15.  
  16. int Des[2100][2100];
  17. double Err[2100][2100];
  18.  
  19. int quiz73()
  20. {
  21.     Mat image = imread("../image/aerk.jpg", IMREAD_GRAYSCALE);
  22.  
  23.     uchar **pointer = (uchar**)malloc(sizeof(uchar*)*image.rows);
  24.     for (int i = 0; i < image.rows; i++)
  25.         pointer[i] = image.ptr<uchar>(i);
  26.  
  27.     for (int i = 0; i < image.rows; i++)
  28.         for (int j = 0; j < image.cols; j++)
  29.             Des[i][j] = 1;
  30.  
  31.     for (int i = 0; i < image.rows*image.cols / 2; i++)
  32.     {
  33.         int x = rand() % image.cols, y = rand() % image.rows;
  34.         if (Des[y][x] == 1)
  35.         {
  36.             Des[y][x] = 0;
  37.             pointer[y][x] = 255;
  38.         }
  39.         else
  40.             i--;
  41.     }
  42.  
  43.     Mat resoult(image.rows, image.cols, CV_8UC1, Scalar(255));
  44.  
  45.     uchar **res = (uchar**)malloc(sizeof(uchar*)*image.rows);
  46.     for (int i = 0; i < image.rows; i++)
  47.         res[i] = resoult.ptr<uchar>(i);
  48.  
  49.     for (int i = 0; i < image.rows; i++)
  50.         for (int j = 0; j < image.cols; j++)
  51.             res[i][j] = rand() % 256;
  52.  
  53.     double Esum = 0, Eold, temp, lambda = 0.000000002, cdf[257], T = 0.00001;
  54.     int start, stop,s=0;
  55.  
  56.     for (int i = 0; i < image.rows; i++)
  57.         for (int j = 0; j < image.cols; j++)
  58.         {
  59.             Err[i][j] = (res[i][j] - pointer[i][j])*(res[i][j] - pointer[i][j])*Des[i][j];
  60.             if (i + 1 < image.rows) Err[i][j] += lambda*(res[i + 1][j] - res[i][j])*(res[i + 1][j] - res[i][j]);
  61.             if (j + 1 < image.cols) Err[i][j] += lambda*(res[i][j + 1] - res[i][j])*(res[i][j + 1] - res[i][j]);
  62.             Esum += Err[i][j];
  63.         }
  64.  
  65.     int n = 0;
  66.     do
  67.     {
  68.         string cd = "../resoult/image" + to_string(n) + ".jpg";
  69.         n++;
  70.         imwrite(cd, resoult);
  71.         printf("%lf\t%lf\n", Esum, T);
  72.         Eold = Esum;
  73.         for (int i = 0; i < image.rows; i++)
  74.             for (int j = 0; j < image.cols; j++)
  75.             {
  76.                 cdf[0] = 0;
  77.                 for (s = 0; s < 256; s++)
  78.                 {
  79.                     temp = (s - pointer[i][j])*(s - pointer[i][j])*Des[i][j];
  80.                     if (i + 1 < image.rows) temp += lambda*(res[i + 1][j] - s)*(res[i + 1][j] - s);
  81.                     if (j + 1 < image.cols) temp += lambda*(res[i][j + 1] - s)*(res[i][j + 1] - s);
  82.                     cdf[s] += pow(2.71828182, -(temp / T));
  83.                     cdf[s + 1] = cdf[s];
  84.                 }
  85.                 for (s = 0; s < 256; s++) cdf[s] /= cdf[255];
  86.                 temp = (rand() % 1000)/1000.0;
  87.  
  88.                 for (s = 0; s < 254 && cdf[s] < temp; s++);
  89.                 res[i][j] = s;
  90.             }
  91.  
  92.         Esum = 0;
  93.         for (int i = 0; i < image.rows - 1; i++)
  94.             for (int j = 0; j < image.cols - 1; j++)
  95.             {
  96.                 Err[i][j] = (res[i][j] - pointer[i][j])*(res[i][j] - pointer[i][j])*Des[i][j];
  97.                 if (i + 1 < image.rows) Err[i][j] += lambda*(res[i + 1][j] - res[i][j])*(res[i + 1][j] - res[i][j]);
  98.                 if (j + 1 < image.cols) Err[i][j] += lambda*(res[i][j + 1] - res[i][j])*(res[i][j + 1] - res[i][j]);
  99.                 Esum += Err[i][j];
  100.             }
  101.         T *= 0.9;
  102.     } while (Esum != Eold);
  103.  
  104.     imshow("Image2", resoult);
  105.     imshow("Image1", image);
  106.     waitKey(0);
  107.  
  108.     return 0;
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement