九色国产,午夜在线视频,新黄色网址,九九色综合,天天做夜夜做久久做狠狠,天天躁夜夜躁狠狠躁2021a,久久不卡一区二区三区

打開APP
userphoto
未登錄

開通VIP,暢享免費(fèi)電子書等14項超值服

開通VIP
三個OpenCV目標(biāo)分割計數(shù)實例(附源碼)

重磅干貨,第一時間送達(dá)

下面實例核心步驟或者算法:
  1.  二值化+形態(tài)學(xué)預(yù)處理
  2. 距離變換
  3. 分水嶺算法
實例一:硬幣分割計數(shù)

上代碼:
# import the necessary packagesfrom skimage.feature import peak_local_maxfrom skimage.morphology import watershedfrom scipy import ndimageimport numpy as npimport argparseimport imutilsimport cv2 # load the image and perform pyramid mean shift filtering# to aid the thresholding stepimage = cv2.imread('1.jpg')shifted = cv2.pyrMeanShiftFiltering(image, 21, 51)cv2.imshow('Input', image) # convert the mean shift image to grayscale, then apply# Otsu's thresholdinggray = cv2.cvtColor(shifted, cv2.COLOR_BGR2GRAY)thresh = cv2.threshold(gray, 0, 255,  cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]cv2.imshow('Thresh', thresh)
# compute the exact Euclidean distance from every binary# pixel to the nearest zero pixel, then find peaks in this# distance mapD = ndimage.distance_transform_edt(thresh)localMax = peak_local_max(D, indices=False, min_distance=10, labels=thresh) # perform a connected component analysis on the local peaks,# using 8-connectivity, then appy the Watershed algorithmmarkers = ndimage.label(localMax, structure=np.ones((3, 3)))[0]labels = watershed(-D, markers, mask=thresh)print('[INFO] {} unique segments found'.format(len(np.unique(labels)) - 1))
# loop over the unique labels returned by the Watershed# algorithmfor label in np.unique(labels): # if the label is zero, we are examining the 'background' # so simply ignore it if label == 0: continue # otherwise, allocate memory for the label region and draw # it on the mask mask = np.zeros(gray.shape, dtype='uint8') mask[labels == label] = 255 # detect contours in the mask and grab the largest one cnts = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) cnts = imutils.grab_contours(cnts) c = max(cnts, key=cv2.contourArea) # draw a circle enclosing the object ((x, y), r) = cv2.minEnclosingCircle(c) cv2.circle(image, (int(x), int(y)), int(r), (0, 255, 0), 2) cv2.putText(image, '{}'.format(label), (int(x) - 10, int(y)), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 0, 255), 2) # show the output imagecv2.imshow('Output', image)cv2.waitKey(0)cv2.destroyAllWindows()
分割計數(shù)結(jié)果:

實例二:藥片分割計數(shù)

代碼同上,可能參數(shù)需略微調(diào)整,上效果:

實例三:玉米粒分割計數(shù)

上代碼
import numpy as npimport cv2from matplotlib import pyplot as pltfont=cv2.FONT_HERSHEY_SIMPLEX
img = cv2.imread('5.jpg')gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)ret, thresh = cv2.threshold(gray,245,255,cv2.THRESH_BINARY)cv2.imshow('threshold', thresh)
k = cv2.getStructuringElement(cv2.MORPH_RECT,(13,13))dilate = cv2.dilate(thresh,k,iterations=3)cv2.imshow('dilate', dilate)
cv2.bitwise_not(dilate, dilate)dist_transform = cv2.distanceTransform(dilate,cv2.DIST_L2,3)dist = cv2.normalize(dist_transform,dist_transform,0,1.0,cv2.NORM_MINMAX)cv2.imshow('distance', dist)cv2.imwrite('dis.jpg', dist)
#dist = np.uint8(dist)dist = cv2.convertScaleAbs(dist)ret2,morph = cv2.threshold(dist,0.99,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)#ret2, morph = cv2.threshold(dist,0,255,cv2.THRESH_BINARY_INV)cv2.imshow('morph', morph)
k2 = cv2.getStructuringElement(cv2.MORPH_RECT,(11,5))sure_fg = cv2.morphologyEx(morph,cv2.MORPH_OPEN,k2, iterations = 1) # 形態(tài)開運(yùn)算
cv2.imshow('result', sure_fg)
thresh,contours,hierarchy = cv2.findContours(sure_fg, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)for i in range(0,len(contours)): (x, y, w, h) = cv2.boundingRect(contours[i]) #cv2.drawContours(img,contours,i,(0,255,0),5) cv2.circle(img,(x+int(w/2),y+int(h/2)),20,(0,0,255),-1, cv2.LINE_AA) cv2.putText(img,str(i+1),(x+int(w/2)-15,y+int(h/2)+5),font,0.8,(0,255,0),2) cv2.imshow('img',img)cv2.waitKey(0)cv2.destroyAllWindows()
分割計數(shù)效果:

中間執(zhí)行結(jié)果略去,大家可以復(fù)制源碼和圖片自己運(yùn)行,查看中間結(jié)果,簡單來做個總結(jié):
  1. 預(yù)處理---基本就是二值化和形態(tài)學(xué)操作
  2. 粘連分割---距離變換少不了
  3. 分水嶺算法---根據(jù)實際情況使用,有時候分割不太可控,慎用
參考資料:
https://www.pyimagesearch.com/2015/11/02/watershed-opencv/
https://www.itread01.com/content/1545201032.html
下載1:OpenCV-Contrib擴(kuò)展模塊中文版教程
本站僅提供存儲服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊舉報。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
簡單人工智能技術(shù)應(yīng)用 使用Python OpenCV進(jìn)行圖像處理
AI 圖像智能修復(fù)老照片,效果驚艷到我了!| 附代碼
還在用PS給證件照換底色嗎?20行代碼教你用Python給證件照換底色
一起來學(xué)opencv(四):形態(tài)轉(zhuǎn)換
halcon算子region
OpenCV-閾值函數(shù)cv::threshold
更多類似文章 >>
生活服務(wù)
熱點(diǎn)新聞
分享 收藏 導(dǎo)長圖 關(guān)注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服