Python OpenCV实例:图像腐蚀(数学公式基本实现)
Python OpenCV实例:图像腐蚀(数学公式基本实现)
为什么80%的码农都做不了架构师?>>>
#coding:utf-8
'''
二值图像的腐蚀运算
定义:
g(x,y) = erode[f(x,y),B] = AND[Bf(x,y)]
其中,g(x,y)为腐蚀后的二值图像,f(x,y)为原始二值图像
B为结构元素,Bf(x,y)定义为
Bf(x,y) = {f(x - bx,y-by),(bx,by)∈B}
算子AND(x(i),...,x(n))定义为:当且仅当x(1) = ... = x(n) = 1时,
AND(x(1),...,x(n))等于1,否则为0
结构元素选择的原则往往是具有旋转不变性,或者镜像不变性。也就是说,结构元素
原点在其几何中心处理,并且其他像素关于该原点对你。常用到的结构元素:
1、水平单列;2、垂直单列;3、十字形;4、方形
'''
import cv2
import numpy as np
# 3位水平方向结构元素
def erode_horizontal_3x1(image):
img_gray = cv2.cvtColor(image,cv2.COLOR_RGB2GRAY)
rows,cols = img_gray.shape
gray_flat = img_gray.reshape((rows * cols,))
# 创建空白图像
dist = np.full(img_gray.shape,fill_value=255,dtype=img_gray.dtype).reshape((rows * cols,))
x_coord = np.arange(1,cols-1)
for i in range(rows):
for j in x_coord:
mid = gray_flat[i * cols + j]
lft = gray_flat[i * cols + j + 1]
rgt = gray_flat[i * cols + j - 1]
if mid == 0 and lft == 0 and rgt == 0:
dist[i * cols + j] = 0
return dist.reshape((rows,cols))
# 5位水平方向结构元素
def erode_horizontal_5x1(image):
img_gray = cv2.cvtColor(image,cv2.COLOR_RGB2GRAY)
rows,cols = img_gray.shape
gray_flat = img_gray.reshape((rows * cols,))
# 创建空白图像
dist = np.full(img_gray.shape,fill_value=255,dtype=img_gray.dtype).reshape((rows * cols,))
x_coord = np.arange(2,cols-2)
for i in range(rows):
for j in x_coord:
lft2 = gray_flat[i * cols + j -2]
rgt = gray_flat[i * cols + j - 1]
mid = gray_flat[i * cols + j]
lft = gray_flat[i * cols + j + 1]
rgt2 = gray_flat[i * cols + j + 2]
if mid == 0 and lft == 0 and rgt == 0 and lft2 == 0 and rgt2 == 0:
dist[i * cols + j] = 0
return dist.reshape((rows,cols))
# 3位垂直方向结构元素
def erode_vertical_3x1(image):
img_gray = cv2.cvtColor(image,cv2.COLOR_RGB2GRAY)
rows,cols = img_gray.shape
gray_flat = img_gray.reshape((rows * cols,))
# 创建空白图像
dist = np.full(img_gray.shape,fill_value=255,dtype=img_gray.dtype).reshape((rows * cols,))
y_coord = np.arange(1,rows-1)
for i in y_coord:
for j in range(cols):
mid = gray_flat[i * cols + j]
lft = gray_flat[(i + 1) * cols + j]
rgt = gray_flat[(i - 1) * cols + j]
if mid == 0 and lft == 0 and rgt == 0:
dist[i * cols + j] = 0
return dist.reshape((rows,cols))
# 5位垂直方向结构元素
def erode_vertical_5x1(image):
img_gray = cv2.cvtColor(image,cv2.COLOR_RGB2GRAY)
rows,cols = img_gray.shape
gray_flat = img_gray.reshape((rows * cols,))
# 创建空白图像
dist = np.full(img_gray.shape,fill_value=255,dtype=img_gray.dtype).reshape((rows * cols,))
y_coord = np.arange(2,rows - 2)
for i in y_coord:
for j in range(cols):
lft2 = gray_flat[(i - 2) * cols + j]
rgt = gray_flat[(i - 1) * cols + j]
mid = gray_flat[i * cols + j]
lft = gray_flat[(i + 1) * cols + j]
rgt2 = gray_flat[(i + 2) * cols + j]
if mid == 0 and lft == 0 and rgt == 0 and lft2 == 0 and rgt2 == 0:
dist[i * cols + j] = 0
return dist.reshape((rows,cols))
# 3位方形结构元素
def erode_cross_3x3(image):
img_gray = cv2.cvtColor(image,cv2.COLOR_RGB2GRAY)
rows,cols = img_gray.shape
gray_flat = img_gray.reshape((rows * cols,))
# 创建空白图像
dist = np.full(img_gray.shape,fill_value=255,dtype=img_gray.dtype).reshape((rows * cols,))
y_coord = np.arange(1,rows - 1)
x_coord = np.arange(1,cols - 1)
for i in y_coord:
for j in x_coord:
p1 = gray_flat[i * cols + j]
p2 = gray_flat[(i - 1) * cols + j]
p3 = gray_flat[(i + 1) * cols + j]
p4 = gray_flat[i * cols + (j - 1)]
p5 = gray_flat[(i - 1) * cols + (j - 1)]
p6 = gray_flat[(i + 1) * cols + (j - 1)]
p7 = gray_flat[(i - 1) * cols + (j + 1)]
p8 = gray_flat[i * cols + (j + 1)]
p9 = gray_flat[(i + 1) * cols + (j + 1)]
if p1 == 0 and p2 == 0 and p3 == 0 and \
p4 == 0 and p5 == 0 and p6 == 0 and \
p7 == 0 and p8 == 0 and p9 == 0:
dist[i * cols + j] = 0
return dist.reshape((rows,cols))
src = cv2.imread('datas/b.png')
dist_h3x1 = erode_horizontal_3x1(src)
dist_h5x1 = erode_horizontal_5x1(src)
dist_v3x1 = erode_vertical_3x1(src)
dist_v5x1 = erode_vertical_5x1(src)
dist_c3x3 = erode_cross_3x3(src)
cv2.imshow('src',src)
cv2.imshow('horizontal-3x1',dist_h3x1)
cv2.imshow('horizontal-5x1',dist_h5x1)
cv2.imshow('vertical-3x1',dist_v3x1)
cv2.imshow('vertical-5x1',dist_v5x1)
cv2.imshow('cross-3x3',dist_c3x3)
cv2.waitKey()
cv2.destroyAllWindows()
转载于:https://my.oschina.net/wujux/blog/1798088
Python OpenCV实例:图像腐蚀(数学公式基本实现)相关教程
Python OpenCV实例:直方图计算(数学公式简单实现)
Python OpenCV实例:直方图计算(数学公式简单实现) 为什么80%的码农都做不了架构师? #coding:utf-8'''直方图'''import cv2import numpy as npimport matplotlib.pyplot as plt'''计算RGB图像每个通道的直方图'''def cal_histgram_rgb(image): image_rgb =
Python OpenCV实例:图像灰度化(数学公式简单实现)
Python OpenCV实例:图像灰度化(数学公式简单实现) 为什么80%的码农都做不了架构师? #coding:utf-8'''图像灰度化1、基本公式:Gray(i,j) = [R(i,j) + G(i,j) + B(i,j)] / 3根据人眼对颜色的感知程度不同,衍生出第二个公式:Gray(i,j) = 0.299 * R(i,j) + 0
Python OpenCV实例:图像灰度拉伸
Python OpenCV实例:图像灰度拉伸 为什么80%的码农都做不了架构师? #coding:utf-8'''灰度拉伸定义:灰度拉伸,也称对比度拉伸,是一种简单的线性点运算。作用:扩展图像的 直方图,使其充满整个灰度等级范围内公式:g(x,y) = 255 / (B - A) * [f(x,y) - A],
Python实例:毛玻璃效果
Python实例:毛玻璃效果 为什么80%的码农都做不了架构师? #coding:utf-8'''毛玻璃效果'''import cv2import numpy as npsrc = cv2.imread('datas/images/f1.jpg')dst = np.zeros_like(src)rows,cols,_ = src.shapeoffsets = 5random_num = 0for y in range(ro
Python实例:线性最小二乘法拟合
Python实例:线性最小二乘法拟合 为什么80%的码农都做不了架构师? #coding:utf-8'''线性最小二乘拟合'''from __future__ import divisionfrom scipy import linalg as lafrom scipy import optimizeimport sympyimport numpy as npsympy.init_printing()impo
OpenCV + VTK + WebCam 采集和显示图像
OpenCV + VTK + WebCam 采集和显示图像 为什么80%的码农都做不了架构师? #include iostream#include vtkAutoInit.hVTK_MODULE_INIT(vtkRenderingOpenGL2)VTK_MODULE_INIT(vtkInteractionStyle) ;VTK_MODULE_INIT(vtkRenderingFreeType);#include opencv2/ope
Python实例:使用Matplotlib绘制三维图形
Python实例:使用Matplotlib绘制三维图形 为什么80%的码农都做不了架构师? #coding:utf-8from mpl_toolkits.mplot3d import Axes3Dfrom matplotlib import cmimport matplotlib.pyplot as pltimport numpy as npfig = plt.figure()ax = fig.gca(projection='
PCL+OpenCV+WebCam实现模拟深度图像实时点云数据采集
PCL+OpenCV+WebCam实现模拟深度图像实时点云数据采集 为什么80%的码农都做不了架构师? #include iostream#include opencv2/opencv.hpp#include pcl/point_types.h#include pcl/filters/extract_indices.h#include pcl/visualization/cloud_viewer.husing nam