Advertisement
Guest User

Untitled

a guest
Jan 20th, 2017
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.37 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 <string>
  7. #include <cmath>
  8. #include <conio.h>
  9.  
  10. using namespace std;
  11. using namespace cv;
  12.  
  13. long int average(int st, int en, long int *histo) //function หาค่าเฉลี่ยตรงกลางของ histogram จาก 'st' ถึง 'en'
  14. {
  15.     long int n = 0, sum = 0;
  16.     for (int i = st; i <= en; i++)
  17.     {
  18.         n += histo[i]; //นับจำนวนข้อมูล
  19.         sum += histo[i] * i; //หาผลรวมข้อมมูลโดยเอาขนาดข้อมูลคูณกับจำนวณข้อมูล
  20.     }
  21.     return sum / n; //return ค่าเฉลี่ย
  22. }
  23.  
  24. int quiz1() //main function
  25. {
  26.     //ข้อ1
  27.  
  28.     Mat image = imread("../image/image.jpg", IMREAD_GRAYSCALE);
  29.     imshow("Image", image);
  30.  
  31.     uchar* data = image.data;
  32.     long int histo[256] = { 0 }, maxhisto = 0;
  33.  
  34.     for (int i = 0; i < image.rows*image.cols; i++) //นับจำนวน data ลงไปใน histogram
  35.         histo[(int)data[i]]++;
  36.  
  37.     for (int i = 0; i < 256; i++) //หาค่าสูงสุดของ histogram
  38.         if (histo[i] > maxhisto) maxhisto = histo[i];
  39.  
  40.     for (int i = 0; i < 256; i++) //scale ค่า histogram ให้อยู่ในช่วง 0 ถึง 255
  41.         histo[i] = histo[i] * 300 / maxhisto;
  42.  
  43.     Mat histopic(300, 256, CV_8UC1, Scalar(255));
  44.     for (int i = 1; i <= 300; i++) //นำข้อมูลใน histrogram มาสร้างเป็นภาพกราฟ
  45.     {
  46.         uchar* pt = histopic.ptr<uchar>(300 - i);
  47.         for (int j = 0; j < 256; j++, pt++)
  48.             if (histo[j] >= i)
  49.                 *pt = 0;
  50.     }
  51.     imshow("Histogram", histopic);
  52.  
  53.     waitKey(0);
  54.     /*##############################################################################*/
  55.  
  56.     //ข้อ2
  57.  
  58.     Mat image2 = image.clone();
  59.     long int T, u1, u2;
  60.     T = average(0, 255, histo); //หาค่าเฉลี่ยกึ่งกลาง threshold ของ histogram จาก 0 ถึง 255
  61.     while (1)
  62.     {
  63.         u1 = average(0, T, histo); //หาค่า thresholdใหม่ ของ histogram จาก 0 ถึง thresholdเดิม
  64.         u2 = average(T + 1, 255, histo); //หา thresholdใหม่ ของ histogram จาก thresholdเดิม ถึง 255
  65.         if ((u1 + u2) / 2 == T) //ถ้าค่า thresholdใหม่ เท่ากันกับ thresholdเดิม ให้หยุดหา
  66.             break;
  67.         T = (u1 + u2) / 2; //เก็บค่า threshold ไปใช้หาอีกรอบ
  68.     }
  69.  
  70.     for (int i = 0; i < image2.rows; i++) //ใช้ค่า threshold ที่หาได้มาแบ่งรูปให้เหลือแค่สีขาวกับดำ
  71.     {
  72.         uchar* pt = image2.ptr<uchar>(i);
  73.         for (int j = 0; j < image2.cols; j++, pt++)
  74.             if (*pt < T)
  75.                 *pt = 0;
  76.             else
  77.                 *pt = 255;
  78.     }
  79.     imshow("Image2", image2);
  80.  
  81.     waitKey(0);
  82.     destroyAllWindows();
  83.     /*##############################################################################*/
  84.  
  85.     //ข้อ3
  86.  
  87.     Mat image3_1, image3_2;
  88.     medianBlur(image, image3_1, 5);
  89.     medianBlur(image, image3_2, 11);
  90.     imshow("Image3_1", image3_1);
  91.     imshow("Image3_2", image3_2);
  92.  
  93.     waitKey(0);
  94.     destroyAllWindows();
  95.     /*##############################################################################*/
  96.  
  97.     //ข้อ4
  98.  
  99.     Mat image4_1, image4_2;
  100.     GaussianBlur(image, image4_1, Size(7, 7), 2, 2);
  101.     GaussianBlur(image, image4_2, Size(13, 13), 2, 2);
  102.     imshow("Image4_1", image4_1);
  103.     imshow("Image4_2", image4_2);
  104.  
  105.     waitKey(0);
  106.     destroyAllWindows();
  107.     /*##############################################################################*/
  108.  
  109.     //ข้อ5
  110.  
  111.     double G[13][13], c = 0;
  112.     int sigma = 2, n = 6;
  113.     for (int i = -n; i <= n; i++) //คำนวนค่า g(i,j) ใส่ matrix of Gaussian
  114.         for (int j = -n; j <= n; j++)
  115.             G[i + n][j + n] = exp(-(i*i + j*j) / (2. * sigma*sigma));
  116.  
  117.     for (int i = -6; i <= 6; i++)
  118.         for (int j = -6; j <= 6; j++)
  119.         {
  120.             G[i + n][j + n] = G[i + n][j + n] / G[n + n][n + n]; //คำนวน g(i,j) / g(n,n)
  121.             c += G[i + n][j + n]; //หาผลรวมค่าทั้งหมดใน matrix (ค่า c)
  122.         }
  123.  
  124.     for (int i = -n; i <= n; i++) //โชว์ค่าใน matrix ออกมาดู
  125.     {
  126.         for (int j = -n; j <= n; j++)
  127.             cout << (int)G[i + n][j + n] << "\t";
  128.         cout << endl;
  129.     }
  130.  
  131.     Mat filter(13, 13, CV_8UC1, Scalar(255));
  132.     for (int i = 0; i < 13; i++) //นำค่าใน matrix มาสร้างเป็นรูป
  133.     {
  134.         uchar* pt = filter.ptr<uchar>(i);
  135.         for (int j = 0; j < 13; j++, pt++)
  136.             *pt = G[i][j] / G[n][n] * 255; //scale ค่าใน matrix ให้อยู่ในช่วง 0 ถึง 255
  137.     }
  138.     namedWindow("filter", WINDOW_NORMAL);
  139.     resizeWindow("filter", 600, 600);
  140.     imshow("filter", filter);
  141.  
  142.  
  143.     Mat image5 = image.clone();
  144.     uchar *pt1[1000], *pt2[1000];
  145.     double sum;
  146.     for (int i = 0; i < image5.rows; i++)
  147.     {
  148.         pt1[i] = image.ptr<uchar>(i); //เอา poiter pt1 ไปชี้รูป image
  149.         pt2[i] = image5.ptr<uchar>(i); // เอา poiter pt2 ไปชี้รูป image5
  150.     }
  151.  
  152.     for (int i = 0; i < image5.rows; i++) //เอา filter ที่ได้มาเบลอภาพ
  153.         for (int j = 0; j < image5.cols; j++, pt2[i]++)
  154.         {
  155.             sum = 0;
  156.  
  157.             for (int k = -n; k <= n; k++) //convolution โดยไม่ต้องไม่ต้องทำการ mirror ภาพ แต่กำหนดเงื่อนไขให้พิกเซลที่อยู่นอกภาพแทน
  158.                 for (int l = -n; l <= n; l++)
  159.  
  160.                     if (i + k < 0)
  161.  
  162.                         if (j + l < 0) //ถ้ากำลังพิจารณาบริเวณนอกภาพด้านซ้ายบน ให้ใช้ค่าจากพิกเซลซ้ายบนในภาพ
  163.                             sum += (double)pt1[0][0] * G[k + n][l + n];
  164.                         else if (j + l >= image.cols) //ถ้ากำลังพิจารณาบริเวณนอกภาพด้านขวาบน ให้ใช้ค่าจากพิกเซลขวาบนในภาพ
  165.                             sum += (double)pt1[0][image.cols - 1] * G[k + n][l + n];
  166.                         else //ถ้ากำลังพิจารณาบริเวณนอกภาพด้านบน ให้ใช้ค่าจากพิกเซลบนสุดในภาพ
  167.                             sum += (double)pt1[0][j + l] * G[k + n][l + n];
  168.  
  169.                     else if (i + k >= image.rows)
  170.  
  171.                         if (j + l < 0) //ถ้ากำลังพิจารณาบริเวณนอกภาพด้านซ้ายล่าง ให้ใช้ค่าจากพิกเซลซ้ายล้างในภาพ
  172.                             sum += (double)pt1[image.rows - 1][0] * G[k + n][l + n];
  173.                         else if (j + l >= image.cols) //ถ้ากำลังพิจารณาบริเวณนอกภาพด้านขวาล่าง ให้ใช้ค่าจากพิกเซลขวาล่างในภาพ
  174.                             sum += (double)pt1[image.rows - 1][image.cols - 1] * G[k + n][l + n];
  175.                         else //ถ้ากำลังพิจารณาบริเวณนอกภาพด้านล่าง ให้ใช้ค่าจากพิกเซลล่างสุดในภาพ
  176.                             sum += (double)pt1[image.rows - 1][j + l] * G[k + n][l + n];
  177.  
  178.                     else
  179.  
  180.                         if (j + l < 0) //ถ้ากำลังพิจารณาบริเวณนอกภาพด้านซ้าย ให้ใช้ค่าจากพิกเซลซ้ายสุดในภาพ
  181.                             sum += (double)pt1[i + k][0] * G[k + n][l + n];
  182.                         else if (j + l >= image.cols) //ถ้ากำลังพิจารณาบริเวณนอกภาพด้านขวา ให้ใช้ค่าจากพิกเซลขวาสุดในภาพ
  183.                             sum += (double)pt1[i + k][image.cols - 1] * G[k + n][l + n];
  184.                         else //ถ้ากำลังพิจารณาบริเวณในภาพก็ให้ใช้ค่าจากพิกเซลนั้นเลย
  185.                             sum += (double)pt1[i + k][j + l] * G[k + n][l + n];
  186.  
  187.             *pt2[i] = (uchar)(sum / c); //เอาค่าผลรวมที่ได้มาหารค่า c ตามสูตรการเบลอ
  188.         }
  189.  
  190.     imshow("Image5", image5);
  191.     waitKey(0);
  192.     return 0;
  193. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement