Skip to content

Instantly share code, notes, and snippets.

@tuttelikz
Last active November 14, 2020 09:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tuttelikz/e1b7be9814991423f226ad787f0c223a to your computer and use it in GitHub Desktop.
Save tuttelikz/e1b7be9814991423f226ad787f0c223a to your computer and use it in GitHub Desktop.
Plots image profile, horizontal and vertical (full or from point to point)
def pltImgProfile(img_gray, pt1=None, pt2=None):
"""Plots image profile (x and y)
Parameters
----------
img_gray : np.array
image (grayscale)
pt1 : tuple
coordinates of first point (row1, col1)
pt2 : tuple
coordinates of second point (row2, col2)
Returns
-------
fig : matplotlib figure
projection of histograms
Requires
-------
numpy, matplotlib
Example
-------
# full profile
my_fig = pltImgProfile(img_gray, (0,0), (img_gray.shape[0],img_gray.shape[-1]))
# from point p1 to point p2
my_fig = pltImgProfile(img_gray, (50,50), (100,100))
"""
# horizontal
cols = np.arange(pt1[-1],pt2[-1])
hor_proj = np.sum(img_gray[:,pt1[-1]:pt2[-1]],axis = 0)
# vertical
rows = np.arange(pt1[0],pt2[0])
vert_proj = np.sum(img_gray[pt1[0]:pt2[0],:],axis = 1)
fig, axs = plt.subplots(2, 1)
axs[0].plot(cols, hor_proj)
axs[0].title.set_text('Horizontal Profile')
axs[1].plot(rows, vert_proj)
axs[1].title.set_text('Vertical Profile')
plt.tight_layout()
return fig
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment