如何在 pyplot 中绘制坐标列表?
How can I plot a list of coordinates in pyplot?
我提取了图像的边缘,以便将它们用作坐标来生成具有图像边缘的图形。但是当尝试使用 plot 或 scatter 方法时,每个函数分别不显示任何内容和错误。
import cv2 as cv
import numpy as np
from matplotlib import pyplot as grafico
def bwareaopen(img, min_size, connectivity=8):
"""Remove small objects from binary image (partial approximation of
bwareaopen in Matlab for 2D images).
Args:
img: a binary image (dtype=uint8) to remove small objects from
min_size: the maximum size of small objects to be removed
connectivity: Pixel connectivity; either 4 (connected via edges) or 8 (connected via edges and corners).
Returns:
the binary image with small objects removed
"""
# Find all connected components (called here "labels")
num_labels, labels, stats, centroids = cv.connectedComponentsWithStats(
img, connectivity=connectivity)
# check size of all connected components (area in pixels)
for i in range(num_labels):
label_size = stats[i, cv.CC_STAT_AREA]
# remove connected components smaller than min_size
if label_size < min_size:
img[labels == i] = 0
return img
# abre a img com opencv
path_img = 'input.jpeg'
img = cv.imread(path_img)
# converte imagem p/ tons de cinza
gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
# calculo de de thresholding com otsu
ret, level = cv.threshold(gray, 0, 255, cv.THRESH_BINARY | cv.THRESH_OTSU);
# converte imagem p/ preto e branco
_, bw_img = cv.threshold(gray, 127, 255, cv.THRESH_BINARY)
bw_img - bwareaopen(bw_img, 50)
# detector de bordas
bw_img_edges = cv.Canny(bw_img, 400, 800)
#cv.imwrite('edge.jpeg', bw_img_edges)
# checando o resultado do Canny
cv.imshow("Edges", bw_img_edges)
cv.waitKey(0)
# detecta as dimensoes da imagem em uma variavel
bw_img_shape = bw_img_edges.shape
# detecta onde ha bordas na imagem e captura sua posicao
# lista onde as coordenadas ficarao salvas
coordenadas = []
# dimensoes da imagem em variaveis
height, width = img.shape[:2]
for i in range(1, height):
for j in range(1, width):
if(bw_img_edges[i,j]==255):
coordenadas.append([i,j])
# coloca as coordenadas no pyplot(grafico)
####grafico.plot(coordenadas)####
如何在图表上绘制列表“coordenadas”中的坐标?
你得到的错误会更容易。这可以解决问题:
coords = np.array(coordenadas)
plt.scatter(coords[:,0], coords[:,1])
我提取了图像的边缘,以便将它们用作坐标来生成具有图像边缘的图形。但是当尝试使用 plot 或 scatter 方法时,每个函数分别不显示任何内容和错误。
import cv2 as cv
import numpy as np
from matplotlib import pyplot as grafico
def bwareaopen(img, min_size, connectivity=8):
"""Remove small objects from binary image (partial approximation of
bwareaopen in Matlab for 2D images).
Args:
img: a binary image (dtype=uint8) to remove small objects from
min_size: the maximum size of small objects to be removed
connectivity: Pixel connectivity; either 4 (connected via edges) or 8 (connected via edges and corners).
Returns:
the binary image with small objects removed
"""
# Find all connected components (called here "labels")
num_labels, labels, stats, centroids = cv.connectedComponentsWithStats(
img, connectivity=connectivity)
# check size of all connected components (area in pixels)
for i in range(num_labels):
label_size = stats[i, cv.CC_STAT_AREA]
# remove connected components smaller than min_size
if label_size < min_size:
img[labels == i] = 0
return img
# abre a img com opencv
path_img = 'input.jpeg'
img = cv.imread(path_img)
# converte imagem p/ tons de cinza
gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
# calculo de de thresholding com otsu
ret, level = cv.threshold(gray, 0, 255, cv.THRESH_BINARY | cv.THRESH_OTSU);
# converte imagem p/ preto e branco
_, bw_img = cv.threshold(gray, 127, 255, cv.THRESH_BINARY)
bw_img - bwareaopen(bw_img, 50)
# detector de bordas
bw_img_edges = cv.Canny(bw_img, 400, 800)
#cv.imwrite('edge.jpeg', bw_img_edges)
# checando o resultado do Canny
cv.imshow("Edges", bw_img_edges)
cv.waitKey(0)
# detecta as dimensoes da imagem em uma variavel
bw_img_shape = bw_img_edges.shape
# detecta onde ha bordas na imagem e captura sua posicao
# lista onde as coordenadas ficarao salvas
coordenadas = []
# dimensoes da imagem em variaveis
height, width = img.shape[:2]
for i in range(1, height):
for j in range(1, width):
if(bw_img_edges[i,j]==255):
coordenadas.append([i,j])
# coloca as coordenadas no pyplot(grafico)
####grafico.plot(coordenadas)####
如何在图表上绘制列表“coordenadas”中的坐标?
你得到的错误会更容易。这可以解决问题:
coords = np.array(coordenadas)
plt.scatter(coords[:,0], coords[:,1])