使用opencv python在图像上绘制网格线

使用opencv python在图像上绘制网格线,python,opencv,Python,Opencv,使用OpenCV python,我想在打开相机时制作一个网格。你们能帮我解释一下逻辑或代码吗。 请找到下面的图片链接以便更好地理解 您可以使用该功能在输入图像上绘制线条。因此,根据您要在何处绘制线,您的基本代码如下所示: img = cv2.imread(r"path\to\img") cv2.line(img, (start_x, start_y), (end_x, end_y), (255, 0, 0), 1, 1) cv2.line(img, (int(img.shape[1]/2),

使用OpenCV python,我想在打开相机时制作一个网格。你们能帮我解释一下逻辑或代码吗。 请找到下面的图片链接以便更好地理解


您可以使用该功能在输入图像上绘制线条。因此,根据您要在何处绘制线,您的基本代码如下所示:

img = cv2.imread(r"path\to\img")
cv2.line(img, (start_x, start_y), (end_x, end_y), (255, 0, 0), 1, 1)
cv2.line(img, (int(img.shape[1]/2), 0),(int(img.shape[1]/2), img.shape[0]), (255, 0, 0), 1, 1)
要获取图像的尺寸,可以使用
img.shape
,它将返回
(高度、宽度)

例如,要绘制一条穿过中心的垂直线,您的代码如下所示:

img = cv2.imread(r"path\to\img")
cv2.line(img, (start_x, start_y), (end_x, end_y), (255, 0, 0), 1, 1)
cv2.line(img, (int(img.shape[1]/2), 0),(int(img.shape[1]/2), img.shape[0]), (255, 0, 0), 1, 1)

可以使用函数在输入图像上绘制线条。因此,根据您要在何处绘制线,您的基本代码如下所示:

img = cv2.imread(r"path\to\img")
cv2.line(img, (start_x, start_y), (end_x, end_y), (255, 0, 0), 1, 1)
cv2.line(img, (int(img.shape[1]/2), 0),(int(img.shape[1]/2), img.shape[0]), (255, 0, 0), 1, 1)
要获取图像的尺寸,可以使用
img.shape
,它将返回
(高度、宽度)

例如,要绘制一条穿过中心的垂直线,您的代码如下所示:

img = cv2.imread(r"path\to\img")
cv2.line(img, (start_x, start_y), (end_x, end_y), (255, 0, 0), 1, 1)
cv2.line(img, (int(img.shape[1]/2), 0),(int(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')

这是我的问题的解决方案。利用它。

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')

def绘图网格(img,线条颜色=(0,255,0),厚度=1,类型=cv2.line\u AA,pxstep=50):
''(ndarray,3元组,int,int)->void
在img上绘制网格线
线条颜色:
颜色的BGR表示
厚度:
线厚
类型:
8、4或cv2.LINE_AA
步骤:
网格线频率(像素)
'''
x=pxstep
y=pxstep
而x
def绘图网格(img,线条颜色=(0,255,0),厚度=1,类型=cv2.line\u AA,pxstep=50):
''(ndarray,3元组,int,int)->void
在img上绘制网格线
线条颜色:
颜色的BGR表示
厚度:
线厚
类型:
8、4或cv2.LINE_AA
步骤:
网格线频率(像素)
'''
x=pxstep
y=pxstep
而x