我試圖通過分析它們的輪廓來對拼圖碎片的型別(頭數,如果它是邊界或角......)進行分類。
我試圖遵循的方法是分析這種型別的圖(來自
我試過:
import cv2
import matplotlib.pyplot as plt
def cart2pol(x, y):
rho = np.sqrt(x**2 y**2)
phi = np.arctan2(y, x)
return(rho, phi)
# load image and find contours
img = cv2.imread("example.png", cv2.IMREAD_GRAYSCALE)
contours, _ = cv2.findContours(img, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
# get contour points in polar coordinates
rhos = []
for i in range(len(contours[0])):
x, y = contours[0][i][0]
rho, _ = cart2pol(x, y)
rhos.append(rho)
plt.show()
plt.plot(rhos)
但這會產生不同的情節,如下所示:
從這張圖片:
在其他影像上嘗試這個,我可以看到峰和谷如何對應于碎片的頭部和孔,但我想要一個像上面那樣的圖(不是我所看到的正確的函式)。你能幫我弄到那個嗎?
uj5u.com熱心網友回復:
找到瓦片的中心:
M = cv2.moments(contours[0])
cx = int(M["m10"] / M["m00"])
cy = int(M["m01"] / M["m00"])
計算從中心到輪廓上點的向量,并將向量轉換為極坐標:
ds, phis = [], []
for i in range(len(contours[0])):
x, y = contours[0][i][0]
d, rho = cart2pol(x-cx, y-cy)
ds.append(d)
phis.append(rho)
用 x 軸上的角度和 y 軸上的距離繪制極坐標:
plt.plot(phis, ds)
完整示例:
import os
os.chdir(os.path.abspath(os.path.dirname(__file__)))
import cv2
import matplotlib.pyplot as plt
import numpy as np
def cart2pol(x, y):
rho = np.sqrt(x**2 y**2)
phi = np.arctan2(y, x)
return (rho, phi)
img = cv2.imread('opencv_so_9_example.png', cv2.IMREAD_GRAYSCALE)
contours, _ = cv2.findContours(img, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
M = cv2.moments(contours[0])
cx = int(M["m10"] / M["m00"])
cy = int(M["m01"] / M["m00"])
polar = [cart2pol(c[0][0] - cx, c[0][1] - cy) for c in contours[0][:]]
max_i = polar.index(max(polar, key = lambda x: x[1]))
polar = polar[max_i:] polar[:max_i]
ds, phis = zip(*polar)
plt.gcf().set_size_inches(6, 3)
plt.plot(phis, ds)
plt.show()
uj5u.com熱心網友回復:
也可以使用 Python/OpenCV 中的二進制影像和 warpPolar 來做到這一點。
輸入:
import cv2
import numpy as np
import math
# read image
img = cv2.imread('puzzle.png')
ht, wd = img.shape[:2]
# convert to grayscale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# threshold
thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY cv2.THRESH_OTSU)[1]
# get non-zero points (i.e. white)
points = np.column_stack(np.where(thresh.transpose() > 0))
# get centroid
M = cv2.moments(points)
cx = int(M["m10"] / M["m00"])
cy = int(M["m01"] / M["m00"])
# compute maxradius = furthest corner - center
maxrad = math.sqrt( (wd-cx)*(wd-cx) (ht-cy)*(ht-cy) )
# convert to polar image relative to centroid
polar1 = cv2.warpPolar(thresh, (ht,wd), (cx,cy), maxrad, cv2.INTER_CUBIC cv2.WARP_POLAR_LINEAR cv2.WARP_FILL_OUTLIERS)
# rotate 270 clocwise
polar2 = cv2.rotate(polar1, cv2.ROTATE_90_COUNTERCLOCKWISE)
ht, wd = polar2.shape[:2]
# save image
cv2.imwrite('puzzle_polar1.png',polar1)
cv2.imwrite('puzzle_polar2.png',polar2)
# show the images
cv2.imshow("polar1", polar1)
cv2.imshow("polar2", polar2)
cv2.waitKey(0)
cv2.destroyAllWindows()
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/429503.html
下一篇:Mediapipe裁剪影像