Python 2.7 Python多维数组
我是Python的初学者,但我想我有一个简单的问题。我使用图像处理来检测图像中的线条Python 2.7 Python多维数组,python-2.7,opencv,numpy,multidimensional-array,Python 2.7,Opencv,Numpy,Multidimensional Array,我是Python的初学者,但我想我有一个简单的问题。我使用图像处理来检测图像中的线条 lines = cv2.HoughLinesP(edges,1,np.pi/180,50,minLineLength,maxLineGap) lines.shape是(151,1,4),这意味着我检测到了151条线,并且有4个参数x1,y1,x2,y2 我想做的是为线条添加另一个因子,称为坡度,从而将lines.shape增加到(151,1,5)。我知道我可以在行尾连接一个空的零数组,但是我如何使它能够在fo
lines = cv2.HoughLinesP(edges,1,np.pi/180,50,minLineLength,maxLineGap)
lines.shape是(151,1,4),这意味着我检测到了151条线,并且有4个参数x1,y1,x2,y2
我想做的是为线条添加另一个因子,称为坡度,从而将lines.shape增加到(151,1,5)。我知道我可以在行尾连接一个空的零数组,但是我如何使它能够在for循环中调用它呢
例如,我想说
for slope in lines
#do stuff
不幸的是,
HoughLinesP
函数返回类型为int32
的numpy数组。不过,我熬夜想弄明白这一点,所以无论如何我都要把它贴出来。我只是将斜率乘以1000,然后像这样将它们放入数组中。希望它对你仍然有用
slopes = []
for row in lines:
slopes.append((row[0][1] - row[0][3]) / float(row[0][0] - row[0][2]) * 1000)
new_column = []
for slope in slopes:
new_column.append([slope])
new_array = np.insert(lines, 4, new_column, axis=2)
print lines
print
print new_array
样本输出:
[[[14 66 24 66]]
[[37 23 54 56]]
[[ 7 62 28 21]]
[[70 61 81 61]]
[[24 64 42 64]]]
[[[ 14 66 24 66 0]]
[[ 37 23 54 56 1941]]
[[ 7 62 28 21 -1952]]
[[ 70 61 81 61 0]]
[[ 24 64 42 64 0]]]
编辑:具有相同输出的更好(完整)代码
import cv2
import numpy as np
img = cv2.imread('cmake_logo-main.png')
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
edges = cv2.Canny(img,50,150,apertureSize = 3)
lines = cv2.HoughLinesP(edges,1,np.pi/180,50,3,10)
def slope(xy):
return (xy[1] - xy[3]) / float(xy[0] - xy[2]) * 1000
new_column = [[slope(row[0])] for row in lines]
new_array = np.insert(lines, 4, new_column, axis=2)
print lines
print
print new_array
不幸的是,
HoughLinesP
函数返回类型为int32
的numpy数组。不过,我熬夜想弄明白这一点,所以无论如何我都要把它贴出来。我只是将斜率乘以1000,然后像这样将它们放入数组中。希望它对你仍然有用
slopes = []
for row in lines:
slopes.append((row[0][1] - row[0][3]) / float(row[0][0] - row[0][2]) * 1000)
new_column = []
for slope in slopes:
new_column.append([slope])
new_array = np.insert(lines, 4, new_column, axis=2)
print lines
print
print new_array
样本输出:
[[[14 66 24 66]]
[[37 23 54 56]]
[[ 7 62 28 21]]
[[70 61 81 61]]
[[24 64 42 64]]]
[[[ 14 66 24 66 0]]
[[ 37 23 54 56 1941]]
[[ 7 62 28 21 -1952]]
[[ 70 61 81 61 0]]
[[ 24 64 42 64 0]]]
编辑:具有相同输出的更好(完整)代码
import cv2
import numpy as np
img = cv2.imread('cmake_logo-main.png')
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
edges = cv2.Canny(img,50,150,apertureSize = 3)
lines = cv2.HoughLinesP(edges,1,np.pi/180,50,3,10)
def slope(xy):
return (xy[1] - xy[3]) / float(xy[0] - xy[2]) * 1000
new_column = [[slope(row[0])] for row in lines]
new_array = np.insert(lines, 4, new_column, axis=2)
print lines
print
print new_array
根据你的评论,我猜你应该怎么做:
lines = np.squeeze(lines)
# remove the unneeded middle dim, a convenience, but not required
slope = <some calculation> # expect (151,) array of floats
mask = np.ones((151,),dtype=bool) # boolean mask
<assign False to mask for all lines you want to delete>
<alt start with False, and set True to keepers>
lines = lines[mask]
slope = lines[mask]
lines=np.挤压(行)
#移除不需要的中间dim,这很方便,但不是必需的
斜率=#期望(151,)浮点数组
掩码=np.ones((151,),dtype=bool)#布尔掩码
线条=线条[遮罩]
斜率=直线[遮罩]
或者,您可以使用np.hstack([lines,np.zero((151,1))]扩展行
(或在轴1上连接)。但是如果像Jason那样思考,行
是dtype
int
,而斜率
必须是浮点
,那就行不通了。你必须使用他的缩放解决方案
还可以使用结构化数组将int和float列组合到一个数组中。如果将
斜率
保持为单独变量同样容易,为什么要这样做呢?根据您的评论,我猜您应该怎么做:
lines = np.squeeze(lines)
# remove the unneeded middle dim, a convenience, but not required
slope = <some calculation> # expect (151,) array of floats
mask = np.ones((151,),dtype=bool) # boolean mask
<assign False to mask for all lines you want to delete>
<alt start with False, and set True to keepers>
lines = lines[mask]
slope = lines[mask]
lines=np.挤压(行)
#移除不需要的中间dim,这很方便,但不是必需的
斜率=#期望(151,)浮点数组
掩码=np.ones((151,),dtype=bool)#布尔掩码
线条=线条[遮罩]
斜率=直线[遮罩]
或者,您可以使用np.hstack([lines,np.zero((151,1))]扩展行
(或在轴1上连接)。但是如果像Jason那样思考,行
是dtype
int
,而斜率
必须是浮点
,那就行不通了。你必须使用他的缩放解决方案
还可以使用结构化数组将int和float列组合到一个数组中。如果将
slope
保持为单独的变量同样容易,为什么要这样做?是否需要将slope
作为同一数组的一部分?它不能只是另一个数组吗?在你的“循环”中,什么是#做事?它是在单个数字上运行还是在151个值的数组上运行?对于行中的x:
在数组的第一维上迭代<代码>行[:,:,-1]
给出数组的最后一个“列”(串联后的“斜率”列)。最后,我希望获得(大致)具有相同斜率和位置的所有行,并将它们删除,直到每个位置都有一行。(我应该在我的图片中只找到4行,但houghlinesp找到151行,其中大部分都很近或在彼此的顶部)。因此,不用从slope和line中删除元素,我可以直接从line中删除。slope是否需要成为同一数组的一部分?它不能只是另一个数组吗?在你的“循环”中,什么是#做事?它是在单个数字上运行还是在151个值的数组上运行?对于行中的x:
在数组的第一维上迭代<代码>行[:,:,-1]
给出数组的最后一个“列”(串联后的“斜率”列)。最后,我希望获得(大致)具有相同斜率和位置的所有行,并将它们删除,直到每个位置都有一行。(我应该在我的图片中只找到4行,但houghlinesp找到151行,其中大部分都很近或在彼此的顶部)。所以,不用从坡度和直线中删除元素,我可以直接从直线中删除。