I'm loading a Tiff and running calcHist in python and c++, but I can't get the results to match. Code and output attached.
Thank you!
**Python Implementation:**
import numpy as np
import cv2 as cv
from PIL import Image
import matplotlib.pyplot as plt
im = Image.open('data.tif')
img = np.array(im).astype(np.uint16)
hist = cv.calcHist([img],[0],None,[256],[0,334])
print(hist)
plt.hist(img.ravel(),255,range=(0,1305)); plt.show()
**C++:**
uint16* raster = new uint16[width*length];
for (uint16 y = 0; y < length; ++y) {
for (uint16 x = 0; x < width; ++x) {
raster[y*width + x] = rasterYX[y][x];
}
}
cv::Mat flattened(length, width, CV_16UC1, &raster[0]);
cv::Mat inputHist;
cv::Mat hist;
float range[] = {0,334};
int num_bins = 256;
const float* ranges[] = {range};
cv::calcHist(&flattened, 1, 0, cv::Mat(), hist, 1,&num_bins, ranges, true, false);
std::cout << "Hist = " << std::endl << cv::format(hist, cv::Formatter::FMT_PYTHON);
Here is part of the c++ output followed by the python output at the point where they begin to diverge.
0,
1,
2,
1,
4,
4,
2,
1,
11,
6,
16,
38,
[0.000000e+00]
[1.000000e+00]
[2.000000e+00]
[1.000000e+00]
[5.000000e+00]
[3.000000e+00]
[2.000000e+00]
[6.000000e+00]
[6.000000e+00]
[6.000000e+00]
[3.100000e+01]
[2.300000e+01]
[3.600000e+01]
[2.010000e+02]
[2.160000e+02]
[4.350000e+02]
[9.560000e+02]
↧