使用opencv python,我想在打开相机时制作网格。你们能用逻辑或代码帮助我吗? 请查看下面的图片链接以便更好地理解。
答案 0 :(得分:3)
以下是我的问题解答。利用它。
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')
答案 1 :(得分:2)
您可以使用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)
要获取图片的尺寸,您可以使用img.shape
,它将返回(height, width)
。
例如,要在中心绘制一条垂直线,您的代码将如下所示:
cv2.line(img, (img.shape[1]/2, 0), (img.shape[1]/2, img.shape[0]), (255, 0, 0), 1, 1)
答案 2 :(得分:0)
def draw_grid(img, line_color=(0, 255, 0), thickness=1, type_=_cv2.LINE_AA, pxstep=50):
'''(ndarray, 3-tuple, int, int) -> void
draw gridlines on img
line_color:
BGR representation of colour
thickness:
line thickness
type:
8, 4 or cv2.LINE_AA
pxstep:
grid line frequency in pixels
'''
x = pxstep
y = pxstep
while x < img.shape[1]:
_cv2.line(img, (x, 0), (x, img.shape[0]), color=line_color, lineType=type_, thickness=thickness)
x += pxstep
while y < img.shape[0]:
_cv2.line(img, (0, y), (img.shape[1], y), color=line_color, lineType=type_, thickness=thickness)
y += pxstep