Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2019
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.55 KB | None | 0 0
  1.  
  2. void bgrtohsv(){
  3. char fname[MAX_PATH];
  4. while (openFileDlg(fname))
  5. {
  6. Mat src = imread(fname);
  7. Mat hsv;
  8. Mat channels[3];
  9. int height = src.rows;
  10. int width = src.cols;
  11. Mat dstH = Mat(height, width, CV_8UC1);
  12. Mat dstS = Mat(height, width, CV_8UC1);
  13. Mat dstV = Mat(height, width, CV_8UC1);
  14.  
  15. cvtColor(src, hsv, CV_BGR2HSV);
  16.  
  17. split(hsv, channels);
  18.  
  19. dstH = channels[0] * (510/360);
  20. dstS = channels[1];
  21. dstV = channels[2];
  22.  
  23. imshow("h", dstH);
  24. imshow("v", dstV);
  25. imshow("s", dstS);
  26.  
  27. imshow("hsv", hsv);
  28. //waitKey();
  29.  
  30. int HistH[256] = { 0 };
  31.  
  32. for (int i = 0; i < dstH.rows; i++){
  33. for (int j = 0; j < dstH.cols; j++){
  34. uchar u = dstH.at<uchar>(i, j);
  35. HistH[u]++;
  36. }
  37. }
  38. int HistS[256] = { 0 };
  39.  
  40. for (int i = 0; i < dstS.rows; i++){
  41. for (int j = 0; j < dstS.cols; j++){
  42. uchar u = dstS.at<uchar>(i, j);
  43. HistS[u]++;
  44. }
  45. }
  46. int HistV[256] = { 0 };
  47.  
  48. for (int i = 0; i < dstV.rows; i++){
  49. for (int j = 0; j < dstV.cols; j++){
  50. uchar u = dstV.at<uchar>(i, j);
  51. HistV[u]++;
  52. }
  53. }
  54.  
  55. showHistogram("histh", HistH, 256, 256, true);
  56. showHistogram("hists", HistS, 256, 256, true);
  57. showHistogram("histv", HistV, 256, 256, true);
  58.  
  59. Mat binarizare= dstH.clone();
  60. for (int i = 0; i < dstH.rows; i++){
  61. for (int j = 0; j < dstH.cols; j++){
  62. float t = channels[0].at<uchar>(i,j);
  63. if (t < 30)
  64. binarizare.at<uchar>(i, j) = 255;
  65. else
  66. binarizare.at<uchar>(i, j) = 0;
  67. }
  68. }
  69.  
  70.  
  71. imshow("imgbinarizata", binarizare);
  72. waitKey();
  73. }
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement