這是一個問題:我想在下面的不規則形狀(

在我找到點的 x 和 y 之后,我將能夠計算 2 個點之間的歐幾里得距離
dist=sqrt((y2-y1)^2 (x2-x1)^2)
請在 colab 筆記本中查看我的代碼:


uj5u.com熱心網友回復:
我做的第一件事就是讓你的影像保持灰度,不需要轉換到 3 個通道來尋找輪廓。其次是將boundary影像轉換為二進制,使其與skeleton影像相同。然后我簡單地添加了這兩個來獲取both影像。
然后,我對組合影像的每一行(當您正在尋找垂直距離)進行計時,并尋找邊界或骨架像素both的元素。True在這一點上,我做了一個簡化的假設——我只搜索了一個邊界后跟一個骨架像素,然后是第二個邊界的情況,我理解這可能并非總是如此,但我把那個特別頭疼的事情留給你整理。
之后,它只是跟蹤您逐行瀏覽影像時記錄的最大和最小距離的情況。(編輯:可能有比我做的更清潔的方法,但希望你明白)
import numpy as np
import matplotlib.pyplot as plt
import cv2
from skimage import filters
from skimage import morphology
def get_skeleton(image_path):
im = cv2.imread(image_path , cv2.IMREAD_GRAYSCALE)
binary = im > filters.threshold_otsu(im)
skeleton = morphology.skeletonize(binary)
return skeleton
def get_boundary(image_path):
reading_Img = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)
canny_Img = cv2.Canny(reading_Img, 90, 100)
contours,_ = cv2.findContours(canny_Img,cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
canvas = np.zeros_like(reading_Img)
boundary = cv2.drawContours(canvas, contours, -1, (255, 0, 0), 1)
binary = boundary > filters.threshold_otsu(boundary)
return binary
skeleton = get_skeleton("LtqlM.png")
boundary = get_boundary("LtqlM.png")
both = skeleton boundary
max_dist = 0
min_dist = 100000
for idx in range(both.shape[0]): # counting through rows
row = both[idx, :]
lines = np.where(row==True)[0]
if len(lines) == 3:
dist_1 = lines[1] - lines[0]
dist_2 = lines[2] - lines[1]
if (dist_1 > dist_2) and dist_1 > max_dist:
max_dist = dist_1
if (dist_2 > dist_1) and dist_2 > max_dist:
max_dist = dist_2
if (dist_1 < dist_2) and dist_1 < min_dist:
min_dist = dist_1
if (dist_2 < dist_1) and dist_2 < min_dist:
min_dist = dist_2
print("Maximum distance = ", max_dist)
print("Minimum distance = ", min_dist)
plt.imshow(both)
uj5u.com熱心網友回復:
從距離最大的像素開始,您可以探索同心方形圖層,直到找到背景像素。然后在最后一層找到歐氏距離最短的背景像素。第二個端點是對稱的。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/462350.html
上一篇:如何去除貼片邊緣的黑點?
