脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - Python opencv医学处理的实现过程

Python opencv医学处理的实现过程

2021-10-28 08:29Dream丶Killer Python

这篇文章主要介绍了Python opencv医学处理的实现过程,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

题目描述

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

实现过程

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# -*- 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:\users\pc\desktop\vas0.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医学处理的实现过程
Python opencv医学处理的实现过程
Python opencv医学处理的实现过程
Python opencv医学处理的实现过程
Python opencv医学处理的实现过程
Python opencv医学处理的实现过程
Python opencv医学处理的实现过程
Python opencv医学处理的实现过程

问题及解决方法

1.自适应阈值处理运行报错

参考链接

解决方式:

void adaptivethreshold(inputarray src, outputarray dst, double
maxvalue, int adaptivemethod, int thresholdtype, int bolcksize, double c)

  • srcinputarray类型的src,输入图像,填单通道,单8位浮点类型mat即可。
  • dst:函数运算后的结果存放在这。即为输出图像(与输入图像同样的尺寸和类型)。
  • maxvalue:预设满足条件的最大值。
  • adaptivemethod自适应阈值算法。
  • adaptive_thresh_mean_cadaptive_thresh_gaussian_c两种。
  • thresholdtype:指定阈值类型。可选择thresh_binary或者thresh_binary_inv两种(即二进制阈值或反二进制阈值)。
  • bolcksize:表示邻域块大小,用来计算区域阈值,一般选择为3、5、7......等。
  • c:参数c表示与算法有关的参数,它是一个从均值或加权均值提取的常数,可以是负数。
  • 根据报错提示及参数解释,blocksize的取值需要大于1且为奇数。

2.图像扩展

参考链接


方式:使用cv2.copymakeborder()函数。
主要参数:

  • src : 输入的图片。
  • top, bottom, left, right :相应方向上的边框宽度。
  • bordertype:定义要添加边框的类型,详情参考链接。

3.面积选择

参考链接

方式:选择满足面积80-10000的图像输出, 去除噪声位置元素。

4.图像细化

参考链接

方式:经过一层层的剥离,从原来的图中去掉一些点,但仍要保持原来的形状,直到得到图像的骨架。骨架,可以理解为图像的中轴。

到此这篇关于python opencv医学处理的实现过程的文章就介绍到这了,更多相关python opencv医学处理内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/qq_43965708/article/details/116572109

延伸 · 阅读

精彩推荐