繁体   English   中英

使用Scikit-image和GDAL进行Python大图像边缘检测

[英]Python Large Image Edge Detection Using Scikit-image and GDAL

我有大文件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关于'image'变量与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试一试。

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

import cv2
image = cv2.imread('txu-pclmaps-oclc-22834566_a-2c.jpg')

opencv可以毫无问题地加载图像。 虽然我怀疑加载图像不是问题。 算法的第一步是查看大约6 GB的内存使用量。 所以如果你不是64位版本的python,它会崩溃。 你的代码看起来也有问题。 当我试图运行它时,它在第二个功能失败了。

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

这是(8003,10859,3)图像。 我也试过只有一个通道有相同的错误信息。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM