我在 Python 方面的經驗為零,但我需要將它用作我專案的資料處理步驟。我正在處理葡萄園的無人機熱影像,目的是根據海拔和溫度將樹冠像素與地面像素分開。使用 DEM(數字高程模型),首先區分了樹冠和地面。這會產生一個二進制掩膜層,可以放在葡萄園的熱影像之上。這樣,我現在在 Python 中有一個熱影像,其中大部分地面像素為 0,而樹冠像素的值介于 0 和 65535 之間,表示溫度。然而,由于第一個區分(使用 DEM)不夠精確,一些地面像素也包含在冠層掩膜中。
現在我想使用所選區域的溫度進行第二次區分。我能夠使用opencv制作所有冠層區域的輪廓(所以我有一個代表冠層區域的所有輪廓的完整串列 - 帶有一些地面像素)。我的目標是制作每個輪廓區域的直方圖,顯示該區域內每個像素值的密度。希望我可以洗掉太熱的像素(即groundpixels)。
有誰知道如何為影像的每個(填充)輪廓生成直方圖?現在的格式是 6082x4922 ndarray,其值介于 0 和 65535 之間,資料型別為 uint16。我使用 PyCharm 作為 IDE。
提前致謝!
uj5u.com熱心網友回復:
方法:
- 遍歷掩碼中的每個輪廓
- 查找掩碼中存在的像素位置
- 在給定的影像中找到它們對應的值
- 計算并繪制直方圖
代碼:
# reading given image in grayscale
img = cv2.imread('apples.jpg', 0)

# reading the mask in grayscale
img_mask = cv2.imread(r'apples_mask.jpg', 0)
# binarizing the mask
mask = cv2.threshold(img_mask,0,255,cv2.THRESH_BINARY cv2.THRESH_OTSU)[1]

# find contours on the mask
contours, hierarchy = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
# the approach is encoded within this loop
for i, c in enumerate(contours):
# create a blank image of same shape
black = np.full(shape = im.shape, fill_value = 0, dtype = np.uint8)
# draw a single contour as a mask
single_object_mask = cv2.drawContours(black,[c],0,255, -1)
# coordinates containing white pixels of mask
coords = np.where(single_object_mask == 255)
# pixel intensities present within the image at these locations
pixels = img[coords]
# plot histogram
plt.hist(pixels,256,[0,256])
plt.savefig('apples_histogram_{}.jpg'.format(i)')
# to avoid plotting in the same plot
plt.clf()
結果:(以下是3個直方圖)



如果洗掉
plt.clf(),所有直方圖都將繪制在一個圖上
您可以為您的用例擴展相同的方法
原圖:

轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/492810.html
上一篇:計算影像兩半的像素比
