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 <string>
- #include <cmath>
- #include <conio.h>
- using namespace std;
- using namespace cv;
- long int average(int st, int en, long int *histo) //function หาค่าเฉลี่ยตรงกลางของ histogram จาก 'st' ถึง 'en'
- {
- long int n = 0, sum = 0;
- for (int i = st; i <= en; i++)
- {
- n += histo[i]; //นับจำนวนข้อมูล
- sum += histo[i] * i; //หาผลรวมข้อมมูลโดยเอาขนาดข้อมูลคูณกับจำนวณข้อมูล
- }
- return sum / n; //return ค่าเฉลี่ย
- }
- int quiz1() //main function
- {
- //ข้อ1
- Mat image = imread("../image/image.jpg", IMREAD_GRAYSCALE);
- imshow("Image", image);
- uchar* data = image.data;
- long int histo[256] = { 0 }, maxhisto = 0;
- for (int i = 0; i < image.rows*image.cols; i++) //นับจำนวน data ลงไปใน histogram
- histo[(int)data[i]]++;
- for (int i = 0; i < 256; i++) //หาค่าสูงสุดของ histogram
- if (histo[i] > maxhisto) maxhisto = histo[i];
- for (int i = 0; i < 256; i++) //scale ค่า histogram ให้อยู่ในช่วง 0 ถึง 255
- histo[i] = histo[i] * 300 / maxhisto;
- Mat histopic(300, 256, CV_8UC1, Scalar(255));
- for (int i = 1; i <= 300; i++) //นำข้อมูลใน histrogram มาสร้างเป็นภาพกราฟ
- {
- uchar* pt = histopic.ptr<uchar>(300 - i);
- for (int j = 0; j < 256; j++, pt++)
- if (histo[j] >= i)
- *pt = 0;
- }
- imshow("Histogram", histopic);
- waitKey(0);
- /*##############################################################################*/
- //ข้อ2
- Mat image2 = image.clone();
- long int T, u1, u2;
- T = average(0, 255, histo); //หาค่าเฉลี่ยกึ่งกลาง threshold ของ histogram จาก 0 ถึง 255
- while (1)
- {
- u1 = average(0, T, histo); //หาค่า thresholdใหม่ ของ histogram จาก 0 ถึง thresholdเดิม
- u2 = average(T + 1, 255, histo); //หา thresholdใหม่ ของ histogram จาก thresholdเดิม ถึง 255
- if ((u1 + u2) / 2 == T) //ถ้าค่า thresholdใหม่ เท่ากันกับ thresholdเดิม ให้หยุดหา
- break;
- T = (u1 + u2) / 2; //เก็บค่า threshold ไปใช้หาอีกรอบ
- }
- for (int i = 0; i < image2.rows; i++) //ใช้ค่า threshold ที่หาได้มาแบ่งรูปให้เหลือแค่สีขาวกับดำ
- {
- uchar* pt = image2.ptr<uchar>(i);
- for (int j = 0; j < image2.cols; j++, pt++)
- if (*pt < T)
- *pt = 0;
- else
- *pt = 255;
- }
- imshow("Image2", image2);
- waitKey(0);
- destroyAllWindows();
- /*##############################################################################*/
- //ข้อ3
- Mat image3_1, image3_2;
- medianBlur(image, image3_1, 5);
- medianBlur(image, image3_2, 11);
- imshow("Image3_1", image3_1);
- imshow("Image3_2", image3_2);
- waitKey(0);
- destroyAllWindows();
- /*##############################################################################*/
- //ข้อ4
- Mat image4_1, image4_2;
- GaussianBlur(image, image4_1, Size(7, 7), 2, 2);
- GaussianBlur(image, image4_2, Size(13, 13), 2, 2);
- imshow("Image4_1", image4_1);
- imshow("Image4_2", image4_2);
- waitKey(0);
- destroyAllWindows();
- /*##############################################################################*/
- //ข้อ5
- double G[13][13], c = 0;
- int sigma = 2, n = 6;
- for (int i = -n; i <= n; i++) //คำนวนค่า g(i,j) ใส่ matrix of Gaussian
- for (int j = -n; j <= n; j++)
- G[i + n][j + n] = exp(-(i*i + j*j) / (2. * sigma*sigma));
- for (int i = -6; i <= 6; i++)
- for (int j = -6; j <= 6; j++)
- {
- G[i + n][j + n] = G[i + n][j + n] / G[n + n][n + n]; //คำนวน g(i,j) / g(n,n)
- c += G[i + n][j + n]; //หาผลรวมค่าทั้งหมดใน matrix (ค่า c)
- }
- for (int i = -n; i <= n; i++) //โชว์ค่าใน matrix ออกมาดู
- {
- for (int j = -n; j <= n; j++)
- cout << (int)G[i + n][j + n] << "\t";
- cout << endl;
- }
- Mat filter(13, 13, CV_8UC1, Scalar(255));
- for (int i = 0; i < 13; i++) //นำค่าใน matrix มาสร้างเป็นรูป
- {
- uchar* pt = filter.ptr<uchar>(i);
- for (int j = 0; j < 13; j++, pt++)
- *pt = G[i][j] / G[n][n] * 255; //scale ค่าใน matrix ให้อยู่ในช่วง 0 ถึง 255
- }
- namedWindow("filter", WINDOW_NORMAL);
- resizeWindow("filter", 600, 600);
- imshow("filter", filter);
- Mat image5 = image.clone();
- uchar *pt1[1000], *pt2[1000];
- double sum;
- for (int i = 0; i < image5.rows; i++)
- {
- pt1[i] = image.ptr<uchar>(i); //เอา poiter pt1 ไปชี้รูป image
- pt2[i] = image5.ptr<uchar>(i); // เอา poiter pt2 ไปชี้รูป image5
- }
- for (int i = 0; i < image5.rows; i++) //เอา filter ที่ได้มาเบลอภาพ
- for (int j = 0; j < image5.cols; j++, pt2[i]++)
- {
- sum = 0;
- for (int k = -n; k <= n; k++) //convolution โดยไม่ต้องไม่ต้องทำการ mirror ภาพ แต่กำหนดเงื่อนไขให้พิกเซลที่อยู่นอกภาพแทน
- for (int l = -n; l <= n; l++)
- if (i + k < 0)
- if (j + l < 0) //ถ้ากำลังพิจารณาบริเวณนอกภาพด้านซ้ายบน ให้ใช้ค่าจากพิกเซลซ้ายบนในภาพ
- sum += (double)pt1[0][0] * G[k + n][l + n];
- else if (j + l >= image.cols) //ถ้ากำลังพิจารณาบริเวณนอกภาพด้านขวาบน ให้ใช้ค่าจากพิกเซลขวาบนในภาพ
- sum += (double)pt1[0][image.cols - 1] * G[k + n][l + n];
- else //ถ้ากำลังพิจารณาบริเวณนอกภาพด้านบน ให้ใช้ค่าจากพิกเซลบนสุดในภาพ
- sum += (double)pt1[0][j + l] * G[k + n][l + n];
- else if (i + k >= image.rows)
- if (j + l < 0) //ถ้ากำลังพิจารณาบริเวณนอกภาพด้านซ้ายล่าง ให้ใช้ค่าจากพิกเซลซ้ายล้างในภาพ
- sum += (double)pt1[image.rows - 1][0] * G[k + n][l + n];
- else if (j + l >= image.cols) //ถ้ากำลังพิจารณาบริเวณนอกภาพด้านขวาล่าง ให้ใช้ค่าจากพิกเซลขวาล่างในภาพ
- sum += (double)pt1[image.rows - 1][image.cols - 1] * G[k + n][l + n];
- else //ถ้ากำลังพิจารณาบริเวณนอกภาพด้านล่าง ให้ใช้ค่าจากพิกเซลล่างสุดในภาพ
- sum += (double)pt1[image.rows - 1][j + l] * G[k + n][l + n];
- else
- if (j + l < 0) //ถ้ากำลังพิจารณาบริเวณนอกภาพด้านซ้าย ให้ใช้ค่าจากพิกเซลซ้ายสุดในภาพ
- sum += (double)pt1[i + k][0] * G[k + n][l + n];
- else if (j + l >= image.cols) //ถ้ากำลังพิจารณาบริเวณนอกภาพด้านขวา ให้ใช้ค่าจากพิกเซลขวาสุดในภาพ
- sum += (double)pt1[i + k][image.cols - 1] * G[k + n][l + n];
- else //ถ้ากำลังพิจารณาบริเวณในภาพก็ให้ใช้ค่าจากพิกเซลนั้นเลย
- sum += (double)pt1[i + k][j + l] * G[k + n][l + n];
- *pt2[i] = (uchar)(sum / c); //เอาค่าผลรวมที่ได้มาหารค่า c ตามสูตรการเบลอ
- }
- imshow("Image5", image5);
- waitKey(0);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement