Skip to content

Instantly share code, notes, and snippets.

@FradSer
Created January 6, 2022 17:17
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save FradSer/4f8c99a51220661db8391208497d30f6 to your computer and use it in GitHub Desktop.
Save FradSer/4f8c99a51220661db8391208497d30f6 to your computer and use it in GitHub Desktop.
Vision Center
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import matplotlib.patches as patches
import matplotlib.text as text
def rgb2gray(rgb):
return np.dot(rgb[...,:3], [0.2989, 0.5870, 0.1140])
img = mpimg.imread('./image2.png')
gray = rgb2gray(img)
x = np.arange(1024+1)[1:]
final_x = []
# x is some generator
for item in x:
final_x.append(x)
final_y = np.rot90(final_x,3)
vb_x = np.sum((gray)*final_x) / np.sum(gray)
vb_y = np.sum((gray)*final_y) / np.sum(gray)
fig, ax = plt.subplots()
ax.add_patch(patches.Rectangle((0.45*1024, 0.45*1024), 0.1*1024, 0.1*1024, linewidth=1, edgecolor='g', facecolor='none'))
ax.text(vb_x/1024, 0.98 - vb_y/1024, '(' + '{:.1f}'.format(vb_x) + ', ' + '{:.1f}'.format(vb_y) + ')', horizontalalignment='left',verticalalignment='top', transform=ax.transAxes, color='r')
plt.imshow(gray, cmap=plt.get_cmap('gray'), vmin=0, vmax=1)
plt.plot(vb_x,vb_y,color='red', marker='o')
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment