Advertisement
Guest User

Untitled

a guest
Apr 26th, 2018
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.17 KB | None | 0 0
  1. // OpenCVApplication.cpp : Defines the entry point for the console application.
  2. // Laborator PI, Nagy Imola, gr 30234
  3.  
  4. #include "stdafx.h"
  5. #include "common.h"
  6. #include <queue>
  7. #include <random>
  8. #include <vector>
  9.  
  10.  
  11. /* Histogram display function - display a histogram using bars (simlilar to L3 / PI)
  12. Input:
  13. name - destination (output) window name
  14. hist - pointer to the vector containing the histogram values
  15. hist_cols - no. of bins (elements) in the histogram = histogram image width
  16. hist_height - height of the histogram image
  17. Call example:
  18. showHistogram ("MyHist", hist_dir, 255, 200);
  19. */
  20. void showHistogram(const string& name, int* hist, const int hist_cols, const int hist_height)
  21. {
  22. Mat imgHist(hist_height, hist_cols, CV_8UC3, CV_RGB(255, 255, 255)); // constructs a white image
  23.  
  24. //computes histogram maximum
  25. int max_hist = 0;
  26. for (int i = 0; i<hist_cols; i++)
  27. if (hist[i] > max_hist)
  28. max_hist = hist[i];
  29. double scale = 1.0;
  30. scale = (double)hist_height / max_hist;
  31. int baseline = hist_height - 1;
  32.  
  33. for (int x = 0; x < hist_cols; x++) {
  34. Point p1 = Point(x, baseline);
  35. Point p2 = Point(x, baseline - cvRound(hist[x] * scale));
  36. line(imgHist, p1, p2, CV_RGB(255, 0, 255)); // histogram bins colored in magenta
  37. }
  38.  
  39. imshow(name, imgHist);
  40. }
  41.  
  42. Mat rgb_to_yuv(Mat img) {
  43.  
  44. Vec3b color;
  45. Mat yuv = Mat(img.size(), CV_8UC3);
  46.  
  47. for (int i = 0; i < img.rows; i++) {
  48. for (int j = 0; j < img.cols; j++) {
  49. color = img.at<Vec3b>(i, j); //bgr
  50. yuv.at<Vec3b>(i, j)[0] = color[2] * .299000 + color[1] * .587000 + color[0] * .114000;
  51. yuv.at<Vec3b>(i, j)[1] = color[2] * -.168736 + color[1] * -.331264 + color[0] * .500000 + 128;
  52. yuv.at<Vec3b>(i, j)[2] = color[2] * .500000 + color[1] * -.418688 + color[0] * -.081312 + 128;
  53. }
  54. }
  55. return yuv;
  56. }
  57.  
  58. Mat normalize_yuv(Mat img) {
  59.  
  60. Mat yuv_normalized = Mat(img.size(), CV_8UC3);
  61. yuv_normalized = img.clone();
  62. for (int i = 0; i < img.rows; i++) {
  63. for (int j = 0; j < img.cols; j++) {
  64.  
  65. yuv_normalized.at<Vec3b>(i, j)[0] = 128;
  66.  
  67. float k;
  68. if (img.at<Vec3b>(i, j)[0] > 128) {
  69. k = yuv_normalized.at<Vec3b>(i, j)[0] / img.at<Vec3b>(i, j)[0];
  70. }
  71. else if (img.at<Vec3b>(i, j)[0] < 128) {
  72. k = (255 - img.at<Vec3b>(i, j)[0] )/ yuv_normalized.at<Vec3b>(i, j)[0];
  73. }
  74. else {
  75. k = 1;
  76. }
  77. yuv_normalized.at<Vec3b>(i, j)[1] = min((img.at<Vec3b>(i, j)[1] - 128) * k + 128, 255);
  78. yuv_normalized.at<Vec3b>(i, j)[2] = min((img.at<Vec3b>(i, j)[2] - 128) * k + 128, 255);
  79. }
  80. }
  81. return yuv_normalized;
  82. }
  83.  
  84. void test() {
  85. char fname[MAX_PATH];
  86. while (openFileDlg(fname)) {
  87. Mat img = imread(fname, CV_LOAD_IMAGE_UNCHANGED);
  88.  
  89. Mat yuv = rgb_to_yuv(img);
  90. Mat yuv_norm = normalize_yuv(yuv);
  91.  
  92. Mat u = Mat(yuv_norm.rows, yuv_norm.cols, CV_8UC1);
  93. Mat v = Mat(yuv_norm.rows, yuv_norm.cols, CV_8UC1);
  94.  
  95. for (int i = 0; i < u.rows; i++) {
  96. for (int j = 0; j < u.cols; j++)
  97. {
  98. u.at<uchar>(i, j) = yuv_norm.at<Vec3b>(i, j)[1];
  99. v.at<uchar>(i, j) = yuv_norm.at<Vec3b>(i, j)[2];
  100. }
  101. }
  102.  
  103.  
  104. imshow("original", img);
  105. imshow("mod", yuv_norm);
  106. imshow("u", u);
  107. imshow("v", v);
  108. waitKey(0);
  109. }
  110.  
  111. }
  112.  
  113. int main()
  114. {
  115. test();
  116. return 0;
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement