How To Create Heatmap Over An Image Using Coordinate Points?
Solution 1:
Clean your data generating the heatmap
First, if you are not comfortable with deep-nested data, you should clean your data in your CSV file (they are not uniform and have duplicates - they are also error-prone if you want rectangles).
The simplest example being the following:
x,y,x1,y1,Probabilityvalue0,0,5,10,0.550,45,55,65,0.9100,150,120,200,0.31000,1005,1005,1010,1
The answer below has been written with this clean CSV dataset in mind.
Use Pandas to process CSV data files
Seeing what your use case is, I recommend using pandas
in order to process your CSV data files.
You can store data from a CSV file in a pandas
DataFrame this way:
df = pd.read_csv("data.csv")
and iterate over rows, using the first row as keys for each column value, as following:
for index, rowin df.iterrows():
print(row["x"], row["y"], row["x1"], row["y1"],
row["Probability value"]
Full working snippet
This snippet is not very pretty but it works for the dummy dataset you've provided and is intended to be pretty self-explanatory with the above introduction. Some tweaking might be needed, especially for the plotting part.
#!/usr/bin/python3import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from skimage import io
from skimage.color import rgb2gray
import matplotlib as mpl
# Read original image
img = io.imread('img.jpg')
# Get the dimensions of the original image
x_dim, y_dim, z_dim = np.shape(img)
# Create heatmap
heatmap = np.zeros((x_dim, y_dim), dtype=float)
# Read CSV with a Pandas DataFrame
df = pd.read_csv("data.csv")
# Set probabilities values to specific indexes in the heatmapfor index, row in df.iterrows():
x = np.int(row["x"])
y = np.int(row["y"])
x1 = np.int(row["x1"])
y1 = np.int(row["y1"])
p = row["Probability value"]
heatmap[x:x1,y:y1] = p
# Plot images
fig, axes = plt.subplots(1, 2, figsize=(8, 4))
ax = axes.ravel()
ax[0].imshow(img)
ax[0].set_title("Original")
fig.colorbar(ax[0].imshow(img), ax=ax[0])
ax[1].imshow(img, vmin=0, vmax=1)
ax[1].imshow(heatmap, alpha=.5, cmap='jet')
ax[1].set_title("Original + heatmap")
# Specific colorbar
norm = mpl.colors.Normalize(vmin=0,vmax=2)
N = 11
cmap = plt.get_cmap('jet',N)
sm = plt.cm.ScalarMappable(cmap=cmap, norm=norm)
sm.set_array([])
plt.colorbar(sm, ticks=np.linspace(0,1,N),
boundaries=np.arange(0,1.1,0.1))
fig.tight_layout()
plt.show()
Post a Comment for "How To Create Heatmap Over An Image Using Coordinate Points?"