使用Python和OpenCV進(jìn)行視覺圖像分割的代碼示例
環(huán)境準(zhǔn)備
在開始之前,請確保你的開發(fā)環(huán)境中已經(jīng)安裝了Python、NumPy、Matplotlib以及OpenCV(即skimage和io模塊)。這些庫可以通過pip進(jìn)行安裝。
項(xiàng)目步驟

一:選取樣本區(qū)域
首先,我們從圖像中選取一個(gè)區(qū)域作為樣本。在這個(gè)例子中,我們選擇了圖像中心的一個(gè)正方形區(qū)域。
from skimage import data, io
import numpy as np
# 讀取圖像
image = io.imread('flower.jpg')
# 選取樣本區(qū)域
height, width, _ = image.shape
roi_size = 100 # 樣本區(qū)域的邊長
roi_center_x = width // 2
roi_center_y = height // 2
roi = image[roi_center_y - roi_size//2:roi_center_y + roi_size//2,
roi_center_x - roi_size//2:roi_center_x + roi_size//2]二:計(jì)算標(biāo)準(zhǔn)差
接下來,我們計(jì)算所選區(qū)域紅色通道的標(biāo)準(zhǔn)差,這將用于后續(xù)的圖像分割。
# 提取紅色通道并計(jì)算標(biāo)準(zhǔn)差 red_channel = roi[:, :, 0] mean_value = np.mean(red_channel) std_dev = np.std(red_channel)
三:建立模板圖像空間
基于計(jì)算出的標(biāo)準(zhǔn)差,我們建立一個(gè)模板圖像空間,用于區(qū)分圖像中的不同區(qū)域。
# 建立模板圖像空間
template_image = np.zeros_like(image, dtype='uint8')
for y in range(height):
for x in range(width):
if abs(image[y, x, 0] - mean_value) <= std_dev:
template_image[y, x] = image[y, x]
else:
template_image[y, x] = [0, 0, 0] # 將不符合條件的像素設(shè)置為黑色四:根據(jù)模板圖像空間分割原圖像
最后,我們使用模板圖像空間來分割原始圖像,得到最終的分割結(jié)果。
# 分割原圖像 segmented_image = np.where(template_image != 0, image, 0)
顯示結(jié)果
使用Matplotlib庫,我們可以將原始圖像、模板圖像以及分割后的圖像展示出來,以便進(jìn)行比較。
from matplotlib import pyplot as plt
# 顯示結(jié)果
plt.figure(figsize=(12, 6))
plt.subplot(1, 3, 1)
plt.title('Original Image')
plt.imshow(image)
plt.axis('off')
plt.subplot(1, 3, 2)
plt.title('Template Image')
plt.imshow(template_image)
plt.axis('off')
plt.subplot(1, 3, 3)
plt.title('Segmented Image')
plt.imshow(segmented_image)
plt.axis('off')
plt.show()完整代碼
from skimage import data, io
from matplotlib import pyplot as plt
import numpy as np
import math
# 讀取圖像
image = io.imread(r'flower.jpg')
# 一:選取樣本區(qū)域
# 假設(shè)我們選取圖像中心的一個(gè)正方形區(qū)域作為樣本區(qū)域
height, width, _ = image.shape
roi_size = 100 # 樣本區(qū)域的邊長
roi_center_x = width // 2
roi_center_y = height // 2
roi = image[roi_center_y - roi_size//2:roi_center_y + roi_size//2,
roi_center_x - roi_size//2:roi_center_x + roi_size//2]
# 提取紅色通道
red_channel = roi[:, :, 0]
# 二:計(jì)算標(biāo)準(zhǔn)差
mean_value = np.mean(red_channel)
std_dev = np.std(red_channel)
# 三:建立模板圖像空間
r1_d = std_dev
template_image = np.zeros_like(image, dtype='uint8')
for y in range(height):
for x in range(width):
if abs(image[y, x, 0] - mean_value) <= r1_d:
template_image[y, x] = image[y, x]
else:
template_image[y, x] = [0, 0, 0] # 將不符合條件的像素設(shè)置為黑色
# 四:根據(jù)模板圖像空間分割原圖像
segmented_image = np.where(template_image != 0, image, 0)
# 顯示結(jié)果
plt.figure(figsize=(12, 6))
plt.subplot(1, 3, 1)
plt.title('Original Image')
plt.imshow(image)
plt.axis('off')
plt.subplot(1, 3, 2)
plt.title('Template Image')
plt.imshow(template_image)
plt.axis('off')
plt.subplot(1, 3, 3)
plt.title('Segmented Image')
plt.imshow(segmented_image)
plt.axis('off')
plt.show()結(jié)論
通過這個(gè)項(xiàng)目,我們學(xué)習(xí)了如何使用Python和OpenCV進(jìn)行圖像分割。這個(gè)過程涉及到了樣本區(qū)域的選擇、標(biāo)準(zhǔn)差的計(jì)算、模板圖像空間的建立以及最終的圖像分割。這個(gè)技術(shù)在許多領(lǐng)域都有廣泛的應(yīng)用,比如醫(yī)學(xué)影像分析、自動(dòng)駕駛車輛的視覺系統(tǒng)等。希望這個(gè)項(xiàng)目能夠?yàn)槟闾峁┮恍﹩l(fā)和幫助。
以上就是使用Python和OpenCV進(jìn)行視覺圖像分割的代碼示例的詳細(xì)內(nèi)容,更多關(guān)于Python OpenCV視覺圖像分割的資料請關(guān)注腳本之家其它相關(guān)文章!
- OpenCV實(shí)戰(zhàn)記錄之基于分水嶺算法的圖像分割
- Tensorflow2.10實(shí)現(xiàn)圖像分割任務(wù)示例詳解
- Python基于紋理背景和聚類算法實(shí)現(xiàn)圖像分割詳解
- OpenCV利用K-means實(shí)現(xiàn)根據(jù)顏色進(jìn)行圖像分割
- 詳解Python OpenCV圖像分割算法的實(shí)現(xiàn)
- python中的opencv?圖像分割與提取
- Python 深入了解opencv圖像分割算法
- openCV實(shí)現(xiàn)圖像分割
- 計(jì)算機(jī)視覺中圖像分割怎么學(xué)?從入門到實(shí)踐的核心技術(shù)全解析
相關(guān)文章
pytorch實(shí)現(xiàn)從本地加載 .pth 格式模型
今天小編就為大家分享一篇pytorch實(shí)現(xiàn)從本地加載 .pth 格式模型,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-02-02
Python實(shí)現(xiàn)網(wǎng)頁內(nèi)容轉(zhuǎn)純文本與EPUB電子書的完整指南
在信息爆炸的時(shí)代,我們每天都會(huì)瀏覽大量網(wǎng)頁內(nèi)容,本文將通過Python實(shí)現(xiàn)兩種主流保存方案,即純文本格式TXT和電子書標(biāo)準(zhǔn)格式EPUB,感興趣的小伙伴可以了解一下2026-02-02
PHP網(wǎng)頁抓取之抓取百度貼吧郵箱數(shù)據(jù)代碼分享
本文給大家介紹PHP網(wǎng)頁抓取之抓取百度貼吧郵箱數(shù)據(jù)代碼分享,程序?qū)崿F(xiàn)了一鍵抓取帖子全部郵箱和分頁抓取郵箱兩個(gè)功能,感興趣的朋友一起學(xué)習(xí)吧2016-04-04
PyTorch實(shí)現(xiàn)MNIST數(shù)據(jù)集手寫數(shù)字識別詳情
這篇文章主要介紹了PyTorch實(shí)現(xiàn)MNIST數(shù)據(jù)集手寫數(shù)字識別詳情,文章圍繞主題展開詳細(xì)的內(nèi)容戒殺,具有一定的參考價(jià)值,需要的朋友可以參考一下2022-09-09
關(guān)于Python核心框架tornado的異步協(xié)程的2種方法詳解
今天小編就為大家分享一篇關(guān)于Python核心框架tornado的異步協(xié)程的2種方法詳解,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-08-08
python學(xué)習(xí)入門細(xì)節(jié)知識點(diǎn)
我們整理了關(guān)于python入門學(xué)習(xí)的一些細(xì)節(jié)知識點(diǎn),對于學(xué)習(xí)python的初學(xué)者很有用,一起學(xué)習(xí)下。2018-03-03
python關(guān)于倒排列的知識點(diǎn)總結(jié)
在本篇文章里小編給大家分享的是一篇關(guān)于python關(guān)于倒排列的知識點(diǎn)總結(jié),有需要的朋友們可以參考下。2020-10-10

