人妖在线一区,国产日韩欧美一区二区综合在线,国产啪精品视频网站免费,欧美内射深插日本少妇

新聞動態(tài)

Python 深入了解opencv圖像分割算法

發(fā)布日期:2021-12-09 18:30 | 文章來源:腳本之家

本文主要是基于Python Opencv 實現(xiàn)的圖像分割,其中使用到的opencv的函數(shù)有:

  • 使用 OpenCV 函數(shù) cv::filter2D 執(zhí)行一些拉普拉斯濾波以進行圖像銳化
  • 使用 OpenCV 函數(shù) cv::distanceTransform 以獲得二值圖像的派生(derived)表示,其中每個像素的值被替換為其到最近背景像素的距離
  • 使用 OpenCV 函數(shù) cv::watershed 將圖像中的對象與背景隔離

加載源圖像并檢查它是否加載沒有任何問題,然后顯示它:

# Load the image
parser = argparse.ArgumentParser(description='Code for Image Segmentation with Distance Transform and Watershed Algorithm.\
 Sample code showing how to segment overlapping objects using Laplacian filtering, \
 in addition to Watershed and Distance Transformation')
parser.add_argument('--input', help='Path to input image.', default='cards.png')
args = parser.parse_args()
src = cv.imread(cv.samples.findFile(args.input))
if src is None:
 print('Could not open or find the image:', args.input)
 exit(0)
# Show source image
cv.imshow('Source Image', src)

原圖

將背景從白色更改為黑色,因為這將有助于稍后在使用距離變換(Distance Transform)期間提取更好的結(jié)果

src[np.all(src == 255, axis=2)] = 0

如果不太理解numpy.all的的用法,可以參考這里

之后,我們將銳化(sharpen)我們的圖像,以銳化前景對象(the foreground objects)的邊緣。 我們將應(yīng)用具有相當(dāng)強過濾器的拉普拉斯(laplacian)過濾器(二階導(dǎo)數(shù)的近似值):

# 創(chuàng)建一個內(nèi)核,我們將用它來銳化我們的圖像 
# 一個二階導(dǎo)數(shù)的近似值,一個非常強大的內(nèi)核
kernel = np.array([[1, 1, 1], [1, -8, 1], [1, 1, 1]], dtype=np.float32)
# do the laplacian filtering as it is
# well, we need to convert everything in something more deeper then CV_8U
# because the kernel has some negative values,
# and we can expect in general to have a Laplacian image with negative values
# BUT a 8bits unsigned int (the one we are working with) can contain values from 0 to 255
# so the possible negative number will be truncated
imgLaplacian = cv.filter2D(src, cv.CV_32F, kernel)
sharp = np.float32(src)
imgResult = sharp - imgLaplacian
# convert back to 8bits gray scale
imgResult = np.clip(imgResult, 0, 255)
imgResult = imgResult.astype('uint8')
imgLaplacian = np.clip(imgLaplacian, 0, 255)
imgLaplacian = np.uint8(imgLaplacian)
#cv.imshow('Laplace Filtered Image', imgLaplacian)
cv.imshow('New Sharped Image', imgResult)

銳化處理的主要目的是突出灰度的過度部分。由于拉普拉斯是一種微分算子,如果所使用的定義具有負的中心系數(shù),那么必須將原圖像減去經(jīng)拉普拉斯變換后的圖像,而不是加上它,從而得到銳化結(jié)果。----摘自《數(shù)字圖像處理(第三版)》

現(xiàn)在我們將新的銳化源圖像分別轉(zhuǎn)換為灰度和二值圖像(binary):

# Create binary image from source image
bw = cv.cvtColor(imgResult, cv.COLOR_BGR2GRAY)
_, bw = cv.threshold(bw, 40, 255, cv.THRESH_BINARY | cv.THRESH_OTSU)
cv.imshow('Binary Image', bw)

我們現(xiàn)在準備在二值圖像(binary image)上應(yīng)用距離變換。 此外,我們對輸出圖像進行歸一化,以便能夠?qū)Y(jié)果進行可視化和閾值處理:

# Perform the distance transform algorithm
dist = cv.distanceTransform(bw, cv.DIST_L2, 3)
# 對范圍 = {0.0, 1.0} 的距離圖像(the distance image)進行歸一化(Normalize),
# 以便我們可以對其進行可視化和閾值處理
cv.normalize(dist, dist, 0, 1.0, cv.NORM_MINMAX)
cv.imshow('Distance Transform Image', dist)

