Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int* computeHistogram(char* fname) {
- int* h = (int*)calloc(256, sizeof(int));
- Mat src = imread(fname, CV_LOAD_IMAGE_GRAYSCALE);
- for (int i = 0; i < src.rows; i++) {
- for (int j = 0; j < src.cols; j++) {
- h[src.at<uchar>(i, j)]++;
- //*(h + src.at<uchar>(i, j)) += 1;
- }
- }
- return h;
- }
- //historgram
- void createHistogram() {
- char fname[MAX_PATH];
- Mat src;
- Mat dst;
- int h[256] = { 0 };
- if (openFileDlg(fname)) {
- src = imread(fname, CV_LOAD_IMAGE_GRAYSCALE);
- int height = src.rows;
- int width = src.cols;
- int level;
- //dst = Mat(height, width, CV_8UC1);
- for (int i = 0; i < height; i++) {
- for (int j = 0; j < width; j++) {
- level = src.at<uchar>(i, j);
- h[level]++;
- }
- }
- }
- imshow("image", src);
- showHistogram("Histogram", h, 256, 256);
- waitKey();
- }
- void showHistogram(const std::string& name, int* hist, const int hist_cols, const int hist_height)
- {
- Mat imgHist(hist_height, hist_cols, CV_8UC3, CV_RGB(255, 255, 255)); // constructs a white image
- //computes histogram maximum
- int max_hist = 0;
- for (int i = 0; i < hist_cols; i++)
- if (hist[i] > max_hist)
- max_hist = hist[i];
- double scale = 1.0;
- scale = (double)hist_height / max_hist;
- int baseline = hist_height - 1;
- for (int x = 0; x < hist_cols; x++) {
- Point p1 = Point(x, baseline);
- Point p2 = Point(x, baseline - cvRound(hist[x] * scale));
- line(imgHist, p1, p2, CV_RGB(255, 0, 255)); // histogram bins colored in magenta
- }
- imshow(name, imgHist);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement