首页 > 解决方案 > 如何从绘制在图像上的线中收集像素?

问题描述

我有一个灰度图像,我使用 cv2 绘制了 4 条线:

钢的显微组织

但是现在我需要沿线收集像素值,创建“线轮廓”,创建四个列表,其中的值从左到右写入或从上到下。

我怎样才能做到这一点?

import cv2
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.image as img

#defines a scale factor
escala=0.7
cinza_esc = cv2.resize(cinza,None,fx=escala,fy=escala,interpolation=cv2.INTER_LINEAR)

#collect the dimensions of the image
xdim=cinza_esc.shape[1]
ydim=cinza_esc.shape[0]
se=(0,0);ie=(xdim,0);sd=(0,ydim);id=(xdim,ydim)

#line and text attr
cor = (0,0,0) #define a cor (tons de cinza)
fonte = cv2.FONT_HERSHEY_SIMPLEX
tamanho=int(xdim/500)
tipo_lh=cv2.LINE_4

#create the lines
cv2.line(cinza_esc, sd, ie, cor, 2)
cv2.line(cinza_esc, se, id, cor, 2)
cv2.line(cinza_esc, (0,int(ydim/2)), (xdim,int(ydim/2)), cor, 2)
cv2.line(cinza_esc, (int(xdim/2),0), (int(xdim/2),ydim), cor, 2)

#presents image
cv2.imshow("Imagem com as linhas", cinza_esc)

cv2.waitKey(0)
cv2.destroyAllWindows()

标签: pythonopencvcv2

解决方案


您实际上根本不需要画线。

如果你使用skimage.draw.line它会给你一个在线点列表。


推荐阅读