显微镜粒径

时间:2019-03-27 13:14:38

标签: python opencv image-segmentation

嗨,我尝试从这张图片中检测出颗粒长度大小enter image description here

,我想得到粒子的长度尺寸,如下所示。并获得粒子大小的直方图

enter image description here

首先,我阈值并绘制轮廓。我得到的是这样的...我尝试了许多种方法来阈值化,但是与其他材料结合的粒子是错误的读数。我尝试分水岭,但似乎失败了。我尝试使用Kmeans并对K均值进行分水岭,但我不知道该怎么做。有什么建议我应该怎么做才能获得粒径。 enter image description here

这是我的代码

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()

0 个答案:

没有答案