Python opencv醫(yī)學(xué)處理的實(shí)現(xiàn)過程
題目描述
利用opencv或其他工具編寫程序?qū)崿F(xiàn)醫(yī)學(xué)處理。
實(shí)現(xiàn)過程
# -*- coding: utf-8 -*-
'''
作者 : 丁毅
開發(fā)時(shí)間 : 2021/5/9 16:30
'''
import cv2
import numpy as np
# 圖像細(xì)化
def VThin(image, array):
rows, cols = image.shape
NEXT = 1
for i in range(rows):
for j in range(cols):
if NEXT == 0:
NEXT = 1
else:
M = int(image[i, j - 1]) + int(image[i, j]) + int(image[i, j + 1]) if 0 < j < cols - 1 else 1
if image[i, j] == 0 and M != 0:
a = [0]*9
for k in range(3):
for l in range(3):
if -1 < (i - 1 + k) < rows and -1 < (j - 1 + l) < cols and image[i - 1 + k, j - 1 + l] == 255:
a[k * 3 + l] = 1
sum = a[0] * 1 + a[1] * 2 + a[2] * 4 + a[3] * 8 + a[5] * 16 + a[6] * 32 + a[7] * 64 + a[8] * 128
image[i, j] = array[sum]*255
if array[sum] == 1:
NEXT = 0
return image
def HThin(image, array):
rows, cols = image.shape
NEXT = 1
for j in range(cols):
for i in range(rows):
if NEXT == 0:
NEXT = 1
else:
M = int(image[i-1, j]) + int(image[i, j]) + int(image[i+1, j]) if 0 < i < rows-1 else 1
if image[i, j] == 0 and M != 0:
a = [0]*9
for k in range(3):
for l in range(3):
if -1 < (i-1+k) < rows and -1 < (j-1+l) < cols and image[i-1+k, j-1+l] == 255:
a[k*3+l] = 1
sum = a[0]*1+a[1]*2+a[2]*4+a[3]*8+a[5]*16+a[6]*32+a[7]*64+a[8]*128
image[i, j] = array[sum]*255
if array[sum] == 1:
NEXT = 0
return image
array = [0, 0, 1, 1, 0, 0, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1,\
1, 1, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1,\
0, 0, 1, 1, 0, 0, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1,\
1, 1, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1,\
1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\
1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1,\
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\
0, 0, 1, 1, 0, 0, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1,\
1, 1, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1,\
0, 0, 1, 1, 0, 0, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1,\
1, 1, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0,\
1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\
1, 1, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0,\
1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 0,\
1, 1, 0, 0, 1, 1, 1, 0, 1, 1, 0, 0, 1, 0, 0, 0]
# 顯示灰度圖
img = cv2.imread(r"C:\Users\pc\Desktop\vas0.png",0)
cv2.imshow("img1",img)
# 自適應(yīng)閾值分割
img2 = cv2.adaptiveThreshold(img, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 17, 4)
cv2.imshow('img2', img2)
# 圖像反色
img3 = cv2.bitwise_not(img2)
cv2.imshow("img3", img3)
# 圖像擴(kuò)展
img4 = cv2.copyMakeBorder(img3, 1, 1, 1, 1, cv2.BORDER_REFLECT)
cv2.imshow("img4", img4)
contours, hierarchy = cv2.findContours(img4, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
# 消除小面積
img5 = img4
for i in range(len(contours)):
area = cv2.contourArea(contours[i])
if (area < 80) | (area > 10000):
cv2.drawContours(img5, [contours[i]], 0, 0, -1)
cv2.imshow("img5", img5)
num_labels, labels, stats, centroids = cv2.connectedComponentsWithStats(img5, connectivity=8, ltype=None)
# print(stats)
s = sum(stats)
img6 = np.ones(img5.shape, np.uint8) * 0
for (i, label) in enumerate(np.unique(labels)):
# 如果是背景,忽略
if label == 0:
# print("[INFO] label: 0 (background)")
continue
numPixels = stats[i][-1]
div = (stats[i][4]) / s[4]
# print(div)
# 判斷區(qū)域是否滿足面積要求
if round(div, 3) > 0.002:
color = 255
img6[labels == label] = color
cv2.imshow("img6", img6)
# 圖像反色
img7 = cv2.bitwise_not(img6)
# 圖像細(xì)化
for i in range(10):
VThin(img7, array)
HThin(img7, array)
cv2.imshow("img7",img7)
# 邊緣檢測
img8 = cv2.Canny(img6, 80, 255)
cv2.imshow("img8", img8)
# 使灰度圖黑白顛倒
img9 = cv2.bitwise_not(img8)
cv2.imshow("img9", img9)
cv2.waitKey(0)
運(yùn)行結(jié)果









問題及解決方法
1.自適應(yīng)閾值處理運(yùn)行報(bào)錯(cuò)
參考鏈接
解決方式:
void adaptiveThreshold(InputArray src, OutputArray dst, double
maxValue, int adaptiveMethod, int thresholdType, int bolckSize, double C)
src:InputArray類型的src,輸入圖像,填單通道,單8位浮點(diǎn)類型Mat即可。dst:函數(shù)運(yùn)算后的結(jié)果存放在這。即為輸出圖像(與輸入圖像同樣的尺寸和類型)。maxValue:預(yù)設(shè)滿足條件的最大值。adaptiveMethod自適應(yīng)閾值算法。ADAPTIVE_THRESH_MEAN_C或ADAPTIVE_THRESH_GAUSSIAN_C兩種。thresholdType:指定閾值類型??蛇x擇THRESH_BINARY或者THRESH_BINARY_INV兩種(即二進(jìn)制閾值或反二進(jìn)制閾值)。bolckSize:表示鄰域塊大小,用來計(jì)算區(qū)域閾值,一般選擇為3、5、7......等。C:參數(shù)C表示與算法有關(guān)的參數(shù),它是一個(gè)從均值或加權(quán)均值提取的常數(shù),可以是負(fù)數(shù)。- 根據(jù)報(bào)錯(cuò)提示及參數(shù)解釋,
blockSize的取值需要大于1且為奇數(shù)。
2.圖像擴(kuò)展
參考鏈接
方式:使用cv2.copyMakeBorder()函數(shù)。
主要參數(shù):
src: 輸入的圖片。top, bottom, left, right:相應(yīng)方向上的邊框?qū)挾取?/li>borderType:定義要添加邊框的類型,詳情參考鏈接。
3.面積選擇
參考鏈接
方式:選擇滿足面積80-10000的圖像輸出, 去除噪聲位置元素。
4.圖像細(xì)化
參考鏈接
方式:經(jīng)過一層層的剝離,從原來的圖中去掉一些點(diǎn),但仍要保持原來的形狀,直到得到圖像的骨架。骨架,可以理解為圖像的中軸。
到此這篇關(guān)于Python opencv醫(yī)學(xué)處理的實(shí)現(xiàn)過程的文章就介紹到這了,更多相關(guān)Python opencv醫(yī)學(xué)處理內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
用Python 爬取貓眼電影數(shù)據(jù)分析《無名之輩》
這篇文章主要介紹了用Python 爬取貓眼電影數(shù)據(jù)分析《無名之輩》,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07
python實(shí)現(xiàn)讀取excel寫入mysql的小工具詳解
EXCEL 和 MySQL 大體上來說都可以算是"數(shù)據(jù)庫",MySQL貌似有EXCEL的接口,但是最近在自學(xué)Python,用Python實(shí)現(xiàn)了一下,下面這篇文章主要給大家介紹了關(guān)于利用python實(shí)現(xiàn)讀取excel寫入mysql的一個(gè)小工具,需要的朋友可以參考下。2017-11-11
Python實(shí)現(xiàn)Linux監(jiān)控的方法
本文通過實(shí)例代碼給大家介紹了Python實(shí)現(xiàn)Linux監(jiān)控的方法,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-05-05
TensorFlow實(shí)現(xiàn)創(chuàng)建分類器
這篇文章主要為大家詳細(xì)介紹了TensorFlow實(shí)現(xiàn)創(chuàng)建分類器,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-02-02

