图像二值化、提取边缘

# -*- coding:utf-8 -*-
'''
@finish time:20170914
@author;fengjiexyb
this code is used for my lcd digit dataset gray and binarization
'''

import pickle
from svmutil import *
from PCV.tools import imtools
import numpy as np
import struct
import matplotlib.pyplot as plt
import random
import logging
import os
from PIL  import Image
import cv2
path='C:/Users/fengjiexyb/Desktop/ttt/'
files=os.listdir(path)
features=[]
label = []
for index in files:
    image=Image.open(path+index)
    imNew=np.array(image.resize((50,50)).convert('L'))
    thresh = 10
    maxValue = 255
    #binarization used ostu in three lower lines
    blur = cv2.GaussianBlur(imNew, (5, 5), 0)
    ret3, dst = cv2.threshold(blur, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)
    Image.fromarray(dst).save('new' + index, 'jpeg')

    # 以下两行二值化的效果不如ostu
    # 具体参数参考http://docs.opencv.org/2.4/modules/imgproc/doc/miscellaneous_transformations.html?highlight=threshold#void cvtColor(InputArray src, OutputArray dst, int code, int dstCn)
    #retval, dst=cv2.threshold(imNew, thresh, maxValue, 0)
    #Image.fromarray(dst).save('new'+index,'jpeg')

    #以下两行二值化的效果是提取边缘
    #imres=cv2.adaptiveThreshold(imNew, 255, 0 , 1 , 5, 5)
    #Image.fromarray(imres).save('new'+index,'jpeg')

    原文作者:fengjiexyb
    原文地址: https://blog.csdn.net/fengjiexyb/article/details/78031213
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