App下載

Python opencv醫(yī)學(xué)處理的實現(xiàn)過程

可樂派掌門人 2021-08-20 10:41:29 瀏覽數(shù) (2407)
反饋

python可以運用在各行各業(yè)中,詳細這樣的slogan很多小伙伴都在各種python培訓(xùn)課上看到過,那么python是怎么應(yīng)用在各行各業(yè)中的呢?以醫(yī)學(xué)為例,python在醫(yī)學(xué)處理應(yīng)用上很多都是使用類似圖像識別圖像處理的相關(guān)功能來輔助醫(yī)生進行醫(yī)學(xué)圖像的輔助判斷。在這方面OpenCV是最常應(yīng)用在醫(yī)學(xué)處理應(yīng)用上的。今天小編就簡單地來介紹一下這種利用圖像識別處理來進行輔助判斷是怎么實現(xiàn)的吧。

題目描述

利用opencv或其他工具編寫程序?qū)崿F(xiàn)醫(yī)學(xué)處理。

實現(xiàn)過程

# -*- coding: utf-8 -*-
'''
作者 : 丁毅
開發(fā)時間 : 2021/5/9 16:30
'''
import cv2
import numpy as np


# 圖像細化
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:UserspcDesktopvas0.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)

# 圖像擴展
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)

# 圖像細化
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)

運行結(jié)果

問題及解決方法
1.自適應(yīng)閾值處理運行報錯
參考鏈接
解決方式:

void adaptiveThreshold(InputArray src, OutputArray dst, double
maxValue, int adaptiveMethod, int thresholdType, int bolckSize, double C)

  • srcInputArray類型的src,輸入圖像,填單通道,單8位浮點類型Mat即可。
  • dst:函數(shù)運算后的結(jié)果存放在這。即為輸出圖像(與輸入圖像同樣的尺寸和類型)。
  • maxValue:預(yù)設(shè)滿足條件的最大值。
  • adaptiveMethod自適應(yīng)閾值算法。
  • ADAPTIVE_THRESH_MEAN_CADAPTIVE_THRESH_GAUSSIAN_C兩種。
  • thresholdType:指定閾值類型。可選擇THRESH_BINARY或者THRESH_BINARY_INV兩種(即二進制閾值或反二進制閾值)。
  • bolckSize:表示鄰域塊大小,用來計算區(qū)域閾值,一般選擇為3、5、7......等。
  • C:參數(shù)C表示與算法有關(guān)的參數(shù),它是一個從均值或加權(quán)均值提取的常數(shù),可以是負數(shù)。
  • 根據(jù)報錯提示及參數(shù)解釋,blockSize的取值需要大于1且為奇數(shù)。

2.圖像擴展

參考鏈接
方式:使用cv2.copyMakeBorder()函數(shù)。
主要參數(shù):

  • src : 輸入的圖片。
  • top, bottom, left, right :相應(yīng)方向上的邊框?qū)挾取?/li>
  • borderType:定義要添加邊框的類型,詳情參考鏈接。

3.面積選擇
參考鏈接
方式:選擇滿足面積80-10000的圖像輸出, 去除噪聲位置元素。

4.圖像細化
參考鏈接
方式:經(jīng)過一層層的剝離,從原來的圖中去掉一些點,但仍要保持原來的形狀,直到得到圖像的骨架。骨架,可以理解為圖像的中軸。

到此這篇Python opencv醫(yī)學(xué)處理的實現(xiàn)過程的文章就介紹到這了,更多Python在其他行業(yè)的應(yīng)用請搜索W3Cschool以前的文章或繼續(xù)瀏覽下面的相關(guān)文章。

0 人點贊