首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用OpenCV Python绘制图像中的网格线

问使用OpenCV Python绘制图像中的网格线
EN

Stack Overflow用户
提问于 2017-06-29 05:00:41
回答 4查看 21.5K关注 0票数 3

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

照相机打开,指向地板。

网格线在整个图像中被分割。

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2017-06-29 19:27:44

您可以使用cv2.line()函数在输入图像上绘制线条。因此,根据要绘制线条的位置,基本代码如下所示:

代码语言:javascript
复制
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)。

例如,要在中间画一条垂直线,代码如下所示:

代码语言:javascript
复制
cv2.line(img, (int(img.shape[1]/2), 0),(int(img.shape[1]/2), img.shape[0]), (255, 0, 0), 1, 1)
票数 2
EN

Stack Overflow用户

发布于 2021-09-08 05:47:47

下面是一个创建m网格的简单解决方案(尽可能平均分割):

代码语言:javascript
复制
import cv2 as cv  # tested with version 4.5.3.56 (pip install opencv-python)
import numpy as np


def draw_grid(img, grid_shape, color=(0, 255, 0), thickness=1):
    h, w, _ = img.shape
    rows, cols = grid_shape
    dy, dx = h / rows, w / cols

    # draw vertical lines
    for x in np.linspace(start=dx, stop=w-dx, num=cols-1):
        x = int(round(x))
        cv.line(img, (x, 0), (x, h), color=color, thickness=thickness)

    # draw horizontal lines
    for y in np.linspace(start=dy, stop=h-dy, num=rows-1):
        y = int(round(y))
        cv.line(img, (0, y), (w, y), color=color, thickness=thickness)

    return img

下面是一个将这个函数封装在CLI中的脚本:https://gist.github.com/mathandy/389ddbad48810d188bdc997c3a1dab0c

票数 7
EN

Stack Overflow用户

发布于 2017-07-25 05:48:49

,这是我的问题解决方案,伙计们。利用它.

代码语言:javascript
复制
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')

​

​

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44816682

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档