我有大文件9600x7000像素JPG文件我想看看我是否可以做邊緣檢測。我嘗試使用加載大(25Mb)文件:Python大圖像邊緣檢測使用Scikit圖像和GDAL
from PIL import Image
image = Image.open("C:\\pathtofile\\test-tac.jpg")
image.show()
但是python解釋器會崩潰。我正在使用運行Python 2.7的Pycharm。因此,我使用GDAL(用於大 GEO refererencing文件)來加載文件。它會毫無問題地將文件加載到內存中。
#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"
這將加載文件。不過,我想在其上運行下面的邊緣檢測:
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很多有關數據:
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()
,如果我用下面的代碼生成圖像這會工作格式來自'圖像'變量與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庫? – aplassard