原始問題
我有大約 80-100 張影像,例如(A)。每個影像都由在 ImageJ 中標記輪廓后填充黑色的形狀組成。我需要像(B)中那樣提取每個單獨的形狀。然后我需要找到最長軸的角度,如(C) 所示。

然后我需要計算黑色形狀內常規點的所有可能寬度,并回傳一個包含這些值的 .csv 檔案。
我一直在手動執行此操作,并且必須有更快的方法來執行此操作。知道我該怎么做嗎?
部分解決方案
根據 Epsi 的回答,解決方案(部分):
import matplotlib.pyplot as plt
import cv2
import numpy as np
image = cv2.imread("Image.tif")
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV cv2.THRESH_OTSU)[1]
# Find contours, find rotated rectangle, obtain four verticies, and draw
cnts = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) > 0 else cnts[1]
rect = cv2.minAreaRect(cnts[0])
box = np.int0(cv2.boxPoints(rect))
image_contours = np.zeros(image.shape)
# draw the contours on the empty image
cv2.drawContours(image, [box], 0, (36,255,12), 3)
plt.imshow(image)
上面的代碼給了我以下結果D。我需要更多幫助,如何獲取代碼來計算所有輪廓的矩形,而不僅僅是一個?

uj5u.com熱心網友回復:
由于我對 python 不是很好,所以我不會發布代碼,但會發布解釋和紀錄片的鏈接。
我會怎么做如下:
反轉影像,使所有白色變為黑色,黑色變為白色。https://www.delftstack.com/howto/python/opencv-invert-image/#invert-images-using-numpy.invert-method-in-python
使用 findContours 查找所有(現在)白色輪廓。https://pythonexamples.org/python-opencv-cv2-find-contours-in-image/
使用 minAreaRect 在輪廓周圍創建邊界框,以使框的區域盡可能小。https://theailearner.com/tag/cv2-minarearect/
從創建的邊界框中,取最長的邊,這將代表輪廓的長度。
您還可以從邊界框獲取旋轉角度
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/462348.html
標籤:Python 麻木的 opencv matplotlib 图像处理
上一篇:標準化像素后如何保存影像?
下一篇:如何去除貼片邊緣的黑點?
