opencv pythonを使用して、カメラをスイッチするときにグリッドを作成したいと思います。皆さんはロジックやコードで私を助けてくれますか? 理解のため、下の画像リンクをご覧ください。イメージ全体にグリッド線を描くopenccv python
Camera switched on and pointed to a floor
Grid Lines are splitted across the whole image
opencv pythonを使用して、カメラをスイッチするときにグリッドを作成したいと思います。皆さんはロジックやコードで私を助けてくれますか? 理解のため、下の画像リンクをご覧ください。イメージ全体にグリッド線を描くopenccv python
Camera switched on and pointed to a floor
Grid Lines are splitted across the whole image
あなたはcv2.line()機能を用いて、入力画像上に線を描くことができます。ですから、線を描画したい場所に応じて、あなたの基本的なコードは次のようになります。画像の大きさを取得するには
img = cv2.imread(r"path\to\img")
cv2.line(img, (start_x, start_y), (end_x, end_y), (255, 0, 0), 1, 1)
、あなたは(height, width)
を返されるimg.shape
を使用することができます。例えば中心を通る垂直線を描画するために
、あなたのコードは次のようになります。
cv2.line(img, (img.shape[1]/2, 0), (img.shape[1]/2, img.shape[0]), (255, 0, 0), 1, 1)
ここに私の質問の人たちのためのソリューションです。それを利用する。
import matplotlib.pyplot as plt
import matplotlib.ticker as plticker
try:
from PIL import Image
except ImportError:
import Image
# Open image file
image = Image.open('bird.jpg')
my_dpi=200.
# Set up figure
fig=plt.figure(figsize=(float(image.size[0])/my_dpi,float(image.size[1])/my_dpi),dpi=my_dpi)
ax=fig.add_subplot(111)
# Remove whitespace from around the image
fig.subplots_adjust(left=0,right=1,bottom=0,top=1)
# Set the gridding interval: here we use the major tick interval
myInterval=300.
loc = plticker.MultipleLocator(base=myInterval)
ax.xaxis.set_major_locator(loc)
ax.yaxis.set_major_locator(loc)
# Add the grid
ax.grid(which='major', axis='both', linestyle='-', color='g')
# Add the image
ax.imshow(image)
# Find number of gridsquares in x and y direction
nx=abs(int(float(ax.get_xlim()[1]-ax.get_xlim()[0])/float(myInterval)))
ny=abs(int(float(ax.get_ylim()[1]-ax.get_ylim()[0])/float(myInterval)))
# Save the figure
fig.savefig('birdgrid_without_Label.jpg')