Map Inference in the Face of Noise and Disparity代码重现遇到的问题(持续更新....)
目录
- 论文链接
- 安装环境
- 遇到的问题和解决方法
- 3.1 第一步运行kde.py
- 3.2 第二步运行skeleton.py
- 3.2.1 ImportError: No module named pyximport***
- 3.2.2 Pychorm提示Unresolved reference 导入模块报错
- 3.2.3 ImportError: Building module subiterations failed: [‘DistutilsPlatformError: Unable to find vcvarsall.bat\n’](生成模块子迭代失败
- 3.2.4 ImportError: Building module subiterations failed: [“CompileError: command ‘C:\\\\Users\\\\87792\\\\AppData\\\\Local\\\\Programs\\\\Common\\\\Microsoft\\\\Visual C++ for Python\\\\9.0\\\\VC\\\\Bin\\\\amd64\\\\cl.exe’ failed with exit status 2\n”]
1. 论文链接
Map Inference in the Face of Noise and Disparity
代码下载地址
2. 安装环境
python 2.7.15 win64位
Opencv 2.4.13 win64位 (安装过程可参考windows平台下python安装opencv,即实现import cv2功能)
pycharm 2019.3.3 win64位(来源微信公众号PyCharm2019【中文版】安装教程)
3. 遇到的问题和解决方法
3.1 第一步运行kde.py
#coding:utf-8
import cv2
import cv2.cv as cv
from math import atan2, sqrt, ceil, pi, fmod
import sys, getopt, os
from location import TripLoader
from pylibs import spatialfunclib
from itertools import tee, izip
import sqlite3
##
## important parameters
##
cell_size = 1 # meters
gaussian_blur = 17
trips_path = "trips/trips_1m/"
def pairwise(iterable):
"s -> (s0,s1), (s1,s2), (s2, s3), ..."
a, b = tee(iterable)
next(b, None)
return izip(a, b)
class KDE:
def __init__(self):
pass
def create_kde_with_trips(self, all_trips):
print "trips path: " + str(trips_path)
print "cell size: " + str(cell_size)
print "gaussian blur: " + str(gaussian_blur)
# flag to save images
save_images = True
sys.stdout.write("\nFinding bounding box... ")
sys.stdout.flush()
min_lat = all_trips[0].locations[0].latitude
max_lat = all_trips[0].locations[0].latitude
min_lon = all_trips[0].locations[0].longitude
max_lon = all_trips[0].locations[0].longitude
for trip in all_trips:
for location in trip.locations:
if (location.latitude < min_lat):
min_lat = location.latitude
if (location.latitude > max_lat):
max_lat = location.latitude
if (location.longitude < min_lon):
min_lon = location.longitude
if (location.longitude > max_lon):
max_lon = location.longitude
print "done."
# find bounding box for data
min_lat -= 0.003
max_lat += 0.003
min_lon -= 0.005
max_lon += 0.005
diff_lat = max_lat - min_lat
diff_lon = max_lon - min_lon
#print min_lat, min_lon, max_lat, max_lon
width = int(diff_lon * spatialfunclib.METERS_PER_DEGREE_LONGITUDE / cell_size)
height = int(diff_lat * spatialfunclib.METERS_PER_DEGREE_LATITUDE / cell_size)
yscale = height / diff_lat # pixels per lat
xscale = width / diff_lon # pixels per lon
# aggregate intensity map for all traces
themap = cv.CreateMat(height,width,cv.CV_16UC1)
cv.SetZero(themap)
##
## Build an aggregate intensity map from all the edges
##
trip_counter = 1
for trip in all_trips:
if ((trip_counter % 10 == 0) or (trip_counter == len(all_trips))):
sys.stdout.write("\rCreating histogram (trip " + str(trip_counter) + "/" + str(len(all_trips)) + ")... ")
sys.stdout.flush()
trip_counter += 1
temp = cv.CreateMat(height,width,cv.CV_8UC1)
cv.SetZero(temp)
temp16 = cv.CreateMat(height,width,cv.CV_16UC1)
cv.SetZero(temp16)
for (orig,dest) in pairwise(trip.locations):
oy = height - int(yscale * (orig.latitude - min_lat))
ox = int(xscale * (orig.longitude - min_lon))
dy = height - int(yscale * (dest.latitude - min_lat))
dx = int(xscale * (dest.longitude - min_lon))
cv.Line(temp, (ox, oy), (dx, dy), (32), 1, cv.CV_AA)
# accumulate trips into themap
cv.ConvertScale(temp,temp16,1,0)
cv.Add(themap,temp16,themap)
lines = cv.CreateMat(height,width,cv.CV_8U)
cv.SetZero(lines)
print "done."
trip_counter = 1
for trip in all_trips:
if ((trip_counter % 10 == 0) or (trip_counter == len(all_trips))):
sys.stdout.write("\rCreating drawing (trip " + str(trip_counter) + "/" + str(len(all_trips)) + ")... ")
sys.stdout.flush()
trip_counter += 1
for (orig, dest) in pairwise(trip.locations):
oy = height - int(yscale * (orig.latitude - min_lat))
ox = int(xscale * (orig.longitude - min_lon))
dy = height - int(yscale * (dest.latitude - min_lat))
dx = int(xscale * (dest.longitude - min_lon))
cv.Line(lines, (ox, oy), (dx, dy), (255), 1, cv.CV_AA)
# save the lines
cv.SaveImage("raw_data.png", lines)
print "done."
#print "Intensity map acquired."
sys.stdout.write("Smoothing... ")
sys.stdout.flush()
# # create the mask and compute the contour
cv.Smooth(themap, themap, cv.CV_GAUSSIAN, gaussian_blur, gaussian_blur)
cv.SaveImage("kde.png", themap)
print "done."
print "\nKDE generation complete."
if __name__ == '__main__':
opts,args = getopt.getopt(sys.argv[1:],"c:b:p:h")
for o,a in opts:
if o == "-c":
cell_size=int(a)
elif o == "-b":
gaussian_blur = int(a)
elif o == "-p":
trips_path = str(a)
elif o == "-h":
print "Usage: kde.py [-c <cell_size>] [-b <gaussian_blur_size>] [-p <trips_path>] [-h]\n"
sys.exit()
k = KDE()
k.create_kde_with_trips(TripLoader.load_all_trips(trips_path))
报错如下:
no module named cv,查了好久才发现是因为自己的opencv版本不匹配,这段代码是用opencv2下写的,而我安装了opencv3所以一开始import cv 便开始报错,后来改成了 import cv2,在77行78行又报 no module named CreateMat 和 no module named SetZero ,后来安装了opencv2后此问题完美解决,opencv2下载链接和安装过程可参见(安装过程可参考windows平台下python安装opencv,即实现import cv2功能)。
3.2 第二步运行skeleton.py
#coding:utf-8
import pyximport; pyximport.install()
import subiterations
import numpy as np
import scipy.ndimage as nd
from scipy.ndimage import imread
from scipy.misc import imsave, toimage
from scipy.stats import threshold
from itertools import izip
from multiprocessing import Process, Manager, cpu_count
from threading import Thread
from scipy.ndimage.morphology import grey_closing
import math
skeleton_images_path = "skeleton_images/"
class GrayscaleSkeleton:
def __init__(self):
pass
def skeletonize(self, image):
image = grey_closing(image, footprint=circle(8), mode='constant', cval=0.0)
image = add_zero_mat(image)
prev_binary_image = np.zeros_like(image)
image_bit_depth = (image.dtype.itemsize * 8) / 2
print "image_bit_depth: " + str(image_bit_depth)
#image_thresholds = range(2**image_bit_depth,-1,-16)
image_thresholds = [2**x for x in range(image_bit_depth, 3, -1)] + range(15, 0, -1)
print "image_thresholds: " + str(image_thresholds)
for curr_threshold in image_thresholds:
print "curr_threshold: " + str(curr_threshold)
curr_thresh_image = threshold(image, curr_threshold)
curr_binary_image = curr_thresh_image.astype(np.bool).astype(np.int)
imsave(skeleton_images_path + "binary_" + str(curr_threshold) + ".png", curr_binary_image)
curr_sum_image = (prev_binary_image + curr_binary_image)
curr_skeleton_image = self.thin_pixels(curr_sum_image)
imsave(skeleton_images_path + "skeleton_" + str(curr_threshold) + ".png", curr_skeleton_image)
print "curr_skeleton max: " + str(curr_skeleton_image.max())
prev_binary_image = curr_skeleton_image
return remove_zero_mat(prev_binary_image)
def thin_pixels(self, image):
pixel_removed = True
neighbors = nd.convolve((image>0).astype(np.int),[[1,1,1],[1,0,1],[1,1,1]],mode='constant',cval=0.0)
fg_pixels = np.where((image==1) & (neighbors >= 2) & (neighbors <= 6))
check_pixels = zip(fg_pixels[0], fg_pixels[1])
while len(check_pixels)>0:
print len(check_pixels)
(image, sub1_check_pixels) = self.parallel_sub(subiterations.first_subiteration, image, check_pixels)
(image, sub2_check_pixels) = self.parallel_sub(subiterations.second_subiteration, image, list(set(check_pixels+sub1_check_pixels)))
check_pixels=list(set(sub1_check_pixels+sub2_check_pixels))
neighbors = nd.convolve(image>0,[[1,1,1],[1,0,1],[1,1,1]],mode='constant',cval=0.0)
fg_pixels = np.where(image==1)
check_pixels = zip(fg_pixels[0],fg_pixels[1])
(image, _) = self.parallel_sub(self.empty_pools, image, check_pixels)
return image
def parallel_sub(self, sub_function, image, fg_pixels):
manager = Manager()
queue = manager.Queue()
next_queue = manager.Queue()
num_procs = int(math.ceil(float(cpu_count()) * 0.75))
workload_size = int(math.ceil(float(len(fg_pixels)) / float(num_procs)))
process_list = []
if len(fg_pixels) == 0:
return (image, [])
(zero_pixels, next_pixels) = sub_function(image,fg_pixels)
for (x,y) in zero_pixels:
image[x][y]=0;
return (image, next_pixels)
def PRE_first_subiteration(self, curr_image, fg_pixels):
zero_pixels = { }
next_pixels = { }
for (i, j) in fg_pixels:
if curr_image[i][j] != 1: continue
p2 = curr_image[i - 1][j]
p3 = curr_image[i - 1][j + 1]
p4 = curr_image[i][j + 1]
p5 = curr_image[i + 1][j + 1]
p6 = curr_image[i + 1][j]
p7 = curr_image[i + 1][j - 1]
p8 = curr_image[i][j - 1]
p9 = curr_image[i - 1][j - 1]
if (2 <= (bool(p2) + bool(p3) + bool(p4) + bool(p5) + bool(p6) + bool(p7) + bool(p8) + bool(p9)) <= 6 and
(p2 * p4 * p6 == 0) and
(p4 * p6 * p8 == 0)):
if ((not p2 and p3) + (not p3 and p4) + (not p4 and p5) + (not p5 and p6) + (not p6 and p7) + (not p7 and p8) + (not p8 and p9) + (not p9 and p2) == 1):
zero_pixels[(i,j)] = 0
if p2 == 1:
next_pixels[(i-1,j)]=0
if p3 == 1:
next_pixels[(i-1,j+1)]=0
if p4 == 1:
next_pixels[(i,j+1)]=0
if p5 == 1:
next_pixels[(i+1,j+1)]=0
if p6 == 1:
next_pixels[(i+1,j)]=0
if p7 == 1:
next_pixels[(i+1,j-1)]=0
if p8 == 1:
next_pixels[(i,j-1)]=0
if p9 == 1:
next_pixels[(i-1,j-1)]=0
return zero_pixels.keys(), next_pixels.keys()
def PRE_second_subiteration(self, curr_image, fg_pixels):
zero_pixels = { }
next_pixels = { }
for (i, j) in fg_pixels:
if curr_image[i][j] != 1: continue
p2 = curr_image[i - 1][j]
p3 = curr_image[i - 1][j + 1]
p4 = curr_image[i][j + 1]
p5 = curr_image[i + 1][j + 1]
p6 = curr_image[i + 1][j]
p7 = curr_image[i + 1][j - 1]
p8 = curr_image[i][j - 1]
p9 = curr_image[i - 1][j - 1]
if (2 <= (bool(p2) + bool(p3) + bool(p4) + bool(p5) + bool(p6) + bool(p7) + bool(p8) + bool(p9)) <= 6 and
(p2 * p4 * p8 == 0) and
(p2 * p6 * p8 == 0)):
if ((not p2 and p3) + (not p3 and p4) + (not p4 and p5) + (not p5 and p6) + (not p6 and p7) + (not p7 and p8) + (not p8 and p9) + (not p9 and p2) == 1):
zero_pixels[(i,j)] = 0
if p2 == 1:
next_pixels[(i-1,j)]=0
if p3 == 1:
next_pixels[(i-1,j+1)]=0
if p4 == 1:
next_pixels[(i,j+1)]=0
if p5 == 1:
next_pixels[(i+1,j+1)]=0
if p6 == 1:
next_pixels[(i+1,j)]=0
if p7 == 1:
next_pixels[(i+1,j-1)]=0
if p8 == 1:
next_pixels[(i,j-1)]=0
if p9 == 1:
next_pixels[(i-1,j-1)]=0
return zero_pixels.keys(), next_pixels.keys()
def empty_pools(self, curr_image, fg_pixels):
zero_pixels = { }
for (i, j) in fg_pixels:
p2 = curr_image[i - 1][j]
p3 = curr_image[i - 1][j + 1]
p4 = curr_image[i][j + 1]
p5 = curr_image[i + 1][j + 1]
p6 = curr_image[i + 1][j]
p7 = curr_image[i + 1][j - 1]
p8 = curr_image[i][j - 1]
p9 = curr_image[i - 1][j - 1]
if (bool(p2) + bool(p3) + bool(p4) + bool(p5) + bool(p6) + bool(p7) + bool(p8) + bool(p9) > 6):
zero_pixels[(i,j)] = 0
return zero_pixels,[]
#
# helper functions
#
def add_zero_mat(image):
num_rows, num_cols = image.shape
image = np.insert(image, num_rows, np.zeros(num_cols, dtype=np.int), 0)
image = np.insert(image, 0, np.zeros(num_cols, dtype=np.int), 0)
num_rows, num_cols = image.shape
image = np.insert(image, num_cols, np.zeros(num_rows, dtype=np.int), 1)
image = np.insert(image, 0, np.zeros(num_rows, dtype=np.int), 1)
return image
def remove_zero_mat(image):
num_rows, num_cols = image.shape
image = np.delete(image, num_rows - 1, 0)
image = np.delete(image, 0, 0)
image = np.delete(image, num_cols - 1, 1)
image = np.delete(image, 0, 1)
return image
def circle(radius):
x, y = np.mgrid[:(2 * radius) + 1, :(2 * radius) + 1]
circle = (x - radius) ** 2 + (y - radius) ** 2
return (circle <= (radius ** 2)).astype(np.int)
import sys, time
if __name__ == '__main__':
input_filename = str(sys.argv[1])
output_filename = str(sys.argv[2])
print "input filename: " + str(input_filename)
print "output filename: " + str(output_filename)
input_kde = imread(input_filename)
s = GrayscaleSkeleton()
start_time = time.time()
skeleton = s.skeletonize(input_kde)
print "total elapsed time: " + str(time.time() - start_time) + " seconds"
toimage(skeleton, cmin=0, cmax=255).save(output_filename)
3.2.1 ImportError: No module named pyximport***
解决方案:
保证安装了cython,可在https://www.lfd.uci.edu/~gohlke/pythonlibs/下载对应版本,然后通过pip安装,然后,在代码中import相应包之前,添加如下代码:
import pyximport; pyximport.install()
import subiterations
3.2.2 Pychorm提示Unresolved reference 导入模块报错
具体情况和解决办法可参考这篇文章,亲测有效https://www.cnblogs.com/NancyRM/p/8258372.html
3.2.3 ImportError: Building module subiterations failed: [‘DistutilsPlatformError: Unable to find vcvarsall.bat\n’](生成模块子迭代失败
(1)关于 ‘DistutilsPlatformError: Unable to find vcvarsall.bat\n’的解决可见
Unable to find vcvarsall.bat
里面整理的非常全面,找到python2.7的路径:D:\Python27\Lib\distutils
找到:msvc9compiler.py修改两个地方:具体的路径以自己安装路径为准
如下:
def get_build_version():
"""Return the version of MSVC that was used to build Python. For Python 2.3 and up, the version number is included in sys.version. For earlier versions, assume the compiler is MSVC 6. """
return 9.0
prefix = "MSC v."
i = sys.version.find(prefix)
if i == -1:
return 6
i = i + len(prefix)
s, rest = sys.version[i:].split(" ", 1)
majorVersion = int(s[:-2]) - 6
minorVersion = int(s[2:3]) / 10.0
# I don't think paths are affected by minor version in version 6
if majorVersion == 6:
minorVersion = 0
if majorVersion >= 6:
return majorVersion + minorVersion
# else we don't know what version of the compiler this is
return None
def find_vcvarsall(version):
"""Find the vcvarsall.bat file At first it tries to find the productdir of VS 2008 in the registry. If that fails it falls back to the VS90COMNTOOLS env var. """
return r'C:\Users\87792\AppData\Local\Programs\Common\Microsoft\Visual C++ for Python\9.0\vcvarsall.bat'
vsbase = VS_BASE % version
try:
productdir = Reg.get_value(r"%s\Setup\VC" % vsbase,
"productdir")
except KeyError:
productdir = None
3.2.4 ImportError: Building module subiterations failed: [“CompileError: command ‘C:\\Users\\87792\\AppData\\Local\\Programs\\Common\\Microsoft\\Visual C++ for Python\\9.0\\VC\\Bin\\amd64\\cl.exe’ failed with exit status 2\n”]
暂时还未找到解决办法
还没有评论,来说两句吧...