- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有六张照片。我将它们更改为 block 结构。考虑一张尺寸为 200x200 的图像。1. 我转换成 10x10 的 block ,所以现在我总共有 400 个 block ,每个大小为 10x10。我为每张图片都做了这个。2.然后我找到每个 block 的平均值。再次为所有图像。3.找到所有 block 的平均值的最大值。所以现在我有一个长度为 400 的数组,其中每个值都是 block 值的最大值。4. 还找到了这些最大值的索引( block 数和图像数)
现在使用这个图像和 block 号我想生成一个图像,这样它应该从原始图像中挑选 block 并将它们放入空图像中以制作最终图像。我认为的一种方法是逐行挑选 block 并将其与原始图像匹配。然后将该 block 放置在空图像中,以便将相同的 block 放置在空图像中作为原始图像。这可能是错误的,但这只是我的一个想法,但我仍然不知道如何进行。
img = [cv2.imread(file,0) for file in glob.glob("resized/*.jpg")]
X=[]
for im in img:
arr = np.asarray(im)
arr = np.split(arr, 20)
arr = np.array([np.split(x, 20, 1) for x in arr])
mat = [arr[i][j].mean() for i in range(20) for j in range(20)]
X.append(mat)
max_X = list(zip(*X))
result = [max(i) for i in max_X]
print(result)
image_number = np.argmax(X,axis=0)
print(image_number)
result1 = [(row.index(max(row)),index) for index, row in enumerate(max_X)]
print(result1)
[(0, 0), (0, 1), (0, 2), (0, 3), (0, 4), (0, 5), (0, 6), (0, 7), (0, 8), (0, 9), (0, 10), (0, 11), (0, 12), (0, 13), (0, 14), (0, 15), (0, 16), (0, 17), (0, 18), (0, 19), (0, 20), (0, 21), (0, 22), (0, 23), (0, 24), (0, 25), (0, 26), (0, 27), (0, 28), (0, 29), (0, 30), (0, 31), (0, 32), (0, 33), (0, 34), (0, 35), (0, 36), (0, 37), (0, 38), (0, 39), (0, 40), (0, 41), (0, 42), (0, 43), (0, 44), (0, 45), (1, 46), (2, 47), (0, 48), (0, 49), (0, 50), (0, 51), (0, 52), (0, 53), (0, 54), (0, 55), (0, 56), (0, 57), (0, 58), (0, 59), (0, 60), (0, 61), (0, 62), (0, 63), (1, 64), (0, 65), (2, 66), (2, 67), (0, 68), (0, 69), (0, 70), (0, 71), (0, 72), (0, 73), (0, 74), (0, 75), (0, 76), (0, 77), (0, 78), (0, 79), (0, 80), (0, 81), (0, 82), (4, 83), (0, 84), (1, 85), (0, 86), (0, 87), (0, 88), (0, 89), (0, 90), (0, 91), (0, 92), (0, 93), (0, 94), (0, 95), (2, 96), (0, 97), (0, 98), (0, 99), (0, 100), (3, 101), (2, 102), (2, 103), (0, 104), (0, 105), (3, 106), (3, 107), (0, 108), (4, 109), (5, 110), (0, 111), (4, 112), (0, 113), (4, 114), (0, 115), (4, 116), (0, 117), (0, 118), (0, 119), (0, 120), (5, 121), (2, 122), (2, 123), (0, 124), (0, 125), (0, 126), (4, 127), (0, 128), (0, 129), (0, 130), (0, 131), (0, 132), (0, 133), (0, 134), (0, 135), (2, 136), (4, 137), (0, 138), (0, 139), (0, 140), (2, 141), (2, 142), (5, 143), (0, 144), (0, 145), (4, 146), (5, 147), (0, 148), (0, 149), (0, 150), (0, 151), (0, 152), (0, 153), (0, 154), (0, 155), (0, 156), (3, 157), (0, 158), (0, 159), (0, 160), (5, 161), (1, 162), (0, 163), (5, 164), (0, 165), (5, 166), (0, 167), (0, 168), (0, 169), (0, 170), (0, 171), (0, 172), (0, 173), (0, 174), (0, 175), (4, 176), (0, 177), (4, 178), (0, 179), (5, 180), (5, 181), (0, 182), (0, 183), (3, 184), (5, 185), (5, 186), (0, 187), (0, 188), (0, 189), (0, 190), (0, 191), (0, 192), (0, 193), (0, 194), (0, 195), (0, 196), (0, 197), (0, 198), (0, 199), (2, 200), (5, 201), (0, 202), (0, 203), (0, 204), (4, 205), (0, 206), (0, 207), (0, 208), (0, 209), (0, 210), (0, 211), (0, 212), (0, 213), (0, 214), (0, 215), (4, 216), (0, 217), (0, 218), (0, 219), (5, 220), (3, 221), (2, 222), (0, 223), (5, 224), (4, 225), (4, 226), (0, 227), (0, 228), (0, 229), (0, 230), (0, 231), (4, 232), (4, 233), (0, 234), (3, 235), (0, 236), (0, 237), (0, 238), (0, 239), (3, 240), (5, 241), (1, 242), (0, 243), (4, 244), (0, 245), (5, 246), (0, 247), (4, 248), (0, 249), (0, 250), (4, 251), (4, 252), (3, 253), (0, 254), (0, 255), (0, 256), (0, 257), (0, 258), (0, 259), (0, 260), (5, 261), (5, 262), (2, 263), (0, 264), (0, 265), (3, 266), (2, 267), (0, 268), (0, 269), (3, 270), (5, 271), (2, 272), (0, 273), (0, 274), (0, 275), (0, 276), (0, 277), (0, 278), (0, 279), (4, 280), (4, 281), (2, 282), (1, 283), (0, 284), (0, 285), (3, 286), (2, 287), (0, 288), (0, 289), (5, 290), (2, 291), (2, 292), (0, 293), (0, 294), (0, 295), (0, 296), (0, 297), (0, 298), (0, 299), (0, 300), (0, 301), (4, 302), (4, 303), (1, 304), (1, 305), (0, 306), (0, 307), (0, 308), (0, 309), (4, 310), (2, 311), (3, 312), (0, 313), (0, 314), (0, 315), (1, 316), (0, 317), (0, 318), (0, 319), (0, 320), (0, 321), (0, 322), (0, 323), (1, 324), (2, 325), (2, 326), (2, 327), (0, 328), (0, 329), (0, 330), (4, 331), (0, 332), (2, 333), (2, 334), (0, 335), (0, 336), (0, 337), (0, 338), (0, 339), (0, 340), (0, 341), (0, 342), (0, 343), (0, 344), (0, 345), (0, 346), (2, 347), (0, 348), (0, 349), (0, 350), (0, 351), (0, 352), (0, 353), (0, 354), (0, 355), (0, 356), (0, 357), (0, 358), (0, 359), (0, 360), (0, 361), (0, 362), (0, 363), (0, 364), (0, 365), (0, 366), (0, 367), (0, 368), (0, 369), (0, 370), (0, 371), (0, 372), (0, 373), (0, 374), (0, 375), (0, 376), (0, 377), (0, 378), (0, 379), (0, 380), (0, 381), (0, 382), (0, 383), (0, 384), (0, 385), (0, 386), (0, 387), (0, 388), (0, 389), (0, 390), (0, 391), (0, 392), (0, 393), (0, 394), (0, 395), (0, 396), (0, 397), (0, 398), (0, 399)]
在此我们有图像编号和 block 编号。现在我想从图像 0 中选择 block 0 并放入空图像等等。我希望这清除了预期的输出。
最佳答案
那么,这是我解决您的问题的方法。我重写了您的部分代码以删除所有列表,并且只处理 NumPy 数组。因此,我将所有图像存储在一个 4D 数组中,并将所有计算出的“ block 均值”存储在一个 3D 数组中。最后,我使用找到的 image_number
数组通过使用 OpenCV 的 resize
生成某种“索引数组”。使用 INTER_AREA
的方法插值标志(“使用像素区域关系重采样”)。这样做时,可以使用 NumPy 的 boolean array indexing 非常轻松地生成最终图像。 .
让我们看一下下面的代码:
import cv2
import numpy as np
# Read images in one single 4D array; resize to [200, 200]
nImages = 3
images = np.zeros((200, 200, 3, nImages), np.uint8)
images[:, :, :, 0] = cv2.resize(cv2.imread('U2Gmz.png', cv2.IMREAD_COLOR), (200, 200))
images[:, :, :, 1] = cv2.resize(cv2.imread('OZxf3.png', cv2.IMREAD_COLOR), (200, 200))
images[:, :, :, 2] = cv2.resize(cv2.imread('aISEB.png', cv2.IMREAD_COLOR), (200, 200))
# Calculate block means and store in one single 3D array
means = np.zeros((20, 20, nImages), np.uint8)
for im in range(nImages):
arr = np.split(images[:, :, :, im], 20)
arr = np.array([np.split(x, 20, 1) for x in arr])
means[:, :, im] = np.reshape([arr[i][j].mean() for i in range(20) for j in range(20)], (20, 20))
# Determine block mean maximum over all images
result = np.max(means, axis=2)
# Determine index of block mean maximum over all images
image_number = np.argmax(means, axis=2)
print(image_number)
# Resize index array with "resampling using pixel area relation" to final image size
image_number_idx = cv2.resize(np.uint8(image_number), (200, 200), interpolation=cv2.INTER_AREA)
# Generate final image by boolean array indexing
final = np.zeros((200, 200, 3), np.uint8)
for im in range(nImages):
idx = image_number_idx == im
final[idx, :] = images[idx, :, im]
# Show images
cv2.imshow('image1', images[:, :, :, 0])
cv2.imshow('image2', images[:, :, :, 1])
cv2.imshow('image3', images[:, :, :, 2])
cv2.imshow('final', final)
cv2.waitKey(0)
cv2.destroyAllWindows()
这些是使用过的图片:
image_number
输出给出了这个:
[[0 0 0 1 1 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0]
[1 0 0 0 1 0 0 0 0 0 0 0 0 1 1 1 1 0 0 0]
[1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0]
[0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1]
[0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 0 1 1 1 0]
[0 0 0 0 0 0 0 2 1 1 1 2 0 0 0 0 0 1 0 0]
[0 0 0 0 0 0 0 2 1 0 0 2 2 2 0 0 0 1 1 0]
[0 0 0 0 0 2 2 2 1 0 2 2 2 2 0 0 0 1 1 0]
[0 0 0 0 0 2 2 2 0 0 0 0 2 2 2 0 0 0 0 0]
[0 0 0 0 2 2 2 2 0 0 0 0 2 2 2 2 0 0 0 0]
[0 0 0 0 2 0 2 2 0 0 0 0 2 0 0 0 0 0 0 0]
[1 1 0 0 0 2 2 0 0 2 2 0 0 2 0 0 0 0 0 0]
[1 1 0 0 2 2 2 0 2 2 2 2 1 2 2 2 2 0 2 1]
[1 0 0 0 0 2 2 2 2 0 2 2 2 2 2 2 0 1 1 1]
[1 1 1 0 0 2 2 2 1 1 1 2 2 2 2 0 0 1 1 0]
[1 1 1 1 1 1 1 1 1 1 1 2 0 0 1 0 0 0 0 0]
[1 1 1 1 1 1 0 1 1 1 1 1 0 2 0 0 0 0 0 0]
[1 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
[1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0]
[0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1]]
最后的图像是这样的:
希望我正确理解了您的问题,这就是您想要实现的目标。
我的假设是,所有输入图像都具有相同的图像尺寸,此处为 (200, 200)
。否则,我想不出一种方法来管理可能不同的 block 大小,如果这里的“网格”(20, 20)
是固定的。
希望对您有所帮助!
编辑:要读取给定文件夹中的所有 jpg
文件,您可以使用:
files = glob.glob('resized/*.jpg')
# Read images in one single 4D array; resize to [200, 200]
nImages = len(files)
images = np.zeros((200, 200, 3, nImages), np.uint8)
for im in range(nImages):
images[:, :, :, im] = cv2.resize(cv2.imread(files[im], cv2.IMREAD_COLOR), (200, 200))
关于python - 组合不同图像的 block 并产生新图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58369483/
我在 Chrome 上做了一些测试,requestAnimationFrame 产生了 61 fps 而 setTimeOut( callback, 0 ) 产生了 233 fps。 如果一个人想要超
当我调试代码时,我发现 GCC 和 Clang 都为 0.0/0.0 产生 nan,这是我所期望的,但 GCC 产生的 nan 将符号位设置为 1,而Clang 将其设置为 0(如果我没记错的话,与
Closed. This question does not meet Stack Overflow guidelines。它当前不接受答案。 想改善这个问题吗?更新问题,以便将其作为on-topic
我在 R Studio 中有一个时间序列。现在我想计算这个系列的log()。我尝试了以下方法: i <- (x-y) ii <- log(i) 但是我得到以下信息:Warning message: I
我有兴趣了解 JavaScript 的内部结构.我试图阅读 SpiderMonkey 的来源和 Rhino但是绕过我的头是相当复杂的。 我问的原因是:为什么像 (![]+[])[+!![]+[]] 生
我们在 Delphi 中使用标准 TWebbrowser 组件,该组件在内部使用 mshtml.dll。另外,我们使用注册表来确保页面使用新的渲染引擎( Web-Browser-Control-Spe
我必须实现一个序列化/反序列化类,并且我正在使用 System.Xml.Serialization 。我有一些IList类型属性并希望在 IList 中序列化解码属于具有特定区域性信息的列表的所有十进
我有一个 Java 应用程序,它读取包含 SQL 查询的 JSON 文件,并使用 JDBC 在数据库上触发它们。 现在我有 5 万个这样的文件,我需要生成 5 万个独立线程来读取每个文件并将它们上传到
我正在尝试将 TensorFlow 入门页面上的示例线性回归程序调整为二次回归。为此,我只是添加了另一个变量并更改了函数。然而,这似乎会导致 NaN 值。这是我的代码: import numpy as
申请后KernelPCA到我的数据并将其传递给分类器 ( SVC ) 我收到以下错误: ValueError: Input contains NaN, infinity or a value too
这背后的想法是,如果我的数据库中存在登录名(正确的用户名+密码),我将重定向到一个页面,并且在进行此身份验证后,他们可以将消息存储在文本文件中。代码非常简单尽管我不确定为什么会收到 IllegalSt
我有一个返回 log10 值的函数。在将它们转换为正常数字时,出现溢出错误。 OverflowError: (34, 'Numerical result out of range') 我检查了日志值,
nosetests 抛出一个 ImportError,尽管我认为这是一个正确配置的 virtualenv。 ==============================================
我是这个网站的新手,所以如果我做错了什么,我提前道歉。当我尝试使用 kivy-garden 的 ScrollLabel 时,它给了我一个错误。基本上我正在尝试创建一个控制台日志,并且我需要能够在文本框
任何人都对 MDSJ 有任何经验?以下输入仅产生 NaN 结果,我不明白为什么。文档非常稀少。 import mdsj.Data; import mdsj.MDSJ; public class MDS
我有一个非常简单的 scala jcuda 程序,它添加了一个非常大的数组。一切都编译和运行得很好,直到我想从我的设备复制超过 4 个字节到主机。当我尝试复制超过 4 个字节时,我收到 CUDA_ER
我正在使用 Hero 组件在两个页面之间创建动画。Hero 组件用于包装一个 Image 小部件(没问题)和一个 Container 小部件(有问题)。 抛出以下溢出错误: ══╡ EXCEPTIO
我无法理解页面 https://developer.mozilla.org/en/JavaScript/Reference/Operators/Special/void 中的这一段: This ope
当在 Angular 中使用不立即触发事件的异步管道时(http 请求或任何有延迟的可观察对象),第一个值为 null为什么会这样?如何避免这种情况? 第一个变化: SimpleChange {
如果一个导入的库生成了一个会 panic 的 goroutine 怎么办?在这种情况下,开发人员无法阻止程序退出。 就像在这段代码中一样,使用延迟恢复调用一个错误的库没有帮助,因为该库正在生成一个 p
我是一名优秀的程序员,十分优秀!