【问题标题】:Python Large Image Edge Detection Using Scikit-image and GDAL使用 Scikit-image 和 GDAL 的 Python 大图像边缘检测
【发布时间】:2013-12-21 08:22:25
【问题描述】:

我有一个 9600x7000 像素的 jpg 大文件,我正在尝试查看是否可以进行边缘检测。我尝试使用以下方法加载大 (25Mb) 文件:

from PIL import Image
image = Image.open("C:\\pathtofile\\test-tac.jpg")
image.show()

但是python解释器会崩溃。我正在使用运行 Python 2.7 的 Pycharm。

所以,我使用了 GDAL(用于大型 GEO 参考文件)来加载文件。它会毫无问题地将文件加载到内存中。

#reference http://www.gdal.org/gdal_tutorial.html
import gdal
from gdalconst import *

dataset = gdal.Open("C:\\pathtofile\\test-tac.jpg", GA_ReadOnly )
if dataset is None:
   print "error loading file in gdal"

这将加载文件。但是,我正在尝试对其进行以下边缘检测:

from matplotlib import pyplot as plt

from skimage import data
from skimage.feature import corner_harris, corner_subpix, corner_peaks
from skimage.transform import warp, AffineTransform
from skimage.draw import ellipse

# running corner Harris on the image object to detect image corners. 
#(reference http://scikit-image.org/docs/dev/auto_examples/plot_corner.html)
coords = corner_peaks(corner_harris(image), min_distance=3) #5
coords_subpix = corner_subpix(image, coords, window_size=13)

plt.gray()
plt.imshow(image, interpolation='nearest')
plt.plot(coords[:, 1], coords[:, 0], '.b', markersize=9)  # dots
plt.plot(coords_subpix[:, 1], coords_subpix[:, 0], '+r', markersize=15) # +
plt.plot(coords_subpix[:, 1][1], coords_subpix[:, 0][1], '*r', markersize=20)  #X_Point1=Subpix[:,1][1], Y_Point1=Subpix[:,0][1]

N=len(coords_subpix[:,0])
labels = ['point{0}'.format(i) for i in range(N)]

#Label corners in image
for label, x, y in zip(labels, coords_subpix[:,1], coords_subpix[:,0]):
   plt.annotate(label,
    xy=(x,y), xytext = (-10,10),
    textcoords = 'offset points', ha = 'right', va = 'bottom',
    bbox = dict(boxstyle = 'round,pad=0.5', fc = 'yellow', alpha = 0.5),
    arrowprops = dict(arrowstyle = '->', connectionstyle = 'arc3,rad=0'))

   plt.axis((0, 9672, 7272, 0))            # (Y_start, Y_Stop, X_Stop, X_Start) ((0, 9672, 7272, 0))
   plt.show()

如果我使用以下代码生成图像,这将起作用:

 tform = AffineTransform(scale=(1.3, 1.1), rotation=1, shear=0.8,
                    translation=(210, 50))
 image = warp(data.checkerboard(), tform.inverse, output_shape=(350, 350))
 rr, cc = ellipse(310, 175, 10, 100)
 image[rr, cc] = 1
 image[180:230, 10:60] = 1
 image[230:280, 60:110] = 1

我的问题是我不太了解 Python 关于“图像”变量与 GDAL 生成的数据集变量的数据格式。我的最终目标是能够使用 Python scikit-image 库对大 (10000x7000) 像素 jpg 图像进行边缘检测。如果 GDAL 有更好的方法来读取大型 jpg 图像,我愿意接受。

如果我设置:

image=dataset

运行它,我得到以下错误:

coords = corner_peaks(corner_harris(image), min_distance=3) #5
 File "C:\Python27\lib\site-packages\skimage\feature\corner.py", line 171, in corner_harris
 Axx, Axy, Ayy = _compute_auto_correlation(image, sigma)
 File "C:\Python27\lib\site-packages\skimage\feature\corner.py", line 54, in _compute_auto_correlation
 if image.ndim == 3:
 AttributeError: 'Dataset' object has no attribute 'ndim'

此错误消息指出我不了解数据集和图像变量之间的数据类型。

type(dataset)

给予:

<class 'osgeo.gdal.Dataset'>

类型(图片)

给予:

(350,350) float64.

对于您的大型源文件,请使用: http://www.lib.utexas.edu/maps/tpc/txu-pclmaps-oclc-22834566_a-2c.jpg试试看。

【问题讨论】:

  • 你试过使用 scipy.ndimage 库吗?

标签: python image-processing gdal edge-detection scikit-image


【解决方案1】:

所有 scikit-image 算法都需要 Numpy 数组作为输入。因此,您需要将数据集变量转换为 ndarray。最简单的方法是使用 gdal 插件来读取文件(或查看插件源——它显示了如何进行转换)。

【讨论】:

  • 如果GDAL可以使用ds = gdal.Open()打开文件,你可以简单地使用image = ds.ReadAsArray()。它会给你一个 Numpy 数组,我认为布局将是 [bands, y, x],所以可能需要一些重塑。
  • @Rutger Kassies 请提供一个使用 Numpy 数组和 gdal.Open() 的工作示例。我是新的薄 python 图像处理。
【解决方案2】:
import cv2
image = cv2.imread('txu-pclmaps-oclc-22834566_a-2c.jpg')

opencv 可以毫无问题地加载图像。虽然我怀疑加载图像不是问题。算法的第一步是查看大约 6 gigs 的内存使用情况。因此,如果您不在 64 位版本的 python 上,它将崩溃。您的代码似乎也有问题。当我尝试运行它时,它在第二个功能上失败了。

ValueError: operands could not be broadcast together with shapes (0,13) (13,13)

这是 (8003, 10859, 3) 图像。我也尝试过只使用一个带有相同错误消息的频道。

【讨论】:

  • 我在 8GB 内存的机器上试过这个。我收到内存错误。我需要帮助将 GDAL 库用于大文件。如何检查我的 python 是 32 位还是 64 位?
  • 打开命令行并输入 python。打印出来的第一行应该告诉你 python 版本和架构。
猜你喜欢
  • 2016-09-11
  • 1970-01-01
  • 2011-06-29
  • 2010-12-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多