如何在 python 中导出没有图像的行
How to export lines without image in python
我正在使用 Houghlines 方法从我的图像创建 hough 线,returns 预期的结果。除了我想导出没有原始导入图像的 hough 线。怎么做?
import numpy as np
import cv2
in_path = 'my/tif/file'
out_path = 'my/output/tif/file'
gray = cv2.imread(in_path)
edges = cv2.Canny(gray,50,150,apertureSize = 3)
cv2.imwrite('edges.tif',edges)
minLineLength=10
lines = cv2.HoughLinesP(image=edges,rho=3,theta=np.pi/180, threshold=100,lines=np.array([]), minLineLength=minLineLength,maxLineGap=20)
a,b,c = lines.shape
for i in range(a):
cv2.line(gray, (lines[i][0][0], lines[i][0][1]), (lines[i][0][2], lines[i][0][3]), (255, 0, 0), 1, cv2.LINE_AA)
cv2.imwrite(out_path,gray)
是否可以将线条导出为矢量或普通图像?
首先创建一个具有与原始图像相同形状和数据类型的黑色像素的图像。然后在此图像上绘制检测到的线条。
black = np.zeros_like(gray)
这里black
是一个所有元素都为0的数组。换句话说,它是一个黑色图像,具有与gray
相同的形状和数据类型。
cv2.line(black, (lines[i][0][0], lines[i][0][1]), (lines[i][0][2], lines[i][0][3]), (255, 0, 0), 1, cv2.LINE_AA)
cv2.imwrite(out_path, black)
正确的方法是先在 for 循环中使用 cv21.line()
画线。在此之后继续使用 cv2.imwrite()
.
保存图像
这里是完整代码运行:
import numpy as np
import cv2
in_path = 'my/tif/file'
out_path = 'my/output/tif/file'
gray = cv2.imread(in_path)
edges = cv2.Canny(gray,50,150,apertureSize = 3)
cv2.imwrite('edges.tif',edges)
minLineLength=10
lines = cv2.HoughLinesP(image=edges,rho=3,theta=np.pi/180, threshold=100,lines=np.array([]), minLineLength=minLineLength,maxLineGap=20)
black = np.zeros_like(gray)
a,b,c = lines.shape
for i in range(a):
cv2.line(black, (lines[i][0][0], lines[i][0][1]), (lines[i][0][2], lines[i][0][3]), (255, 0, 0), 1, cv2.LINE_AA)
cv2.imwrite(out_path,gray)
我正在使用 Houghlines 方法从我的图像创建 hough 线,returns 预期的结果。除了我想导出没有原始导入图像的 hough 线。怎么做?
import numpy as np
import cv2
in_path = 'my/tif/file'
out_path = 'my/output/tif/file'
gray = cv2.imread(in_path)
edges = cv2.Canny(gray,50,150,apertureSize = 3)
cv2.imwrite('edges.tif',edges)
minLineLength=10
lines = cv2.HoughLinesP(image=edges,rho=3,theta=np.pi/180, threshold=100,lines=np.array([]), minLineLength=minLineLength,maxLineGap=20)
a,b,c = lines.shape
for i in range(a):
cv2.line(gray, (lines[i][0][0], lines[i][0][1]), (lines[i][0][2], lines[i][0][3]), (255, 0, 0), 1, cv2.LINE_AA)
cv2.imwrite(out_path,gray)
是否可以将线条导出为矢量或普通图像?
首先创建一个具有与原始图像相同形状和数据类型的黑色像素的图像。然后在此图像上绘制检测到的线条。
black = np.zeros_like(gray)
这里black
是一个所有元素都为0的数组。换句话说,它是一个黑色图像,具有与gray
相同的形状和数据类型。
cv2.line(black, (lines[i][0][0], lines[i][0][1]), (lines[i][0][2], lines[i][0][3]), (255, 0, 0), 1, cv2.LINE_AA)
cv2.imwrite(out_path, black)
正确的方法是先在 for 循环中使用 cv21.line()
画线。在此之后继续使用 cv2.imwrite()
.
这里是完整代码运行:
import numpy as np
import cv2
in_path = 'my/tif/file'
out_path = 'my/output/tif/file'
gray = cv2.imread(in_path)
edges = cv2.Canny(gray,50,150,apertureSize = 3)
cv2.imwrite('edges.tif',edges)
minLineLength=10
lines = cv2.HoughLinesP(image=edges,rho=3,theta=np.pi/180, threshold=100,lines=np.array([]), minLineLength=minLineLength,maxLineGap=20)
black = np.zeros_like(gray)
a,b,c = lines.shape
for i in range(a):
cv2.line(black, (lines[i][0][0], lines[i][0][1]), (lines[i][0][2], lines[i][0][3]), (255, 0, 0), 1, cv2.LINE_AA)
cv2.imwrite(out_path,gray)