我有一个图像存储为2d numpy数组(可能是multi-d).
我可以在反射2d滑动窗口的数组上进行视图,但是当我重塑它以使每行都是一个平坦的窗口(行是窗口,列是该窗口中的一个像素)时,python会生成一个完整的副本.它这样做是因为我使用了典型的步幅技巧,并且新形状在内存中不连续.
我需要这个,因为我将整个大图像传递给sklearn分类器,该分类器接受2d矩阵,其中没有批处理/部分拟合程序,并且完整的扩展副本对于存储器来说太大了.
我的问题:如果没有完整地复制视图,有没有办法做到这一点?
我相信一个答案将是(1)关于我忽略的步幅或numpy内存管理,或者(2)python的某种屏蔽内存结构,它可以模拟一个numpy数组,甚至包括像sklearn这样的外部包用Cython.
这种在内存中移动二维图像窗口的训练任务很常见,但我所知道的唯一一个直接考虑补丁的尝试是Vigra项目(http://ukoethe.github.io/vigra/).
谢谢您的帮助.
>>> A=np.arange(9).reshape(3,3)
>>> print A
[[0 1 2]
[3 4 5]
[6 7 8]]
>>> xstep=1;ystep=1; xsize=2; ysize=2
>>> window_view = np.lib.stride_tricks.as_strided(A, ((A.shape[0] - xsize + 1) / xstep, (A.shape[1] - ysize + 1) / ystep, xsize, ysize),
... (A.strides[0] * xstep, A.strides[1] * ystep, A.strides[0], A.strides[1]))
>>> print window_view
[[[[0 1]
[3 4]]
[[1 2]
[4 5]]]
[[[3 4]
[6 7]]
[[4 5]
[7 8]]]]
>>>
>>> np.may_share_memory(A,window_view)
True …Run Code Online (Sandbox Code Playgroud) 是否有可能检测到骰子的上侧?如果从顶部看,这将是一项简单的任务,从多个角度来看,多个方面都是可见的.
您通常想知道您取得的分数.我很容易提取所有点,但如何只提取顶部的那些?在这种特殊情况下,顶部是最大的,但这可能并非总是如此.我正在寻找一些评估顶部正方形(或者在这种情况下是圆形,我可以提取的)与底部网格给出的透视相关的变形.
下面给出了具有一些结果的示例程序.
import numpy as np
import cv2
img = cv2.imread('dice.jpg')
# Colour range to be extracted
lower_blue = np.array([0,0,0])
upper_blue = np.array([24,24,24])
# Threshold the BGR image
dots = cv2.inRange(img, lower_blue, upper_blue)
# Colour range to be extracted
lower_blue = np.array([0,0,0])
upper_blue = np.array([226,122,154])
# Threshold the BGR image
upper_side_shape = cv2.inRange(img, lower_blue, upper_blue)
cv2.imshow('Upper side shape',upper_side_shape)
cv2.imshow('Dots',dots)
cv2.waitKey(0)
cv2.destroyAllWindows()
Run Code Online (Sandbox Code Playgroud)
我使用以下代码从文件夹中读取一组tiff文件
from PIL import image
from skimage import io
io.use_plugin('pil')
images = os.listdir(train_data_path)
for image_name in images:
img = io.imread(os.path.join(train_data_path, image_name))
Run Code Online (Sandbox Code Playgroud)
运行上面的代码时,大多数文件都可以顺利读取.但我发现该程序会为某些特定文件生成一些警告消息
/devl/lib/python3.4/site-packages/scikit_image-0.12.3-py3.4-linux-x86_64.egg/skimage/external/tifffile/tifffile.py:1794: RuntimeWarning: py_decodelzw encountered unexpected end of stream
strip = decompress(strip)
Run Code Online (Sandbox Code Playgroud)
打开该文件时,我看不出与其他人有任何明显的区别.这可能是什么原因造成的?
我使用此功能将(512x512)2维阵列划分为2x2块.
skimage.util.view_as_blocks (arr_in, block_shape)
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11],
[12, 13, 14, 15]])
>>> B = view_as_blocks(A, block_shape=(2, 2))
>>> B[0, 0]
array([[0, 1],
[4, 5]])
>>> B[0, 1]
array([[2, 3],
[6, 7]])
Run Code Online (Sandbox Code Playgroud)
现在我需要在操作之后将相同的块放到原始位置,但是我在skimage中看不到任何功能.
合并非重叠数组的最佳方法是什么?
谢谢!
我只是想从skimage.exposure绘制Matplotlib直方图,但我得到了一个ValueError: bins must increase monotonically.原始图像来自这里和我的代码:
from skimage import io, exposure
import matplotlib.pyplot as plt
img = io.imread('img/coins_black_small.jpg', as_grey=True)
hist,bins=exposure.histogram(img)
plt.hist(bins,hist)
Run Code Online (Sandbox Code Playgroud)
ValueError:bin必须单调增加.
但是当我对bin值进行排序时会出现同样的错误:
import numpy as np
sorted_bins = np.sort(bins)
plt.hist(sorted_bins,hist)
Run Code Online (Sandbox Code Playgroud)
ValueError:bin必须单调增加.
我终于尝试检查了箱子的值,但是我认为它们似乎是排序的(对于这种测试的任何建议也会受到赞赏):
if any(bins[:-1] >= bins[1:]):
print "bim"
Run Code Online (Sandbox Code Playgroud)
没有输出.
关于会发生什么的任何建议?
我正在努力学习Python,所以请放纵.这是我的安装(在Linux Mint上):
在我的图像比较代码中如下:https : //www.pyimagesearch.com/2014/09/15/python-compare-two-images/
使用时
from skimage.measure import structural_similarity as ssim
然后
s = ssim(imageA, imageB)
我收到错误消息:
Run Code Online (Sandbox Code Playgroud)from skimage.measure import structural_similarity as ssimImportError:无法导入名称“ structural_similarity”
我试图找到给定图像的一些相对最大值。据我所知,有两种可能的方法,第一种是使用scipy.ndimage.maximum_filter(),第二种是使用 skimage.feature.peak_local_max()。
为了比较这两种方法,我修改了此处显示的skimage 的示例,以便比较找到的峰值。
from scipy import ndimage as ndi
import matplotlib.pyplot as plt
from skimage.feature import peak_local_max
from skimage import data, img_as_float
im = img_as_float(data.coins())
# use ndimage to find the coordinates of maximum peaks
image_max = ndi.maximum_filter(im, size=20) == im
j, i = np.where(image_max)
coordinates_2 = np.array(zip(i,j))
# use skimage to find the coordinates of local maxima
coordinates = peak_local_max(im, min_distance=20)
# display results
fig, axes = plt.subplots(1, 2, figsize=(8, 3), sharex=True, sharey=True) …Run Code Online (Sandbox Code Playgroud) 我正在开发 3D 重建系统,并希望使用 Python 3 从注册的点云数据生成三角形网格。我的对象不是凸的,因此行进立方体算法似乎是解决方案。
我更喜欢使用此类方法的现有实现,因此我尝试了scikit-image和Open3d,但这两个 API 都不接受原始点云作为输入(请注意,我不是这些库的专家)。我转换数据的尝试失败了,并且我已经没有想法了,因为文档没有阐明函数的输入格式。
这些是我想要的片段,这pcd_to_volume是我需要的。
scikit 图像
import numpy as np
from skimage.measure import marching_cubes_lewiner
N = 10000
pcd = np.random.rand(N,3)
def pcd_to_volume(pcd, voxel_size):
#TODO
volume = pcd_to_volume(pcd, voxel_size=0.05)
verts, faces, normals, values = marching_cubes_lewiner(volume, 0)
Run Code Online (Sandbox Code Playgroud)
开放3D
import numpy as np
import open3d
N = 10000
pcd = np.random.rand(N,3)
def pcd_to_volume(pcd, voxel_size):
#TODO
volume = pcd_to_volume(pcd, voxel_size=0.05)
mesh = volume.extract_triangle_mesh()
Run Code Online (Sandbox Code Playgroud)
我无法找到正确编写该pcd_to_volume函数的方法。我不喜欢图书馆,所以这两种解决方案对我来说都很好。
您对正确转换我的数据有什么建议吗?点云是一个Nx3矩阵,其中dtype=float.
您知道另一种适用于原始点云数据的[行进立方体算法]实现吗?我更喜欢 …
我已经成功安装了 skimage 软件包,但是当我尝试导入
from skimage.feature import graycomatrix, graycoprops
Run Code Online (Sandbox Code Playgroud)
它会导致错误:
cannot import name 'graycomatrix' from 'skimage.feature' (/Users/ain/opt/anaconda3/lib/python3.8/site-packages/skimage/feature/__init__.py)
Run Code Online (Sandbox Code Playgroud) 我正在尝试计算相应图像之间的 SSIM。例如,groundtruth 目录中名为 106.tif 的图像对应于 fake 目录中的“假”生成图像 106.jpg。
真实目录绝对路径是/home/pr/pm/zh_pix2pix/datasets/mousebrain/test/B
假目录绝对路径是/home/pr/pm/zh_pix2pix/output/fake_B
里面的图片是一一对应的,像这样: 见图片
我想一对一地比较数千张这样的图像。我不想将一张图像的 SSIM 与许多其他图像进行比较。相应的真实图像和假图像都具有相同的文件名,但扩展名不同(即106.tif和106.jpg),我只想将它们相互比较。
我正在努力以这种方式编辑用于 SSIM 比较的可用脚本。我想使用这个:https://github.com/mostafaGwely/Structural-Similarity-Index-SSIM-/blob/master/ssim.py但欢迎其他建议。代码也如下所示:
# Usage:
#
# python3 script.py --input original.png --output modified.png
# Based on: https://github.com/mostafaGwely/Structural-Similarity-Index-SSIM-
# 1. Import the necessary packages
#from skimage.measure import compare_ssim
from skimage.metrics import structural_similarity as ssim
import argparse
import imutils
import cv2
# 2. Construct the argument parse and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-f", "--first", required=True, help="Directory of the image …Run Code Online (Sandbox Code Playgroud) scikit-image ×10
python ×8
numpy ×2
opencv ×2
python-3.x ×2
image ×1
matplotlib ×1
ndimage ×1
scikit-learn ×1
scipy ×1
ssim ×1
tiff ×1