基于opencv-python的灰分检测(灰度图像特征提取、文件夹图片批量处理)

任务目标:

  1. 完成灰分图片的特征提取
  2. 将文件夹所有灰分图片进行批处理
  3. 输出结果

关于灰分学习(灰分特征)

  • 煤灰分,煤完全燃烧后余下的残渣量。煤中灰分增加,增加了无效运输,加剧了我国铁路运输的紧张。
  • 从相关研究中了解到通过图像的特征分析可以提取出与煤灰分相关的特征有:均值、标准差、能量、熵、偏度、峰度等。
  • 在本文选取从灰度直方图中提取到均值、标准差、能量、熵、对比度来进行研究。

均值、标准差、熵、对比度与煤灰分的含量呈负相关
能量与煤灰分含量成正相关

灰度直方图在这里插入图片描述

i 代表灰度级,L 代表灰度级的种类数,n代表图像中具有灰度级的像素点的数目,N 代表图像中的像素点总数。从公式中我们也可以看出,灰度直方图H(i)代表的是图像中该灰度级的像素点的数目占图像全部像素点的比率,也就是图像中该灰度级i的像素点的频率。其横坐标表示图像的灰度级,纵坐标则表示此灰度级的频率。

从灰度直方图中我们提取出以下特征:

  • 均值在这里插入图片描述

  • 方差在这里插入图片描述

  • 能量在这里插入图片描述

  • 在这里插入图片描述

  • 灰度对比度在这里插入图片描述

任务流程:

1.定义图片处理函数‘gray_features’
2.读取图片文件夹
3.利用‘gray_features’函数批处理文件夹图片
4.数据输出并存储

全部代码展示:

import os
import cv2
import numpy as np


def gray_features(img):
    hist = cv2.calcHist([img], [0], None, [256], [0, 255])  # 得到全局直方图统计数据
    h, w = img.shape
    hist = hist / (h * w)  # 将直方图归一化为0-1,概率的形式

    grayFeature = []
    # 灰度平均值
    mean_gray = 0
    for i in range(len(hist)):
        mean_gray += i * hist[i]
    grayFeature.append(mean_gray[0])

    # 灰度方差
    var_gray = 0
    for i in range(len(hist)):
        var_gray += hist[i] * ((i - mean_gray) ** 2)
    grayFeature.append(var_gray[0])

    # 能量sum(hist[i])**2
    ##归一化
    max_ = np.max(hist)
    min_ = np.min(hist)
    histOne = (hist - min_) / (max_ - min_)
    ##求解能量
    energy = 0
    for i in range(len(histOne)):
        energy += histOne[i] ** 2
    grayFeature.append(energy[0])

    # 熵
    he = 0
    for i in range(len(hist)):
        if hist[i] != 0:  # 当等于0时,log无法进行计算,因此只需要计算非0部分的熵即可
            he += hist[i] * (np.log(hist[i]) / (np.log(2)))
    he = -he
    grayFeature.append(he[0])  # 因为返回的是含有一个元素的数组,所以通过取值操作将其取出来再加入到列表中去

    # 灰度对比度
    con = np.max(img) - np.min(img)
    grayFeature.append(con)
    return grayFeature


# 打开文件

path = "F:\coal_ash"
dirs = os.listdir(path)
# 输出所有文件和文件夹
for file in dirs:
    print(file)


# 单张待处理图片路径
f = open('F:\data.txt', 'a')  # 设置文件对象
f.write(("灰度均值,灰度方差,能量值,熵,灰度对比度 "))
f.write('\n')

for name in dirs:

    image_path = os.path.join(path, name)
    img = cv2.imread(image_path,0)
    grayFeas = gray_features(img)
    print('*' * 50)
    print(grayFeas)
    f = open('F:\data.txt', 'a')
    f.write(str(grayFeas))
    f.write('\n')
    f.close()
print("done")

灰分文件夹
在这里插入图片描述

最终输出效果图
在这里插入图片描述

学习的一些python知识

1.关于cv2的imread函数

import cv2
image_path = “绝对路径”
image = cv2.imread(image_path)
cv2.imshow(“cv2_image”, image)
cv2.waitKey()

2.关于路径拼接函数os.path.join

这里是引用

os.path.join()函数:连接两个或更多的路径名组件
1.如果各组件名首字母不包含’/’,则函数会自动加上
2.如果有一个组件是一个绝对路径,则在它之前的所有组件均会被舍弃
3.如果最后一个组件为空,则生成的路径以一个’/’分隔符结尾


Demo1 import os

Path1 = ‘home’ Path2 = ‘develop’ Path3 = ‘code’

Path10 = Path1 + Path2 + Path3 Path20 =
os.path.join(Path1,Path2,Path3) print ('Path10 = ',Path10) print
('Path20 = ',Path20)

输出

Path10 = homedevelopcode Path20 = home\develop\code


Demo2

import os

Path1 = ‘/home’ Path2 = ‘develop’ Path3 = ‘code’

Path10 = Path1 + Path2 + Path3 Path20 =
os.path.join(Path1,Path2,Path3) print ('Path10 = ',Path10) print
('Path20 = ',Path20) 输出

Path10 = /homedevelopcode Path20 = /home\develop\code


Demo3 import os

Path1 = ‘home’ Path2 = ‘/develop’ Path3 = ‘code’

Path10 = Path1 + Path2 + Path3 Path20 =
os.path.join(Path1,Path2,Path3) print ('Path10 = ',Path10) print
('Path20 = ',Path20)

输出

Path10 = home/developcode Path20 = /develop\code


Demo4 import os

Path1 = ‘home’ Path2 = ‘develop’ Path3 = ‘/code’

Path10 = Path1 + Path2 + Path3 Path20 =
os.path.join(Path1,Path2,Path3) print ('Path10 = ',Path10) print
('Path20 = ',Path20 )

输出

Path10 = homedevelop/code Path20 = /code

转https://www.cnblogs.com/an-ning0920/p/10037790.html

3.将输出结果储存到txt文件

f = open(‘workfile’, ‘w’)
第一个参数filename是包含文件地址的str
第二个参数mode用来指定文件被使用的方式,The mode argument is optional;如果不指定则默认为mode= ‘r’ 即只读模式
其中 mode=‘r’ 意味着 文件只是用来读入python ,
mode=‘w’ for only writing (如果写入之后的文件和之前的文件同名,则之前的那个文件会被擦除、覆盖an existing file with the same name will be erased)
mode=‘a’ opens the file for appending; any data written to the file is automatically added to the end任何append进file的数据都被自动加到文件末尾位置
mode=‘r+’ opens the file for both reading and writing读写均可.

with open(‘./data.txt’, ‘a’) as f: # 设置文件对象
print(‘11111111111111’,file = f)

参考文献:
[1]Qiu Zhaoyu,Dou Dongyang,Zhou Deyang,Yang Jianguo. On-line prediction of clean coal ash content based on image analysis[J]. Measurement,2021,173:
[2]王靖千. 基于图像处理的浮选尾矿灰分检测方法研究[D].太原理工大学,2019.
[3]高博. 基于图像灰度特征的浮选尾矿灰分软测量研究[D].中国矿业大学,2016.

  • 1
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值