Advertisement
Guest User

Untitled

a guest
May 23rd, 2019
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.50 KB | None | 0 0
  1. int* computeHistogram(char* fname) {
  2. int* h = (int*)calloc(256, sizeof(int));
  3. Mat src = imread(fname, CV_LOAD_IMAGE_GRAYSCALE);
  4. for (int i = 0; i < src.rows; i++) {
  5. for (int j = 0; j < src.cols; j++) {
  6. h[src.at<uchar>(i, j)]++;
  7. //*(h + src.at<uchar>(i, j)) += 1;
  8. }
  9. }
  10. return h;
  11. }
  12. //historgram
  13. void createHistogram() {
  14. char fname[MAX_PATH];
  15. Mat src;
  16. Mat dst;
  17. int h[256] = { 0 };
  18. if (openFileDlg(fname)) {
  19. src = imread(fname, CV_LOAD_IMAGE_GRAYSCALE);
  20. int height = src.rows;
  21. int width = src.cols;
  22. int level;
  23. //dst = Mat(height, width, CV_8UC1);
  24. for (int i = 0; i < height; i++) {
  25. for (int j = 0; j < width; j++) {
  26. level = src.at<uchar>(i, j);
  27. h[level]++;
  28. }
  29. }
  30. }
  31. imshow("image", src);
  32. showHistogram("Histogram", h, 256, 256);
  33. waitKey();
  34. }
  35. void showHistogram(const std::string& name, int* hist, const int hist_cols, const int hist_height)
  36. {
  37. Mat imgHist(hist_height, hist_cols, CV_8UC3, CV_RGB(255, 255, 255)); // constructs a white image
  38.  
  39. //computes histogram maximum
  40. int max_hist = 0;
  41. for (int i = 0; i < hist_cols; i++)
  42. if (hist[i] > max_hist)
  43. max_hist = hist[i];
  44. double scale = 1.0;
  45. scale = (double)hist_height / max_hist;
  46. int baseline = hist_height - 1;
  47.  
  48. for (int x = 0; x < hist_cols; x++) {
  49. Point p1 = Point(x, baseline);
  50. Point p2 = Point(x, baseline - cvRound(hist[x] * scale));
  51. line(imgHist, p1, p2, CV_RGB(255, 0, 255)); // histogram bins colored in magenta
  52. }
  53.  
  54. imshow(name, imgHist);
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement