2013-12-20 106 views
0

我有大文件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試試看。

+0

您是否嘗試過使用scipy.ndimage庫? – aplassard

回答

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

opencv可以加載圖像沒有問題。雖然我懷疑加載圖像不是問題。你的算法的第一步是偷看大約6個內存使用情況。所以如果你不在64位版本的python上,它會崩潰。此外,你的代碼看起來有問題。當我試圖運行它時,它在第二個功能上失敗。

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

這是(8003,10859,3)圖像。我也嘗試過只有一個通道的錯誤信息。

+0

我在8GB內存的機器上試過這個。我得到MemoryError。我需要幫助使用GDAL庫來處理大文件。如何檢查我的Python是32位還是64位? – user914425

+0

打開命令行並鍵入python。打印出來的第一行應該告訴你python的版本和體系結構。 – M4rtini

2

所有scikit-image算法都需要Numpy數組作爲輸入。因此您需要將數據集變量轉換爲ndarray。最簡單的方法是使用gdal插件來讀取文件(或者查看插件來源 - 它顯示瞭如何進行轉換)。

+0

如果GDAL可以用'ds = gdal.Open()'打開文件,則可以簡單地執行'image = ds.ReadAsArray()'。它會給你一個Numpy數組,我認爲這個佈局會是'[bands,y,x]',所以也許需要重新調整。 –

+0

@Rutger Kassies請提供一個有關Numpy數組和gdal.Open()的工作示例。我是新的python圖像處理。 – user914425