distanceTransform用法

cv.distanceTransform( src, distanceType, maskSize[, dst[, dstType]] )
src:輸入圖像,數(shù)據(jù)類型為CV_8U的單通道圖像
dst: 輸出圖像,與輸入圖像具有相同的尺寸,數(shù)據(jù)類型為CV_8U或者CV_32F的單通道圖像。
distanceType:選擇計算兩個像素之間距離方法的標志,其常用的距離度量方法, DIST_L1(distance = |x1-x2| + |y1-y2| 街區(qū)距離), DIST_L2 (Euclidean distance 歐幾里得距離,歐式距離) 。
maskSize:距離變換掩碼矩陣的大小,參數(shù)可以選擇的尺寸為DIST_MASK_3(3×3)和DIST_MASK_5(5×5).

我們對 dist 圖像進行閾值處理,然后執(zhí)行一些形態(tài)學(xué)操作(即膨脹)以從上述圖像中提取峰值:

# Threshold to obtain the peaks
# This will be the markers for the foreground objects
_, dist = cv.threshold(dist, 0.4, 1.0, cv.THRESH_BINARY)
# Dilate a bit the dist image
kernel1 = np.ones((3,3), dtype=np.uint8)
dist = cv.dilate(dist, kernel1)
cv.imshow('Peaks', dist)

從每個 blob 中,我們在 cv::findContours 函數(shù)的幫助下為分水嶺算法創(chuàng)建一個種子/標記:

# Create the CV_8U version of the distance image
# It is needed for findContours()
dist_8u = dist.astype('uint8')
# Find total markers
contours, _ = cv.findContours(dist_8u, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE)
# Create the marker image for the watershed algorithm
markers = np.zeros(dist.shape, dtype=np.int32)
# Draw the foreground markers
for i in range(len(contours)):
 cv.drawContours(markers, contours, i, (i+1), -1)
# Draw the background marker
cv.circle(markers, (5,5), 3, (255,255,255), -1)
markers_8u = (markers * 10).astype('uint8')
cv.imshow('Markers', markers_8u)

最后,我們可以應(yīng)用分水嶺算法,并將結(jié)果可視化:

# Perform the watershed algorithm
cv.watershed(imgResult, markers)
#mark = np.zeros(markers.shape, dtype=np.uint8)
mark = markers.astype('uint8')
mark = cv.bitwise_not(mark)
# uncomment this if you want to see how the mark
# image looks like at that point
#cv.imshow('Markers_v2', mark)
# Generate random colors
colors = []
for contour in contours:
 colors.append((rng.randint(0,256), rng.randint(0,256), rng.randint(0,256)))
# Create the result image
dst = np.zeros((markers.shape[0], markers.shape[1], 3), dtype=np.uint8)
# Fill labeled objects with random colors
for i in range(markers.shape[0]):
 for j in range(markers.shape[1]):
  index = markers[i,j]
  if index > 0 and index <= len(contours):
dst[i,j,:] = colors[index-1]
# Visualize the final image
cv.imshow('Final Result', dst)

代碼

基于機器學(xué)習(xí)的圖像分割

Pixellib是一個用于對圖像和視頻中的對象進行分割的庫。 它支持兩種主要類型的圖像分割:

1.語義分割
2.實例分割
PixelLib 支持兩個用于圖像分割的深度學(xué)習(xí)庫,分別是 Pytorch 和 Tensorflow

以上就是Python 深入了解opencv圖像分割算法的詳細內(nèi)容,更多關(guān)于Python的資料請關(guān)注本站其它相關(guān)文章!

版權(quán)聲明:本站文章來源標注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請保持原文完整并注明來源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學(xué)習(xí)參考,不代表本站立場,如有內(nèi)容涉嫌侵權(quán),請聯(lián)系alex-e#qq.com處理。

相關(guān)文章

實時開通

自選配置、實時開通

免備案

全球線路精選!

全天候客戶服務(wù)

7x24全年不間斷在線

專屬顧問服務(wù)

1對1客戶咨詢顧問

在線
客服

在線客服:7*24小時在線

客服
熱線

400-630-3752
7*24小時客服服務(wù)熱線

關(guān)注
微信

關(guān)注官方微信
頂部