我正在尝试使用matplotlibRGB图像读取并将其转换为灰度.
在matlab中我用这个:
img = rgb2gray(imread('image.png'));
Run Code Online (Sandbox Code Playgroud)
在matplotlib教程中,他们没有涵盖它.他们只是读入图像
import matplotlib.image as mpimg
img = mpimg.imread('image.png')
Run Code Online (Sandbox Code Playgroud)
然后他们切割数组,但这与我理解的将RGB转换为灰度不同.
lum_img = img[:,:,0]
Run Code Online (Sandbox Code Playgroud)
我发现很难相信numpy或matplotlib没有内置函数可以将rgb转换为灰色.这不是图像处理中的常见操作吗?
我写了一个非常简单的函数,可以imread在5分钟内使用导入的图像.这是非常低效的,但这就是为什么我希望内置的专业实现.
塞巴斯蒂安已经改善了我的功能,但我仍然希望找到内置的功能.
matlab的(NTSC/PAL)实现:
import numpy as np
def rgb2gray(rgb):
r, g, b = rgb[:,:,0], rgb[:,:,1], rgb[:,:,2]
gray = 0.2989 * r + 0.5870 * g + 0.1140 * b
return gray
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用PIL /枕头的ImageDraw模块在图像上绘制粗矩形.
我试过使用draw.rectangle([x1, y1, x2, y2], outline='yellow', width=3)但它似乎不喜欢width参数.
我可以用一堆线来模仿我想做的事情,但我想知道是否有一种正确的方法.
'''
coordinates = [(x1, y1), (x2, y2)]
(x1, y1)
*--------------
| |
| |
| |
| |
| |
| |
--------------*
(x2, y2)
'''
def draw_rectangle(drawing, xy, outline='yellow', width=10):
top_left = xy[0]
bottom_right = xy[1]
top_right = (xy[1][0], xy[0][1])
bottom_left= (xy[0][0], xy[1][1])
drawing.line([top_left, top_right], fill=outline, width=width)
drawing.line([top_right, bottom_right], fill=outline, width=width)
drawing.line([bottom_right, bottom_left], fill=outline, width=width)
drawing.line([bottom_left, top_left], fill=outline, width=width)
Run Code Online (Sandbox Code Playgroud) 要创建带注释图像的COCO数据集,您需要根据对象的类型将二进制掩码转换为多边形或未压缩的运行长度编码表示。
该pycocotools图书馆功能编码和解码成和压缩RLE,但没有多边形和未压缩的RLE。
我可以使用 skimage 的测量库来生成多边形的面具,但我不确定如何创建未压缩的 RLE。
我可以使用这个 RLE 编码器从图像创建RLE的表示,但我不确定 COCO 期望什么格式。COCO 只是提到他们使用“自定义运行长度编码 (RLE) 方案”
例如,
ground_truth_binary_mask = np.array([[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0, 1, 1, 1, 0, 0],
[ 0, 0, 0, 0, 0, 1, 1, 1, 0, 0],
[ 0, 0, 0, 0, 0, …Run Code Online (Sandbox Code Playgroud) I'm using vscode with the python plugin and autopep8 with
"editor.formatOnSave": true.
I have local packages I need to import, so I have something like
import sys
sys.path.insert(0, '/path/to/packages')
import localpackage
Run Code Online (Sandbox Code Playgroud)
but when I save, vscode/autopep8 moves all import statements before code, so python can't find my local package.
import sys
import localpackage
sys.path.insert(0, '/path/to/packages')
Run Code Online (Sandbox Code Playgroud)
how can I tell vscode/autopep8 that it's okay to put a statement before imports, or is there a more correct way of importing local …
是否可以在Qt中禁用进度条的动画并使其行为像仪表?
下面是默认行为,我希望它不会让闪亮的波浪定期通过它.我希望用它来显示CPU,内存和磁盘空间等已用资源.

我在Rosetta Code的 MATLAB中找到了Hough变换的实现,但是我无法理解它.另外我想修改它以显示原始图像和重建线(de-Houghing).
理解它和de-Houghing的任何帮助表示赞赏.谢谢
为什么图像会翻转?
theImage = flipud(theImage);
我无法绕过规范功能.它的目的是什么,是否可以避免?
编辑: norm只是欧几里德距离的同义词:sqrt(width ^ 2 + height ^ 2)
rhoLimit = norm([width height]);
有人可以解释如何/为什么计算rho,theta和houghSpace?
rho = (-rhoLimit:1:rhoLimit);
theta = (0:thetaSampleFrequency:pi);
numThetas = numel(theta);
houghSpace = zeros(numel(rho),numThetas);
Run Code Online (Sandbox Code Playgroud)我怎么能去霍夫空间来重建线条呢?
使用使用身份(眼睛)功能创建的对角线的10x10图像调用该功能
theImage = eye(10)
thetaSampleFrequency = 0.1
[rho,theta,houghSpace] = houghTransform(theImage,thetaSampleFrequency)
Run Code Online (Sandbox Code Playgroud)
实际功能
function [rho,theta,houghSpace] = houghTransform(theImage,thetaSampleFrequency)
%Define the hough space
theImage = flipud(theImage);
[width,height] = size(theImage);
rhoLimit = norm([width height]);
rho = (-rhoLimit:1:rhoLimit);
theta = (0:thetaSampleFrequency:pi);
numThetas = numel(theta);
houghSpace = zeros(numel(rho),numThetas);
%Find the "edge" …Run Code Online (Sandbox Code Playgroud) 在Qt Designer中,我可以使用属性layout*Margin设置VBox或HBox的边距.
我怎么能在pyqt中做到这一点?
我试图在图像上叠加箭袋渐变箭头,但由于原点位置不同,它们看起来不正确。我将如何解决这个问题?
这是一个例子。左边的图像是我期望的渐变,但是一旦我将它们绘制在图像的顶部,由于原点位置的变化,它们指向错误的方向。
import matplotlib.pyplot as plt
import numpy as np
test_array = np.array([[0, 0, 0, 0, 0],
[0, 64, 128, 64, 0],
[0, 127, 255, 127, 0],
[0, 64, 127, 64, 0],
[0, 0, 0, 0, 0]]).astype("float")
dy, dx = np.gradient(test_array)
plt.imshow(test_array)
plt.quiver(dx, dy)
plt.show()
Run Code Online (Sandbox Code Playgroud)
我有一个图像和一些二进制蒙版,我想应用于图像以突出显示某些区域。我能够绘制一个蒙版,但随后应用的每个蒙版都会使前一个蒙版和原始图像越来越亮。
如何应用多个蒙版,同时保持每个蒙版和图像亮度恒定?
import numpy as np
import matplotlib
from matplotlib.pyplot import imshow
from PIL import Image, ImageDraw, ImageFont
import requests
matplotlib.rcParams['figure.figsize'] = (20.0, 10.0)
url = 'https://upload.wikimedia.org/wikipedia/commons/thumb/e/ee/Grumpy_Cat_by_Gage_Skidmore.jpg/480px-Grumpy_Cat_by_Gage_Skidmore.jpg'
image = (Image.open(requests.get(url, stream=True).raw)).convert('RGBA')
annotation1 = (np.ones((image.size[1], image.size[0], 3))*255).astype(np.uint8)
annotation1[350:400, 50:450] = (255, 0, 0)
mask1 = (np.zeros((image.size[1], image.size[0])))
mask1[350:400, 50:450] = 1
mask1 = Image.fromarray(mask1, mode='1')
annotation2 = (np.ones((image.size[1], image.size[0], 3))*255).astype(np.uint8)
annotation2[400:450, 50:450] = (255, 0, 0)
mask2 = (np.zeros((image.size[1], image.size[0])))
mask2[350:400, 50:450] = 1
mask2 = Image.fromarray(mask2, mode='1')
annotation1 = Image.fromarray(annotation1, …Run Code Online (Sandbox Code Playgroud) 将二进制PNG文件从PIL图像对象转换为numpy数组时,无论原始图像是否反转,其值都相同。
例如,这两个图像都产生相同的numpy数组。
import numpy as np
from PIL import Image
t = Image.open('t.png')
t_inverted = Image.open('t_inverted.png')
np.asarray(t)
np.asarray(t_inverted)
Run Code Online (Sandbox Code Playgroud)
np.asarray(t)或的输出np.asarray(t_inverted)是:
array([[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 1, 1, 0, 0, 1, 1, 0, 1],
[1, 1, 1, 1, 0, 0, 1, 1, 1, 1],
[1, 1, 1, 1, 0, 0, 1, 1, 1, 1],
[1, 1, 1, 1, 0, 0, 1, 1, …Run Code Online (Sandbox Code Playgroud) python ×7
numpy ×4
matplotlib ×2
qt ×2
autopep8 ×1
draw ×1
gradient ×1
image ×1
layout ×1
matlab ×1
progress-bar ×1
pyqt ×1
pyqt4 ×1
qt-designer ×1
windows ×1