Python 图像处理形态学(Image processing Morphology)

import numpy as np
import cv2

img = cv2.imread("img_.jpg",0)
# Creat a binary image
threshold = 0
for i in range(0, img.shape[0]):
    for j in range(0, img.shape[1]):
        threshold += img[i][j]
threshold = int(threshold / (img.shape[0] * img.shape[1]))

img_binary = np.zeros(img.shape)
for i in range(0, img.shape[0]):
    for j in range(0, img.shape[1]):
        if img[i][j] >= threshold:
            img_binary[i][j] = 255
        else:
            img_binary[i][j] = 0

cv2.imwrite("img_binary.jpg", img_binary)
# Erosion
img_before_erosion = img_binary
img_after_erosion = np.ones(img.shape) * 255
erosion_kernel = np.array([1,1])
for i in range(0, img_binary.shape[0]):
    for j in range(1, img_binary.shape[1]):
        if img_before_erosion[i][j - 1] * erosion_kernel[0] == 0 and img_before_erosion[i][j] * erosion_kernel[1] == 0:
            img_after_erosion[i][j - 1] = 0
        else:
            img_after_erosion[i][j - 1] = 255

cv2.imwrite("img_after_erosion.jpg", img_after_erosion)
# Dilation
img_before_dilation = img_binary
img_after_dilation = np.ones(img.shape) * 255
dilation_kernel = np.array([1,1])
for i in range(0, img_binary.shape[0]):
    for j in range(1, img_binary.shape[1]):
        if img_before_dilation[i][j - 1] * dilation_kernel[0] == 0 or img_before_dilation[i][j] * dilation_kernel[1] == 0:
            img_after_dilation[i][j - 1] = 0
        else:
            img_after_dilation[i][j - 1] = 255

cv2.imwrite("img_after_dilation.jpg", img_after_dilation)
# Opening
img_before_opening = img_after_erosion
img_after_opening = np.ones(img.shape) * 255
dilation_kernel = np.array([1,1])
for i in range(0, img_binary.shape[0]):
    for j in range(1, img_binary.shape[1
  • 6
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Chapter 1, A Taste of Machine Learning, will gently introduce you to the different subfields of machine learning, and explain how to install OpenCV and other essential tools in the Python Anaconda environment. Chapter 2, Working with Data in OpenCV and Python, will show you what a typical machine learning workflow looks like, and where data comes in to play. I will explain the difference between training and test data, and show you how to load, store, manipulate, and visualize data with OpenCV and Python. Chapter 3, First Steps in Supervised Learning, will introduce you to the topic of supervised learning by reviewing some core concepts, such as classification and regression. You will learn how to implement a simple machine learning algorithm in OpenCV, how to make predictions about the data, and how to evaluate your model. Chapter 4, Representing Data and Engineering Features, will teach you how to get a feel for some common and well-known machine learning datasets and how to extract the interesting stuff from your raw data. Chapter 5, Using Decision Trees to Make a Medical Diagnosis, will show you how to build decision trees in OpenCV, and use them in a variety of classification and regression problems. Chapter 6, Detecting Pedestrians with Support Vector Machines, will explain how to build support vector machines in OpenCV, and how to apply them to detect pedestrians in images. Chapter 7, Implementing a Spam Filter with Bayesian Learning, will introduce you to probability theory, and show you how you can use Bayesian inference to classify emails as spam or not. Chapter 8, Discovering Hidden Structures with Unsupervised Learning, will talk about unsupervised learning algorithms such as k-means clustering and Expectation-Maximization, and show you how they can be used to extract hidden structures in simple, unlabeled datasets. Chapter 9, Using Deep Learning to Classify Handwritten Digits, will introduce you to the exciting field of deep learning. Starting with the perceptron and multi-layer perceptrons, you will learn how to build deep neural networks in order to classify handwritten digits from the extensive MNIST database. Chapter 10, Combining Different Algorithms into an Ensemble, will show you how to effectively combine multiple algorithms into an ensemble in order to overcome the weaknesses of individual learners, resulting in more accurate and reliable predictions. Chapter 11, Selecting the Right Model with Hyper-Parameter Tuning, will introduce you to the concept of model selection, which allows you to compare different machine learning algorithms in order to select the right tool for the task at hand. Chapter 12, Wrapping Up, will conclude the book by giving you some useful tips on how to approach future machine learning problems on your own, and where to find information on more advanced topics.
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值