dariahinz

Untitled

Oct 27th, 2017
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.83 KB | None | 0 0
  1. #include "opencv2/opencv.hpp"
  2. using namespace cv;
  3. using namespace std;
  4.  
  5. Mat grayImage;
  6.  
  7. int threshold_value = 100;
  8. const int max_value = 255;
  9. Mat binarizedImage;
  10.  
  11. void Threshold(int pos, void* userdata)
  12. {
  13. threshold(grayImage, binarizedImage, threshold_value, max_value, THRESH_BINARY);
  14. imshow("Binarization", binarizedImage);
  15.  
  16. }
  17.  
  18. int main()
  19. {
  20. Mat srcImage;
  21. srcImage = imread("Samples/ryba.jpg", 1);
  22.  
  23. if (!srcImage.data)
  24. {
  25. std::cout << "Blad odczytu obrazu!" << endl;
  26. return 1;
  27. }
  28.  
  29. namedWindow("Source image");
  30. moveWindow("Source image", 0, 0);
  31. imshow("Source image", srcImage);
  32.  
  33.  
  34. //obraz achromatyczny
  35. cvtColor(srcImage, grayImage, COLOR_BGR2GRAY);
  36. namedWindow("Gray image");
  37. moveWindow("Gray image", 300, 0);
  38. imshow("Gray image", grayImage);
  39. imwrite("Samples/Gray image.jpg", grayImage);
  40.  
  41. //obraz skalowany
  42. Mat resizedImage(100, 100, srcImage.type());
  43. resize(srcImage, resizedImage, resizedImage.size());
  44. namedWindow("Resized image");
  45. moveWindow("Resized image", 600, 0);
  46. imshow("Resized image", resizedImage);
  47.  
  48. //filtr dolno
  49. Mat blurImage;
  50. blur(srcImage, blurImage, Size(5, 5));
  51. namedWindow("Blur image");
  52. moveWindow("Blur image", 900, 0);
  53. imshow("Blur image", blurImage);
  54.  
  55. //filtr gorno
  56. Mat CannyImage;
  57. Canny(srcImage, CannyImage, 90, 90);
  58. namedWindow("Canny Edges");
  59. moveWindow("Canny Edges", 1200, 0);
  60. imshow("Canny Edges", CannyImage);
  61.  
  62. //gorno 2
  63.  
  64. Mat LaplacianImage;
  65. Laplacian(grayImage, LaplacianImage, CV_16S, 3);
  66. Mat scaledLaplacianImage;
  67. convertScaleAbs(LaplacianImage, scaledLaplacianImage);
  68. namedWindow("Laplacian Image");
  69. moveWindow("Laplacian Image", 0, 300);
  70. imshow("Laplacian Image", scaledLaplacianImage);
  71.  
  72. //2a
  73.  
  74. const int histSize = 256;
  75. const int hist_w = 256;
  76. const int hist_h = 256;
  77. float range[2] = { 0, 256 };
  78. const float* histRange = range;
  79. Mat histImageGray(Size(hist_w, hist_h), CV_8UC3, Scalar(0, 0, 0));
  80. Mat histogramGray;
  81. calcHist(&grayImage, 1, 0, Mat(), histogramGray, 1, &histSize, &histRange);
  82. normalize(histogramGray, histogramGray, range[0], range[1], NORM_MINMAX);
  83. for (int i = 0; i < 256; i++)
  84. line(histImageGray, Point(i, hist_h), Point(i, hist_h - histogramGray.at<float>(i)), Scalar(255, 0, 0), 2);
  85.  
  86. namedWindow("Histogram Gray");
  87. moveWindow("Histogram Gray", 300, 300);
  88. imshow("Histogram Gray", histImageGray);
  89.  
  90. //2b
  91. Mat equalizedHistImage;
  92. equalizeHist(grayImage, equalizedHistImage);
  93. namedWindow("Equalized Histogram");
  94. moveWindow("Equalized Histogram", 600, 300);
  95. imshow("Equalized Histogram", equalizedHistImage);
  96.  
  97. //2c
  98.  
  99. Mat hist2(Size(hist_w, hist_h), CV_8UC3, Scalar(0, 0, 0));
  100. Mat histogram2;
  101. calcHist(&equalizedHistImage, 1, 0, Mat(), histogram2, 1, &histSize, &histRange);
  102. normalize(histogram2, histogram2, range[0], range[1], NORM_MINMAX);
  103. for (int i = 0; i < 256; i++)
  104. line(hist2, Point(i, hist_h), Point(i, hist_h - histogram2.at<float>(i)), Scalar(255, 0, 0), 1);
  105.  
  106. namedWindow("Histogram Equalized");
  107. moveWindow("Histogram Equalized", 900, 300);
  108. imshow("Histogram Equalized", hist2);
  109.  
  110.  
  111. //3a
  112. Mat brightImage;
  113. srcImage.copyTo(brightImage);
  114.  
  115. for (int i = 0; i < brightImage.rows; i++)
  116. {
  117. for (int j = 0; j < brightImage.cols; j++)
  118. {
  119. Vec3b pixelColor;
  120. pixelColor = brightImage.at<Vec3b>(Point(j, i));
  121.  
  122. for (int k = 0; k < 3; k++)
  123. {
  124. if (pixelColor[k] + 100 > 255)
  125. pixelColor[k] = 255;
  126. else
  127. pixelColor[k] += 100;
  128. brightImage.at<Vec3b>(Point(j, i)) = pixelColor;
  129. }
  130.  
  131. }
  132. }
  133.  
  134. //4
  135.  
  136. namedWindow("Binarization");
  137. moveWindow("Binarization", 300, 600);
  138. createTrackbar("Threshold value", "Binarization", &threshold_value, max_value, Threshold);
  139.  
  140.  
  141. namedWindow("Bright Image");
  142. moveWindow("Bright Image", 0, 600);
  143. imshow("Bright Image", brightImage);
  144.  
  145.  
  146.  
  147.  
  148.  
  149.  
  150. waitKey();
  151.  
  152. return( 0 ); // no error
  153. }
Add Comment
Please, Sign In to add comment