Population density maps were drawn using PIL and CV2, respectively

Time:2021-7-28

The pred variable is a single channel density map of the network output.When drawing density map, it needs to be converted to numpy format supported by CPU.

Draw using pil

import matplotlib.pyplot as plt
plt.imshow(pred, cmap=plt.cm.jet)
plt.show()

This drawing method will have its own coordinate system, which will occupy the real pixels of the picture, resulting in the inconsistency between the size of the output density map and the size of the input picture. However, if you only view the density effect, this method is still recommended.

2. Draw with CV2

import cv2
heatmapshow = None
heatmapshow = cv2.normalize(pred, heatmapshow, alpha=0, beta=255, norm_type=cv2.NORM_MINMAX, dtype=cv2.CV_8U)
heatmapshow = cv2.applyColorMap(heatmapshow, cv2.COLORMAP_JET)
cv2.imshow("Heatmap", heatmapshow)
cv2.waitKey(0)

We know that the values in the pred matrix output by the network are very small, and they are floating-point numbers of np.float64, while CV2 deals with numbers in the [0255] interval. Therefore, if it is directly displayed or saved in CV2, it will be a dark graph.

Plt.imshow actually normalizes the pred and then applies color mapping to present the effect of thermal map.
So in CV2, you also need to do these two steps: normalization (CV2. Normalize) and color mapping (applycolormap)
The CMAP parameter in plt.imshow is equivalent to cv2.colormap in cv2.applycolormap_ Jet parameter that specifies the color.


reference resources:
https://blog.csdn.net/hxydip/…