- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我有已经重新缩放的 RGB 图像,因此较长的边缘变为 256 像素,现在我想用该图像的中值 RGB 值填充边框,因此生成的图像始终为 256x256 像素。
这段代码已经可以工作了,但我相信可以有更简单、更优雅的方法来做到这一点:
img = loadAndFitImage(filePath, maxSideLength=256, upscale=True)
shp = img.shape
#the shp in this case is typically (256,123,3) or (99,256,3)
leftPad = (256 - shp[0]) / 2
rightPad = 256 - shp[0] - leftPad
topPad = (256 - shp[1]) / 2
bottomPad = 256 - shp[1] - topPad
# this part looks like there might be a way to do it with one median call instead of 3:
median = (np.median(img[:, :, 0]),np.median(img[:, :, 1]),np.median(img[:, :, 2]))
img = np.lib.pad(img, ((leftPad,rightPad),(topPad,bottomPad),(0,0)),
'constant',constant_values=0)
if leftPad > 0:
img[:leftPad,:,0].fill(median[0])
img[:leftPad,:,1].fill(median[1])
img[:leftPad,:,2].fill(median[2])
if rightPad > 0:
img[-rightPad:,:,0].fill(median[0])
img[-rightPad:,:,1].fill(median[1])
img[-rightPad:,:,2].fill(median[2])
if topPad > 0:
img[:,:topPad,0].fill(median[0])
img[:,:topPad,1].fill(median[1])
img[:,:topPad,2].fill(median[2])
if bottomPad > 0:
img[:,-bottomPad:,0].fill(median[0])
img[:,-bottomPad:,1].fill(median[1])
img[:,-bottomPad:,2].fill(median[2])
编辑(附加信息):
最佳答案
您可以轻松地做到这一点:
import numpy as np
a = np.asarray([[1,2,3,4,5,6],
[8,4,5,6,7,7],
[1,2,3,4,5,6],
[1,2,3,4,5,6],
[1,2,3,4,5,6],
[1,2,3,4,5,6]])
b = a * 3
c = a * 4
d = (a,b,c)
im = np.asarray([np.pad(x, (2,), 'constant', constant_values=(np.median(x) ,)) for x in d])
print im
输出:
[[[ 4 4 4 4 4 4 4 4 4 4]
[ 4 4 4 4 4 4 4 4 4 4]
[ 4 4 1 2 3 4 5 6 4 4]
[ 4 4 8 4 5 6 7 7 4 4]
[ 4 4 1 2 3 4 5 6 4 4]
[ 4 4 1 2 3 4 5 6 4 4]
[ 4 4 1 2 3 4 5 6 4 4]
[ 4 4 1 2 3 4 5 6 4 4]
[ 4 4 4 4 4 4 4 4 4 4]
[ 4 4 4 4 4 4 4 4 4 4]]
[[12 12 12 12 12 12 12 12 12 12]
[12 12 12 12 12 12 12 12 12 12]
[12 12 3 6 9 12 15 18 12 12]
[12 12 24 12 15 18 21 21 12 12]
[12 12 3 6 9 12 15 18 12 12]
[12 12 3 6 9 12 15 18 12 12]
[12 12 3 6 9 12 15 18 12 12]
[12 12 3 6 9 12 15 18 12 12]
[12 12 12 12 12 12 12 12 12 12]
[12 12 12 12 12 12 12 12 12 12]]
[[16 16 16 16 16 16 16 16 16 16]
[16 16 16 16 16 16 16 16 16 16]
[16 16 4 8 12 16 20 24 16 16]
[16 16 32 16 20 24 28 28 16 16]
[16 16 4 8 12 16 20 24 16 16]
[16 16 4 8 12 16 20 24 16 16]
[16 16 4 8 12 16 20 24 16 16]
[16 16 4 8 12 16 20 24 16 16]
[16 16 16 16 16 16 16 16 16 16]
[16 16 16 16 16 16 16 16 16 16]]]
或者在您的特定情况下:
import numpy as np
from PIL import Image
filePath = '/home/george/Desktop/img.jpg'
Img = Image.open(filePath)
img = np.asarray(Img, np.int)
shp = np.shape(img)
img = img.transpose(2,0,1).reshape(3,215,215)
leftPad = round(float((255 - shp[0])) / 2)
rightPad = round(float(255 - shp[0]) - leftPad)
topPad = round(float((255 - shp[1])) / 2)
bottomPad = round(float(255 - shp[1]) - topPad)
pads = ((leftPad,rightPad),(topPad,bottomPad))
img_arr = np.ndarray((3,255,255),np.int)
for i,x in enumerate(img):
cons = np.int(np.median(x))
x_p = np.pad(x,pads,
'constant',
constant_values=cons)
img_arr[i,:,:] = x_p
im_shp = np.shape(img_arr)
ii = np.uint8(img_arr).transpose(1,2,0)
im = Image.fromarray(np.array( (ii) ))
im.show()
im.save((filePath), "JPEG")
输出:
关于python - 如何有效地用图像的中值填充 RGB numpy 数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31635936/
我拦截了一个数据包并提取了有效载荷。此有效负载是压缩的 jpeg 字节流数据(例如,将此数据分配给 unsigned char *payload )。我知道如果我有一个 FILE 指针,那么我可以使用
假设我们有一个单 channel 图像 (5x5) A = [ 1 2 3 4 5 6 7 8 9 2 1 4 5 6 3 4 5 6 7 4 3 4
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 2 年前。 Improve th
我有一个 RGB LED 并且有 9、10、11 的引脚和接地的引脚。已为 R、G 和 B 提供电阻器。 当我这样做时: analogWrite(r, 255); // I see a red c
我想知道如何从像素中获取颜色作为 RGB 整数,并在需要时进行转换。另外,如何利用差异来确定一个像素是否比另一个像素更亮或更暗。 最佳答案 简单: rgb_int = rgb_tuple[0] <<
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 这个问题似乎与 help center 中定义的范围内的编程无关。 . 关闭 3 年前。 Improve
我的目标是找到与数组中的 RGB 相比最接近的 RGB 匹配项。我已经创建了一个循环遍历图片中每个像素的函数。我现在唯一需要做的就是找到图片中每个像素最接近数组颜色的颜色。 $colors = arr
将 YUV 文件转换为 RGB 文件时出现问题。完成后,我无法使用 GIMP 或其他查看器打开 .rgb 文件,但我成功打开下载的 .rgb 文件。请告诉我RGB文件的结构是什么? (它是否包含标题?
我正在开发一个程序,在该程序中我获取图像的一部分并计算该图像的平均 RGB。当我计算它时,我得到完全不同的值,就好像我要使用内置函数一样。当我测试我的数字并将它们放入 RGB 颜色图表时,它们会关闭,
我正在尝试无损压缩图像,为了利用规律性,我想将图像从 RGB 转换为 Y'CbCr。 (我所说的 RGB 和 Y'CbCr 的具体细节在这里并不重要;RGB 数据由三个字节组成,我有三个字节来存储结果
我有一个应用程序可以生成一堆 jpg,我需要将这些 jpg 转换为 webm 视频。我正在尝试将 jpeg 中的 rgb 数据放入 vpxenc 示例中。我可以在输出视频中看到原始 jpg 的基本形状
我不太熟悉位移位,所以我有以下问题。我使用下面的函数(在别处找到)从 YUV 解码为 RGB int 数组。 现在我想调整红色或绿色或蓝色值来创建一些自定义滤镜效果。我需要检索 R 值、G 值和 B
在下面的代码片段中,我试图在 Visual C++ 中检索像素的 RGB 值,然后将相同的 RGB 值设置回相同的像素。也就是说,这只是一个测试。但是,当我这样做时,生成的图像相似但颜色错误/关闭。生
我试图在 上将 RGB 颜色从蓝色 (rgba(0,0,255)) 转换为红色 (rgba(255,0,0)) >JS mouseenter,渐进式。 因此,每次鼠标进入一个元素时,它都会“增加”其背
我需要根据像素的 RGB 颜色创建一个 Color 对象(读取 PNG 文件的 BufferedImage 对象,BufferedImage 颜色空间为 BufferedImage.TYPE_4BYT
我正在编写一段代码,它必须从 RGB 图像转换为 rgb 标准化空间。我已经使用 for 格式使用它,但它运行速度太慢,我需要评估大量图像。我正在尝试矢量化完整功能以加快它的速度。我现在有以下内容:
我想在多种光照条件下获取图像的 RGB 值。为了获得某种中性场景,我想使用一些预定义图像的 RGB 值对 RGB 值进行归一化。 让我解释一下。我有 6 张预定义图像,我知道它们的确切平均 RGB 值
将平行四边形((RGB)点的二维数组)投影到三角形((RGB)点的二维数组)(在我的特定情况下,将矩形投影到具有相同边长的直角三角形)的伪代码算法是什么(等腰),在我的例子中,斜边的大小与矩形的最大边
假设我有一张摄影底片扫描为 RGB 图像,我正试图找到一种算法将颜色值转换为 RGB 正片。 由于橙色偏差 (http://photo.net/learn/orange-negative-mask),
我已成功将图像转换为灰度图像,我想将灰度图像恢复为 RGB 图像。请帮忙。提前致谢。 -(UIImage *) toGrayscale { const int RED = 1;
我是一名优秀的程序员,十分优秀!