Advertisement
Guest User

Anna 2

a guest
Oct 6th, 2015
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.72 KB | None | 0 0
  1. #include <iostream>
  2. #include <opencv2/opencv.hpp>
  3. #include "opencv2/core/core.hpp"
  4. #include "opencv2/highgui/highgui.hpp"
  5. #include "opencv2/imgproc/imgproc.hpp"
  6. #include <math.h>
  7.  
  8. using namespace std;
  9. using namespace cv;
  10.  
  11. // calculating the conversion from RGB pixel values to the values of hue
  12.  
  13. float hue (int countR,int countG, int countB)
  14. {
  15. float h = 0.5*((countR - countG) + (countR - countB)) / sqrt(((countR - countG)*(countR - countG)) + ((countR - countB)*(countG - countB)));
  16.  
  17.  
  18. if (countG >= countB) {
  19. h= acos(h);
  20. }
  21. else {
  22. h = 2* 3.14 - acos(h);
  23. }
  24. float hue= saturate_cast<uchar>(h);
  25. return hue;
  26. }
  27. // calculating the conversion from RGB pixel values to the values of saturation
  28. float sat (int countR,int countG, int countB)
  29. {
  30. int totalcolor = countR + countG + countB;
  31. float s = 0;
  32. if (countR == countG && countG == countB)
  33. {
  34. s = 0;
  35. }
  36. else
  37. {
  38. if(countR <= countG && countR <= countB)
  39. {
  40. s = 1 - (3 * (countR / totalcolor));
  41. }
  42.  
  43. if (countG <= countR && countG <= countB)
  44. {
  45. s = 1 - (3 * (countG / totalcolor));
  46. }
  47.  
  48. if (countB <= countR && countB <= countG)
  49. {
  50. s = 1 - (3 * (countB / totalcolor));
  51. }
  52. }
  53. float sat= saturate_cast<uchar>(s);
  54. return sat;
  55.  
  56. }
  57.  
  58. // calculating the conversion from RGB pixel values to the values of intensity
  59.  
  60. float intns (int countR,int countG, int countB)
  61. {
  62. float i = 0;
  63. i = ((countR + countG + countB) / 3);
  64. return i;
  65. }
  66.  
  67. int main()
  68. {
  69. Mat image;
  70.  
  71. image = imread("333.jpg", 1);
  72.  
  73. Mat hueImage=image.clone();
  74. Mat satImage=image.clone();
  75. Mat intnsImage=image.clone();
  76.  
  77.  
  78. hueImage = Mat::ones(image.rows, image.cols, CV_8UC1);
  79. satImage = Mat::ones(image.rows, image.cols, CV_8UC1);
  80. intnsImage = Mat::ones(image.rows, image.cols, CV_8UC1);
  81.  
  82. if (image.data && !image.empty()){
  83. imshow("Original image", image);
  84.  
  85. //getting RGB values from every pixel
  86.  
  87. int countR = 0, countG = 0, countB = 0;
  88.  
  89. for (int y = 0; y < image.rows; ++y){
  90. for (int x = 0; x < image.cols; ++x){
  91. countB = image.at<Vec3b>(y, x)[0];
  92. countG = image.at<Vec3b>(y, x)[1];
  93. countR = image.at<Vec3b>(y, x)[2];
  94.  
  95. //normalizing RGB values ?
  96. //countR=countR/(countR+countG+countB);
  97. //countG=countG/(countR+countG+countB);
  98. //countB=countB/(countR+countG+countB);
  99.  
  100.  
  101. intnsImage.at<unsigned char>(y, x) = intns(countR,countG,countB);
  102. satImage.at<unsigned char>(y, x) = (sat(countR,countG,countB));
  103. hueImage.at<unsigned char>(y, x) = (hue(countR,countG,countB));
  104.  
  105. }
  106. }
  107.  
  108. //displaying images
  109.  
  110. imshow("Hue", hueImage);
  111. imshow("Saturation", satImage);
  112. imshow("Intensity", intnsImage);
  113.  
  114. }
  115. waitKey(0);
  116. return(0);
  117.  
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement