Issue
I have extracted the edges of an image in order to use them as coordinates to generate a graph with the edges of the image. But when trying to use plot or scatter methods, nothing and an error are displayed respectively for each function.
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)####
How can i plot the coordinates from the list"coordenadas" on a graph?
Solution
It would be easier with the error you get. This could do the trick:
coords = np.array(coordenadas)
plt.scatter(coords[:,0], coords[:,1])
Answered By - Pierre Vermot
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.