Привет. Я пытаюсь определить размер частиц по длине этого изображения
, и я хотел бы получить размер частиц по длине, как показано ниже.и получить гистограмму размера частицы
Первый I порог и нарисовать контур.И то, что я получаю, похоже на это ... Я пробовал так много путей к порогу, но частица, которая объединяется с другим материалом, является ложным чтением.Я пытаюсь переломить, но, кажется, не удалосьЯ пытаюсь использовать Kmeans и переломить K Means, но я не знаю, как это сделать.Есть ли какие-либо предположения, что я должен сделать, чтобы получить размер частиц ..
Вот мой код
from __future__ import print_function
from imutils import perspective
from imutils import contours
import numpy as np
import imutils
import cv2
import scalar
import sys
from scipy.spatial import distance as dist
import scipy.ndimage as ndi
import matplotlib.pyplot as plt
from skimage import filters
from sklearn import cluster
from scipy import ndimage
from skimage.feature import peak_local_max
from skimage.morphology import watershed
from scipy.ndimage import label
img = cv2.imread("C:\\Users\\adiyu\Pictures\\snipping\\c3smeasure.png",
cv2.IMREAD_COLOR)
kernel = np.array([[-1,-1,-1],[-1,9,-1], [-1,-1,-1]])
kernel2 = np.ones((3,3),np.uint8)
img=cv2.filter2D(img,-255, kernel)
img = cv2.medianBlur(img,3)
img_np = np.array(img)
img_np_rgb = cv2.cvtColor(img_np,cv2.COLOR_RGBA2RGB)
#Kmeans cluster using Opencv
Z = img.reshape((-1,3))
# convert to np.float32
Z = np.float32(Z)
# define criteria, number of clusters(K) and apply kmeans()
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 10, 1.0)
K = 3
ret,label,center=cv2.kmeans(Z,K,None,criteria,10,cv2.KMEANS_PP_CENTERS)
# Now convert back into uint8, and make original image
center = np.uint8(center)
res = center[label.flatten()]
res2 = res.reshape((img.shape))
gray= cv2.cvtColor(res2,cv2.COLOR_BGR2GRAY)
gray = cv2.fastNlMeansDenoising(gray, None, 6,7,21)
cv2.imshow('res2',res2)
cv2.imshow('gray',gray)
thresh = cv2.threshold(gray, 100, 255, cv2.THRESH_BINARY_INV +
cv2.THRESH_OTSU + cv2.THRESH_OTSU)[1]
cv2.imshow('thresh',thresh)
opening = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, kernel, iterations=2)
cv2.imshow("opening", opening)
closing = cv2.morphologyEx(opening, cv2.MORPH_CLOSE,kernel2, iterations=2)
cv2.imshow("closing", closing)
fg = cv2.erode(closing,None,iterations = 2)
cv2.imshow("fg", fg)
bgt = cv2.dilate(closing,None,iterations = 3)
ret,bg = cv2.threshold(bgt,1,128,1)
cv2.imshow("bg", bg)
marker = cv2.add(fg,bg)
cv2.imshow("marker", marker)
#Then we convert it into 32SC1 :
marker32 = np.int32(marker)
# Finally we apply watershed and convert result back into uint8 image:
cv2.watershed(img,marker32)
m = cv2.convertScaleAbs(marker32)
cv2.imshow("m", m)
ret,thresh = cv2.threshold(m,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
res = cv2.bitwise_and(img,img,mask = thresh)
cv2.imshow("res", res)
#plt.figure(figsize=(35,35)
#plt.imshow(cv2.morphologyEx(img_np_rgb,cv2.MORPH_CLOSE,np.ones((2,2))
,iterations=10))
#model = cluster.KMeans(3)
#plt.figure(figsize=35,35))
#aa=model.fit_predict(img_np_rgb.reshape((-1,3)))
.reshape(img_np_rgb.shape[:2])
#plt.imshow(gray)
#plt.show()
#-------Draw Contour------
c, h= cv2.findContours(opening, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
for i in c:
area = cv2.contourArea(i)
if area > 10:
epsilon = 0.00001*cv2.arcLength(i, True)
approx = cv2.approxPolyDP(i, epsilon, True)
img2 =cv2.drawContours(img,[approx], -1, (0,255,0), 2)
cv2.imshow("img2", img2)
cv2.waitKey(0)
cv2.destroyAllWindows()