python图像灰度进行图像分析处理实战(一)

一、材料

1、python3.8
2、numpy库
3、math库
4、OpenCV-python 库

二、目的

利用灰度和最小二分法对图像进行识别处理,下面为待处理图片需要计算液滴的相对半径。

在这里插入图片描述

三、代码实现过程

import cv2
import numpy as np
import math

# 配置数据
class Config:
    def __init__(self):
        pass

    src = "photo1.jpg"
    resizeRate = 0.5
    min_area = 5000
    min_contours = 8
    threshold_thresh = 50
    epsilon_start = 10
    epsilon_step = 10

def order_points(pts):
    # 初始化一个坐标列表,该列表将被排序,
    # 列表中的第一个条目是左上角,第二个条目是右上角,
    # 第三个是右下角,第四个是左下角
    rect = np.zeros((4, 2), dtype="float32")

    # 左上点的总和最小,而右下点的总和最大
    s = pts.sum(axis=1)
    rect[0] = pts[np.argmin(s)]
    rect[2] = pts[np.argmax(s)]

    # 现在,计算点之间的差异,右上角的点差异最小,而左下角的差异最大
    diff = np.diff(pts, axis=1)
    rect[1] = pts[np.argmin(diff)]
    rect[3] = pts[np.argmax(diff)]

    # 返回有序坐标
    return rect

# 求两点间的距离
def point_distance(a, b):
    return int(np.sqrt(np.sum(np.square(a - b))))

# 找出外接四边形, c是轮廓的坐标数组
def boundingBox(idx, c):
    if len(c) < Config.min_contours:
        return None
    epsilon = Config.epsilon_start
    while True:
        approxBox = cv2.approxPolyDP(c, epsilon, True)
        # 求出拟合得到的多边形的面积
        theArea = math.fabs(cv2.contourArea(approxBox))
        # 输出拟合信息
        print("contour idx: %d ,contour_len: %d ,epsilon: %d ,approx_len: %d ,approx_area: %s" % (
        idx, len(c), epsilon, len(approxBox), theArea))
        if (len(approxBox) < 4):
            return None
        if theArea > Config.min_area:
            if (len(approxBox) > 4):
                # epsilon 增长一个步长值
                epsilon += Config.epsilon_step
                continue
            else:  # approx的长度为4,表明已经拟合成矩形了
                # 转换成4*2的数组
                approxBox = approxBox.reshape((4, 2))
                return approxBox
        else:
            print("failed to find boundingBox,idx = %d area=%f" % (idx, theArea))
            return None

# 开始图像处理,读取图片文件
image = cv2.imread(Config.src)

# 获取原始图像的大小
srcHeight, srcWidth, channels = image.shape

# 对原始图像进行缩放
image = cv2.resize(image, (int(srcWidth * Config.resizeRate), int(srcHeight * Config.resizeRate)))

# 转成灰度图
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

# 中值滤波平滑,消除噪声
# 当图片缩小后,中值滤波的孔径也要相应的缩小,否则会将有效的轮廓擦除
binary = cv2.medianBlur(gray, 1)

# 转换为二值图像
ret, binary = cv2.threshold(binary, Config.threshold_thresh, 255, cv2.THRESH_BINARY)
# 显示转换后的二值图像
cv2.imshow("binary", binary)

# 进行2次腐蚀操作(erosion)
# 腐蚀操作将会腐蚀图像中白色像素,可以将断开的线段连接起来
binary = cv2.erode(binary, None, iterations=2)
# 显示腐蚀后的图像
cv2.imshow("erode", binary)

# canny 边缘检测
binary = cv2.Canny(binary, 0, 60, apertureSize=3)
# 显示边缘检测的结果
cv2.imshow("Canny", binary)

# 提取轮廓
contours, _ = cv2.findContours(binary, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
# 输出轮廓数目
print("the count of contours is  %d \n" % (len(contours)))

# 针对每个轮廓,拟合外接四边形,如果成功,则将该区域切割出来,作透视变换,并保存为图片文件
for idx, c in enumerate(contours):
    approxBox = boundingBox(idx, c)
    if approxBox is None:
        print("\n")
        continue

    # 获取最小矩形包络
    rect = cv2.minAreaRect(approxBox)
    box = cv2.boxPoints(rect)
    box = np.int0(box)
    box = box.reshape(4, 2)
    box = order_points(box)
    print("boundingBox:\n", box)

    # 待切割区域的原始位置,
    # approxPolygon 点重排序, [top-left, top-right, bottom-right, bottom-left]
    src_rect = order_points(approxBox)
    print("src_rect:\n", src_rect)

    w, h = point_distance(box[0], box[1]), point_distance(box[1], box[2])
    print("w = %d ,h= %d " % (w, h))

    # 生成透视变换矩阵
    dst_rect = np.array([
        [0, 0],
        [w - 1, 0],
        [w - 1, h - 1],
        [0, h - 1]],
        dtype="float32")

    # 透视变换
    M = cv2.getPerspectiveTransform(src_rect, dst_rect)

    # 得到透视变换后的图像
    warped = cv2.warpPerspective(image, M, (w, h))

    # 将变换后的结果图像写入png文件
    cv2.imwrite("output/piece%d.png" % idx, warped, [int(cv2.IMWRITE_PNG_COMPRESSION), 9])

    print("\n")

print('over')
cv2.waitKey(0)

四、结果展示

在这里插入图片描述
在这里插入图片描述

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

绝域时空

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值