日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python中label组件参数_python中连接的组件标签

發布時間:2024/9/19 python 47 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python中label组件参数_python中连接的组件标签 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

How to implement connected component labeling in python with open cv?

This is an image example:

I need connected component labeling to separate objects on a black and white image.

解決方案

The OpenCV connectedComponents() docs don't mention Python but it actually is implemented. See for e.g. this SO question.

The function call is simple: retval, labels = cv2.connectedComponents(img) and you can specify a parameter connectivity to check for 4- or 8-way (default) connectivity. The difference is that 4-way connectivity just checks the top, bottom, left, and right pixels and sees if they connect; 8-way checks if any of the eight neighboring pixels connect. If you have diagonal connections (like you do here) you should specify connectivity=8. Note that it just numbers each component and gives them increasing integer labels starting at 0. So all the zeros are connected, all the ones are connected, etc. If you want to visualize them, you can map those numbers to specific colors. I like to map them to different hues, combine them into an HSV image, and then convert to BGR to display. Here's an example with your image:

import cv2

import numpy as np

img = cv2.imread('eGaIy.jpg', 0)

img = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY)[1] # ensure binary

ret, labels = cv2.connectedComponents(img)

# Map component labels to hue val

label_hue = np.uint8(179*labels/np.max(labels))

blank_ch = 255*np.ones_like(label_hue)

labeled_img = cv2.merge([label_hue, blank_ch, blank_ch])

# cvt to BGR for display

labeled_img = cv2.cvtColor(labeled_img, cv2.COLOR_HSV2BGR)

# set bg label to black

labeled_img[label_hue==0] = 0

cv2.imshow('labeled.png', labeled_img)

cv2.waitKey()

總結

以上是生活随笔為你收集整理的python中label组件参数_python中连接的组件标签的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。