固体颗粒的分水岭分割问题

时间:2019-10-24 11:40:26

标签: python opencv

  • 我对如何检测图像的边界存在问题 土壤颗粒并通过使用以下方法测量这些边界的面积 如下所示的分水岭分割算法,我将其用于 彩色图像,但是我发现了更好的边缘检测 使用整体嵌套边缘检测的算法 结果比精巧的边缘检测。但是,我的主要问题 涉及如何在获得这些锐利边缘后如何使用该图像 粒子直接获取图像中的边界区域 使用python包中的任何内置函数。

enter image description here enter image description here

import cv2
import math
import numpy as np
from skimage.feature import peak_local_max
from skimage.morphology import watershed
from scipy import ndimage
import xlwt
from xlwt import Workbook
wb = Workbook()
sheet1 = wb.add_sheet('Sheet 1')
n = 1
# Load in image, convert to gray scale, and Otsu's threshold
image = cv2.imread('gravels1_out.jpg')
# image = cv2.medianBlur(image, 7)
# kernel = np.ones((5,5),np.float32)/36
# image = cv2.filter2D(image,-1,kernel)
# cv2.imshow('img',image)
# cv2.waitKey()
# exit()
# gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# thresh = cv2.threshold(image, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
areas =[]
# Compute Euclidean distance from every binary pixel
# to the nearest zero pixel then find peaks
distance_map = ndimage.distance_transform_edt(image)
local_max = peak_local_max(distance_map, indices=False, min_distance=20, labels=image)

# Perform connected component analysis then apply Watershed
markers = ndimage.label(local_max, structure=np.ones((3, 3)))[0]
labels = watershed(-distance_map, markers, mask=image)

# Iterate through unique labels
total_area = 0
for label in np.unique(labels):
    if label == 0:
        continue

    # Create a mask
    mask = np.zeros(gray.shape, dtype="uint8")
    mask[labels == label] = 255
    # Find contours and determine contour area
    cnts = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
    cnts = cnts[0] if len(cnts) == 2 else cnts[1]
    c = max(cnts, key=cv2.contourArea)
    area = cv2.contourArea(c)
    if area > 20:
        areas.append(area)
    print('polygon number ', n , 'of area ',area)
    n+=1
    cv2.drawContours(image, [c], -1, (36,255,12), 2)
cal_list=[]
for a in areas:
    b = a/29700
    cal_list.append(b)

row = 1
col = 0
gamma = 1.7
W =[]
cum = 0
for item in cal_list:
    r = 10*((item/math.pi)**0.5)
    v = (4/3)*math.pi*r**3
    w = gamma*v
    W.append(w)
    for c in W:
        cum = cum + c
    sheet1.write(row, col, item)
    sheet1.write(row,col+1,r)
    sheet1.write(row,col+2,v)
    sheet1.write(row, col + 3, w)
    sheet1.write(row, col + 4,cum)
    row+=1

wb.save('SAMPLE A .xls')
print('MAXIMUM VALUE OF AREAS DETECTED ', max(cal_list))
print('MINUMUM VALUE OF AREAS DETECTED ', min(cal_list))
print('Total number of polygons detected = ' , n)
cv2.imshow('image', image)
cv2.waitKey()

0 个答案:

没有答案