用python计算菌斑面积

研究生课题需要所以写了一个:

import numpy as np
from PIL import Image
import skimage.io

#把图片二值化
def binaryzation(pic_id, new_id):
    img = Image.open(pic_id)
    img = img.convert("L")

    imgs = skimage.io.imread(pic_id)
    ttt = 1.4 * np.mean(imgs)

    WHITE, BLACK = 255, 0

    img = img.point(lambda x: WHITE if x > ttt else BLACK)
    img = img.convert('1')
    img.save(new_id)
#计算菌斑面积占比
def zone_cal(new_id):
    imgs = skimage.io.imread(new_id)
    a = imgs.tolist()
    b = w = 0
    for j in a:
        for l in j:
            if l <= 125:
                b += 1
            else:
                w += 1
    fungi_percent = w / (w + b)
    return fungi_percent
    #print(fungi_percent, w, b)
before = after = 0
for x in range(1, 4):
    binaryzation('d:/p/hx-'+str(x)+'.jpg', 'd:/p/hx-'+str(x)+'2值.jpg')
    before += zone_cal('d:/p/hx-'+str(x)+'2值.jpg')
for x in range(4, 7):
    binaryzation('d:/p/hx-'+str(x)+'.jpg', 'd:/p/hx-'+str(x)+'2值.jpg')
    after += zone_cal('d:/p/hx-'+str(x)+'2值.jpg')
before /= 3
after /= 3
print(before, after)

'''
0.27155324073124204 0.11879577511345958
'''

网上用matlab做的比较多,原理也很清楚,就不多说了。没有处理噪声,细节也损失的比较多。

转载于:https://www.cnblogs.com/imageSet/p/7496764.html

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值