温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Python opencv怎么实现医学处理

发布时间:2022-01-26 10:34:04 来源:亿速云 阅读:160 作者:zzz 栏目:开发技术

这篇文章主要讲解了“Python opencv怎么实现医学处理”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“Python opencv怎么实现医学处理”吧!

题目描述

利用opencv或其他工具编写程序实现医学处理。

实现过程

# -*- coding: utf-8 -*-
'''
作者 : 丁毅
开发时间 : 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)

# 自适应阈值分割
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)
    # 判断区域是否满足面积要求
    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)

感谢各位的阅读,以上就是“Python opencv怎么实现医学处理”的内容了,经过本文的学习后,相信大家对Python opencv怎么实现医学处理这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI