欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

tensorflow和cv2在形态学操作上的区别

程序员文章站 2022-03-02 14:46:19
tensorflow和cv2在形态学操作上的区别tf.nn.erosion2d、tf.nn.dilation2d和cv2.erode,cv2.dilate的区别:1.结果的区别:实验:原图片:(来自西工大NWPU-RESISC45数据集)用tf.nn.erosion2d的代码import tensorflow as tfimport skimage.io as ioimport keras.backend as Kimport numpy as npfrom matplotlib i...

tensorflow和cv2在形态学操作上的区别


tf.nn.erosion2d、tf.nn.dilation2d和cv2.erode,cv2.dilate的区别:

1.结果的区别:
实验:
原图片:(来自西工大NWPU-RESISC45数据集)
tensorflow和cv2在形态学操作上的区别
用tf.nn.erosion2d的代码

import tensorflow as tf
import skimage.io as io
import keras.backend as K
import numpy as np
from matplotlib import pyplot as plt

kernel = K.random_normal(shape = (3, 3, 1))
img = io.imread('./airplane.png')
img = tf.convert_to_tensor(img ,tf.float32) #将array转化为tensor
img = tf.expand_dims(img, 0)                #升维度
img = tf.expand_dims(img, 3)

img1 = tf.nn.erosion2d(img, kernel, strides = [1,1,1,1], rates = [1,1,1,1], padding = "SAME")#图像腐蚀

with tf.Session() as sess:
    imgout = sess.run(img1)
    print(sess.run(img))
    print('-----')
    print(sess.run(img1))

imgout = imgout.squeeze(axis=0) #降维度
imgout = imgout.squeeze(axis=2)

plt.imshow(imgout)
plt.show()
plt.close()
结果(截取了一部分)[[[[147.]
   [153.]
   [147.]
   ...
   [147.]
   [153.]
   [147.]]
   ...
   [ 51.]
   [ 96.]
   [ 51.]]]]
-----
[[[[145.54266 ]
   [117.63092 ]
   [115.54267 ]
   ...
   [ 49.86652 ]
   [ 49.99149 ]
   [ 50.790764]]]]

输出图片:
tensorflow和cv2在形态学操作上的区别

用cv2.erode的代码:

from matplotlib import pyplot as plt
import skimage.io as io
import numpy as np
import cv2

img = io.imread('./airplane.png')
kernel = np.random.normal(loc = 0, scale = 1, size = (3, 3))
img1 = cv2.erode(img, kernel)
print(img)
print('-----')
print(img1)

plt.imshow(img1)
plt.show()
plt.close()
输出结果:
[[147 153 147 ... 147 153 147]
 [147 153 117 ... 147 123 153]
 [153 147 153 ... 153 147 137]
 ...
 [147 123 147 ...  96  51  96]
 [102 102 147 ...  80  51  96]
 [102 147 102 ...  51  96  51]]
-----
[[147 117 117 ... 123 123 123]
 [147 117 117 ... 123 123 123]
 [147 117 117 ... 123 107 107]
 ...
 [102 102 102 ...  51  51  51]
 [102 102 102 ...  51  51  51]
 [102 102 102 ...  51  51  51]]

输出图片:
tensorflow和cv2在形态学操作上的区别


图片看起来几乎一样,但是数据不是一样的。tf.nn.erosion2d只接受浮点数值的tensor,返回是浮点数据。而cv2.erode返回的是整数数据。在形态学膨胀上也如此。
2.参数区别:
tf.nn.erosion2d(
    value,
    kernel,
    strides,
    rates,
    padding,
    name=None
)

tf.nn.dilation2d(
    input,
    filter,
    strides,
    rates,
    padding,
    name=None
)

cv2.erode(src, kernel, dst=None, anchor=None,
	iterations=None, borderType=None, borderValue=None)
	
cv2.dilate(src, kernel, dst=None, anchor=None, 
	iterations=None, borderType=None, borderValue=None)

本文地址:https://blog.csdn.net/gushiyi27/article/details/110493617