垂直网格线在大图像中的某个点停止出现

2024-10-02 12:26:51 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用matplotlib在图像上绘制垂直和水平网格线。生成的图像将在其上有编号的正方形。为此,我使用了this代码:

def drawGridOverImage(image):
    """Draw a numbered grid with 1cm2 boxes over an image."""
    import matplotlib.pyplot as plt
    import matplotlib.ticker as plticker
    
    my_dpi=300.

    # 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
    # my_dpi is dots per inch, so each square is 1 inch2
    myInterval=my_dpi
    loc = plticker.MultipleLocator(base=myInterval)
    ax.xaxis.set_major_locator(loc)
    ax.yaxis.set_major_locator(loc)

    # Add the grid
    ax.grid(True, which='major', axis='both', linestyle='-')

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

    # Add some labels to the gridsquares
    for j in range(ny):
        y=myInterval/2+j*myInterval
        for i in range(nx):
            x=myInterval/2.+float(i)*myInterval
            ax.text(x,y,'{:d}'.format(i+j*nx),color='r',ha='center',va='center')

    # Save the figure
    #fig.savefig('myImageGrid.tiff',dpi=my_dpi)
    newImageName = nameImageWithGrid(image.filename)
    fig.savefig(newImageName,dpi=my_dpi)
    return fig

但是,当我运行此代码时,垂直网格线在某个点停止出现。我附上了这张图片的一部分截图(整个图片非常大)来演示这个问题

enter image description here

经过一些搜索,我确实找到了之前的issue,所以我不确定这是否是同一个问题。值得注意的是,我正在处理非常大的图像,上图中的图像的尺寸为11648 × 8736.

我将感谢一些帮助解决这个问题,使垂直网格线出现在整个图像上

编辑: 我在这个链接here中找到的另一个大图像上尝试了代码,遇到了相同的问题,下面是垂直网格线停止的部分图像的屏幕截图: enter image description here


Tags: the代码图像imagegetmatplotlibmyfig
1条回答
网友
1楼 · 发布于 2024-10-02 12:26:51

我认为定位器是目前出现问题的原因。将为间隔值创建的记号绘制网格线。例如,如果像这样更改x轴上的记号标记,将正确绘制轴网线

# Set the gridding interval
# my_dpi is dots per inch, so each square is 1 inch2
myInterval=my_dpi
# loc = plticker.MultipleLocator(base=myInterval)
# ax.xaxis.set_major_locator(loc)
# ax.yaxis.set_major_locator(loc)
ax.set_xticks(np.arange(0,9900,myInterval))

相关问题 更多 >

    热门问题