瑕疵品鉴定:python图片预处理

安装Python

从python官网下载 Python 安装即可 两个版本:python2.× :python3.× ,我用的是python3.7,建议版本不要太高,3.5即可。
python官网

配置环境

使用到的模块

  1. NumPy : 用于数组运算,矩阵等数学运算
  2. pillow: 用于图片获取以及各种图像操作
  3. Matplotlib: Matplotlib 是 Python 的绘图库。 它可与 NumPy 一起使用,提供了一种有效的 MatLab 开源替代方案。 它也可以和图形工具包一起使用,如 PyQt 和 wxPython。

安装模块:
CMD:

pip install matplotlib
pip install pillow
pip install numpy
pip install opencv-python

引入模块:

import numpy as np
from PIL import Image
import matplotlib.pyplot as plt
import cv2 as cv

图像处理的重中之重:矩阵运算

由于在python中使用for循环运行速度很慢,这样在图像处理过程中,运用矩阵运算就极为重要了。

矩阵运算(numpy)过程中的函数:

import numpy as np
a  =  np.arange(100) # 生成1-100的一个数组
a = np.mat(a) 	     # 将数组变换为矩阵
b = np.array(a)	    # 变回数组
c = np.mat(np.arange(100))
d = np.multiply(a, c.T) # 矩阵乘法运算,以及矩阵的转置
e = np.zeros([4, 5], float)
f = np.ones([5, 4], int)

a = a > 50   # 创建一个1 - 0 矩阵

附带创建棋盘的函数

def zeros_self():
    im_1 = Image.open(r'1.bmp', "r")
    width = im_1.size[0]
    height = im_1.size[1]
    zeros_1 = np.arange(0, width)
    zeros_1 = np.mat(zeros_1)
    zeros_1 = zeros_1 % 56 == 0
    zeros_2 = np.arange(0, height)
    zeros_2 = np.mat(zeros_2)
    zeros_2 = zeros_2 % 46 == 0
    result_zer = np.multiply(zeros_1, zeros_2.T)
    # show_image(result_zer)
    return result_zer

OpenCV操作

包括OpenCV图像获取,边缘处理,二值化,打开灰度图,腐蚀膨胀算法,以及图片显示,OpenCV矩阵的运算。

# 边缘处理以及腐蚀膨胀
def border_detection():
    img = cv.imread(path1, 0)
    b = np.ones((3, 3), np.uint8)
    result_1 = cv.erode(img, b)
    img_1 = cv.Canny(result_1, 30, 70)
    c = np.ones((5, 5), np.uint8)
    result_2 = cv.dilate(img_1, c)
    result_2 = cv.dilate(result_2, c)

    cv.namedWindow("result_1", 0)
    cv.namedWindow("result_2", 0)
    cv.namedWindow("img_1", 0)
    cv.imshow("result_1", result_1)
    cv.imshow("result_2", result_2)
    cv.imshow("img_1", img_1)
    cv.waitKey(0)
    cv.destroyAllWindows()

    result = result_2 > 0
    return result

附:

# coding:utf-8
# author:HOJAY
# 矩阵运算 图像预处理
from PIL import Image, ImageDraw
import numpy as np
import matplotlib.pyplot as plt
import cv2 as cv

path1 = "border_detection.bmp"

#打开图片并返回二值化图片

def openfile():
    img = Image.open(r'1.bmp', "r")
    # pix = img.load()
    r, g, b = img.split()
    r = np.mat(r)
    g = np.mat(g)
    b = np.mat(b)
    c = (2 * r - g - b)/(2 * r + g + b + 0.000001)
    c = c > 0.3
    show_pic(r, g, b, c)
    # pic.show()


#显示合并的图片
def show_pic(r, g, b, c):
    r = np.multiply(r, c)
    g = np.multiply(g, c)
    b = np.multiply(b, c)
    r = Image.fromarray(r)
    g = Image.fromarray(g)
    b = Image.fromarray(b)
    pic = Image.merge('RGB', [r, g, b])     #合并三通道
    # pic.show()
    pic.save(path1)


# 边缘处理以及腐蚀膨胀
def border_detection():
    img = cv.imread(path1, 0)
    b = np.ones((3, 3), np.uint8)
    result_1 = cv.erode(img, b)
    img_1 = cv.Canny(result_1, 30, 70)
    c = np.ones((5, 5), np.uint8)
    result_2 = cv.dilate(img_1, c)
    result_2 = cv.dilate(result_2, c)

    cv.namedWindow("result_1", 0)
    cv.namedWindow("result_2", 0)
    cv.namedWindow("img_1", 0)
    cv.imshow("result_1", result_1)
    cv.imshow("result_2", result_2)
    cv.imshow("img_1", img_1)
    cv.waitKey(0)
    cv.destroyAllWindows()

    result = result_2 > 0
    return result


def cut():
    im_2 = Image.open(r'1.bmp', "r")
    chessboard = zeros_self()
    img_k = border_detection()
    result = np.multiply(chessboard, img_k)
    result_array = np.nonzero(result)
    print(result_array)
    print(len(result_array[0]))
    for i in range(0, len(result_array[0])-1):
        draw = ImageDraw.Draw(im_2)
        draw.text((result_array[1][i], result_array[0][i]), str(i), fill=(255, 0, 0),)
        img = im_2.crop((result_array[1][i]-112, result_array[0][i]-92, result_array[1][i]+112, result_array[0][i]+92))
        # img.show()
        img.save("F:/data/"+str(i)+"temp.bmp")
    # im_2.show()


def zeros_self():
    im_1 = Image.open(r'1.bmp', "r")
    width = im_1.size[0]
    height = im_1.size[1]
    zeros_1 = np.arange(0, width)
    zeros_1 = np.mat(zeros_1)
    zeros_1 = zeros_1 % 56 == 0
    zeros_2 = np.arange(0, height)
    zeros_2 = np.mat(zeros_2)
    zeros_2 = zeros_2 % 46 == 0
    result_zer = np.multiply(zeros_1, zeros_2.T)
    # show_image(result_zer)
    return result_zer


def show_image(mat_img):
    mat_img = Image.fromarray(mat_img)
    mat_img.show()


if __name__ == '__main__':
    openfile()
    cut()
    print("end")


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值