我是R用户在Python中进行图像分析时遇到问题。计算图像中心建筑物面积的有效方法是什么?目标是将边缘算法应用于Google Maps静态图像并计算地址屋顶的表面区域。
from pygeocoder import Geocoder
import urllib
import numpy as np
from scipy import ndimage
from skimage import filter, io, measure
import matplotlib.pyplot as plt
def getMap(address):
"""Geocode address and retreive image centered
around lat/long"""
results = Geocoder.geocode(address)
lat, lng = results[0].coordinates
zip_code = results[0].postal_code
map_url = 'https://maps.googleapis.com/maps/api/staticmap?center={0},{1}&size=640x640&zoom=19&sensor=false&maptype=roadmap&&style=visibility:simplified|gamma:0.1'
request_url = map_url.format(lat, lng)
req = urllib.urlopen(request_url)
return(req)
def mapEdge(req):
"""Convert img to bytearray and do edge detection
on centered building"""
img = io.imread(req.geturl(),flatten=True)
labels, numobjects = ndimage.label(img)
edges = filter.canny(img, sigma=3)
plt.imshow(edges, cmap=plt.cm.gray)
plt.show()
map_tmp = getMap('1403 Elmwood Ave., Evanston, IL')
mapEdge(map_tmp)
答案 0 :(得分:1)
一种方法是使用opencv中可用的轮廓寻找技术,然后检测轮廓的中心。代码是用c ++编写的,但很容易转换成python
findContours( dst4, contours2, hierarchy2, CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE);
vector<vector<Point> > contours_poly2( contours2.size() );
vector<Rect> boundRect2( contours2.size() );
cout << contours2.size()<<endl;
Mat drawing1=Mat::zeros(dst4.rows,dst4.cols,dst4.depth());
for( int i = 0; i < contours2.size(); i++ ){
approxPolyDP( Mat(contours2[i]), contours_poly2[i], 3, true );
boundRect2[i] = boundingRect( Mat(contours_poly2[i]) );
}
现在您有了一个边界矩形矢量,因此如果边界矩形的中心非常靠近图像的中心,那么它就是一个正匹配。
这是使用python进行轮廓查找的教程 http://opencvpython.blogspot.in/2012/06/hi-this-article-is-tutorial-which-try.html
希望这有帮助
答案 1 :(得分:0)
我会看看其中一个库: