首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >与图像不同的是角点的conv2D

与图像不同的是角点的conv2D
EN

Stack Overflow用户
提问于 2019-07-01 08:40:39
回答 1查看 278关注 0票数 1

的目标是尝试在模型的第一层上使用自定义权重来充分填充高通滤波器的功能--使模型的第一层与图像的高通滤波器相同。

1.首先,类似的解决方案是:在图像处理中使用高通滤波器,生成新的图像,并将其应用于模型中。--这是必须使用的图像处理,这是时间的成本。

2.我要设置Conv2D的a层,它也可以高传递图像。使用自定义筛选器(作为初始化器)。其基本原理是滤波器和conv2D都采用卷积规则。

但结果与第一解不同。

代码语言:javascript
运行
复制
#The image processing code:
    kernel55 = np.array([[-1, 2, -2, 2, -1], 
                         [2, -6, 8, -6, 2], 
                         [-2, 8, -12, 8, -2], 
                         [2,-6, 8, -6, 2],
                         [-1, 2, -2, 2, -1]])/12
        # load the image, pre-process it, and store it in the data list
        image = cv2.imread('1.pgm',-1)
        image = ndimage.convolve(image, kernel55)
        print(image)

#the first layer of the Model:

    def kernel_init(shape):
        kernel = np.zeros(shape)
        kernel[:,:,0,0] = np.array([[-1, 2, -2, 2, -1], 
                             [2, -6, 8, -6, 2], 
                             [-2, 8, -12, 8, -2], 
                             [2,-6, 8, -6, 2],
                             [-1, 2, -2, 2, -1]])/12
        return kernel
    #Build Keras model
    model = Sequential()
    model.add(Conv2D(1, [5,5], kernel_initializer=kernel_init, 
                     input_shape=(256,256,1), padding="same",activation='relu'))
    model.build()

test_im=cv2.imread('1.pgm',-1)  # define a test image
test_im=np.expand_dims(np.expand_dims(np.array(test_im),2),0)
out = model.predict(test_im)

问题是:使用图像处理能够产生合适的高通率图像,但使用Conv2D的结果并不相同。

我假设两个结果应该是相同或相似的,结果不是.

为什么,我的想法有什么问题吗?

EN

回答 1

Stack Overflow用户

发布于 2019-07-01 10:48:59

对这个不完整的答案表示歉意,但我有一些部分有效的东西,还有一些解释。下面是代码:

代码语言:javascript
运行
复制
import cv2
import numpy as np
import scipy.ndimage as ndimage
from keras.models import Sequential
from keras.layers import Dense, Activation, Conv2D

#The image processing code:
#the first layer of the Model:

def kernel_init(shape):
    kernel = np.zeros(shape)
    kernel[:,:,0,0] = np.array([[-1, 2, -2, 2, -1],
                         [2, -6, 8, -6, 2],
                         [-2, 8, -12, 8, -2],
                         [2,-6, 8, -6, 2],
                         [-1, 2, -2, 2, -1]])
    #kernel = kernel/12
    #print("Here is the kernel")
    #print(kernel)
    #print("That was the kernel")
    return kernel

def main():
    print("starting")
    kernel55 = np.array([[-1, 2, -2, 2, -1],
                         [2, -6, 8, -6, 2],
                         [-2, 8, -12, 8, -2],
                         [2,-6, 8, -6, 2],
                         [-1, 2, -2, 2, -1]])
    # load the image, pre-process it, and store it in the data list
    image = cv2.imread('tiger.bmp',-1)
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    myimage = cv2.resize(gray,(256,256))
    myimage = myimage
    print("The image")
    #print(myimage)
    print("That was the image")
    segment = myimage[0:10, 0:10]
    print(segment)

    imgOut = ndimage.convolve(myimage, kernel55)
    #imgOut = imgOut/12
    print(imgOut.shape)
    cv2.imwrite('zzconv.png', imgOut)

    #print(imgOut)
    segment = imgOut[0:10, 0:10]
    print(segment)

    #Build Keras model
    print("And the Keras stuff")
    model = Sequential()
    model.add(Conv2D(1, [5,5], kernel_initializer=kernel_init, input_shape=(256,256,1), padding="same"))
    model.build()

    test_im=myimage
    test_im = test_im.reshape((1, 256, 256, 1))
    print(test_im.shape)
    imgOut2 = model.predict(test_im)
    imgOut2 = imgOut2.reshape(256, 256)
    print(imgOut2.shape)
    #imgOut2 = imgOut2 / 12
    imgOut2[imgOut2 < 0] += 256

    cv2.imwrite('zzconv2.png', imgOut2)

    #print(imgOut2)
    segment = imgOut2[0:10, 0:10]
    print(segment)

以下是要注意的事情:

  • 这是一个图像,像素是字节,任何大于字节的东西都可能被截断,并且可能被不正确地截断(请注意,我必须删除内核上的"/12“。这就是为什么我添加了"+=256“部分。
  • 您不能假设“填充”区域将产生相同的结果。我不知道keras和opencv用于填充的值是什么,但它似乎不是相同的值。您的输出图像应该仅与3,3相同。
  • 在使用之前先检查一下内核。在我的系统中它被四舍五入到-1和0。大概是使用整数运算。添加“核=内核/12”行对内核给出了更正确的结果,但是卷积函数中的舍入似乎又把事情搞砸了,所以我没有使用"/12“。
  • Relu把事情搞砸了,这也是因为舍入(没有正确截断为无符号字节的任何低于零的字符都会被激活函数过滤掉)。
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56832504

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档