数字图像处理实验完成情况

实验1.1-中值滤波

import cv2 as cv
import numpy as np
# import scipy


def medianBlur(img, kernel, padding_way='ZERO', Multithreading=False):
    # 检测传入的kernel是否为一个合法数组.
    if kernel % 2 == 0 or kernel is 1:
        print('kernel size need 3, 5, 7, 9....')
        return None

    # 通过kernel的大小来计算paddingSize的大小
    paddingSize = kernel // 2

    # 获取图片的通道数
    layerSize = len(img.shape)

    # 获取传入的图片的大小
    height, width = img.shape[:2]

    # 这里是对多通道的处理
    if layerSize == 3:  # 多通道的处理方式,就是反复的调用单通道.
        matMutbase = np.zeros_like(img)
        for l in range(matMutbase.shape[2]):
            matMutbase[:, :, l] = medianBlur(img[:, :, l], kernel, padding_way)
        return matMutbase
    elif layerSize == 2:  # 单通道是中值滤波的实际工作的位置.
        # 实现方式和np.lib.pad相同
        # matBase = np.lib.pad(img,paddingSize, mode='constant', constant_values=0)
        matBase = np.zeros((height + paddingSize * 2, width + paddingSize * 2), dtype=img.dtype)

        matBase[paddingSize:-paddingSize, paddingSize:-paddingSize] = img
        # print(matBase)
        if padding_way is 'ZERO':
            pass
        elif padding_way is 'REPLICA':
            for r in range(paddingSize):
                matBase[r, paddingSize:-paddingSize] = img[0, :]
                matBase[-(1 + r), paddingSize:-paddingSize] = img[-1, :]
                matBase[paddingSize:-paddingSize, r] = img[:, 0]
                matBase[paddingSize:-paddingSize, -(1 + r)] = img[:, -1]
            # print(matBase)
        else:
            print('padding_way error need ZERO or REPLICA')
            return None

        # 创建用于输出的矩阵
        matOut = np.zeros((height, width), dtype=img.dtype)
        # 这里是遍历矩阵的每个点
        for x in range(height):
            for y in range(width):
                # 获取kernel X kernel 的内容,并转化成队并列
                line = matBase[x:x + kernel, y:y + kernel].flatten()
                # 队列排序处理.
                line = np.sort(line)
                # 取中间值赋值
                matOut[x, y] = line[(kernel * kernel) // 2]
        return matOut
    else:
        print('image layers error')
        return None



src=cv.imread("D:\softManager\install\Python\Python36\my_workspace\ep1\median_blur\lenna_jiaoyan.jpg")
cv.namedWindow("input image",cv.WINDOW_AUTOSIZE)
cv.imshow("input image",src)
# 将素材图片转为灰度图像
gray=cv.cvtColor(src,cv.COLOR_BGR2GRAY)
#Median_filtering(gray,cv.WINDOW_AUTOSIZE)
# Median_filtering(gray,3*3)
result = medianBlur(src, 5, padding_way='REPLICA')

cv.imshow("result",result)
cv.imwrite('D://softManager//install//Python//Python36//my_workspace//ep1//median_blur//result.jpg',result)
# scipy.misc.imsave('meelo.jpg', x)
cv.waitKey(0) # 等待下一个输出
cv.destroyAllWindows()  # 关闭所有窗口

实验1.2:直方图均衡化

from matplotlib import pyplot as plt
import sys
import numpy as np
import cv2 as mpimg


def equalization(gray_value):
    """
    传入灰度值,对灰度值做均衡化,不需要返回,直接修改传入的参数
    :param gray_value:
    """
    # 统计灰度直方图
    gray = np.zeros(256)
    row, column = gray_value.shape
    for i in range(row):
        for j in range(column):
            gray[gray_value[i][j]] += 1

    # 计算灰度占比
    gray /= (row * column)
    # 显示灰度直方图
    plt.subplot(2, 2, 2)
    plt.plot(gray)

    cumsum = np.cumsum(gray)  # 计算累积和

    # 均衡化
    # equa_t[i]=j表示原灰度值i经过均衡化后转化为灰度值j
    # 255×累积和四舍五入为int型
    equa_t = np.array((255 * cumsum + 0.5)).astype(np.int32)
    # 统计均衡化后的灰度数量
    equa_gray = np.zeros(256)
    for i in range(256):
        equa_gray[equa_t[i]] += gray[i]
    # 显示均衡化后的直方图
    plt.subplot(2, 2, 4)
    plt.plot(equa_gray)
    # 对原灰度矩阵做均衡化
    for i in range(row):
        for j in range(column):
            gray_value[i][j] = equa_t[gray_value[i][j]]


def run(img_path):
    img_array = mpimg.imread(img_path)
    plt.subplot(2, 2, 1)
    plt.imshow(img_array)
    img_array *= 255
    img_array = img_array.astype(np.int32)
    equalization(img_array[:, :, 0])
    equalization(img_array[:, :, 1])
    equalization(img_array[:, :, 2])
    img_array = img_array.astype(np.float64)
    img_array /= 255
    plt.subplot(2, 2, 3)
    plt.imshow(img_array)


if __name__ == "__main__":
    """
    if sys.argv.__len__() <= 1:
        png = input("请输入要处理的图片的路径:\n")
    else:
        png = sys.argv[1]
    #run(png)
    """
    run("D://softManager//install//Python//Python36//my_workspace//ep1//hist//rice.jpg")
    plt.show()

实验2.1 边缘检测

import cv2
from pylab import *
 
saber  = cv2.imread("D://softManager//install//Python//Python36//my_workspace//ep2//edge_detection//lenna.jpg")
# 首先将原图像进行边界扩展,并将其转换为灰度图。
gray_saber = cv2.cvtColor(saber,cv2.COLOR_RGB2GRAY)
gray_saber = cv2.resize(gray_saber,(200,200))
 
def RobertsOperator(roi):
    operator_first = np.array([[-1,0],[0,1]])
    operator_second = np.array([[0,-1],[1,0]])
    return np.abs(np.sum(roi[1:,1:]*operator_first))+np.abs(np.sum(roi[1:,1:]*operator_second))
 
def RobertsAlogrithm(image):
    image = cv2.copyMakeBorder(image,1,1,1,1,cv2.BORDER_DEFAULT)
    for i in range(1,image.shape[0]):
        for j in range(1,image.shape[1]):
            image[i,j] = RobertsOperator(image[i-1:i+2,j-1:j+2])
    return image[1:image.shape[0],1:image.shape[1]]
 
Robert_saber = RobertsAlogrithm(gray_saber)
plt.imshow(Robert_saber,cmap="binary")
cv2.imwrite("D://softManager//install//Python//Python36//my_workspace//ep2//edge_detection//result.jpg",Robert_saber)
plt.axis("off")
plt.show()

2.2 houg变换-直线检测

 import os
import numpy as np
import cv2
from PIL import Image, ImageEnhance
import math
from pylab import *
saber  = cv2.imread("D://softManager//install//Python//Python36//my_workspace//ep2//edge_detection//lenna.jpg")
# 首先将原图像进行边界扩展,并将其转换为灰度图。
gray_saber = cv2.cvtColor(saber,cv2.COLOR_RGB2GRAY)
gray_saber = cv2.resize(gray_saber,(200,200))
 
def RobertsOperator(roi):
    operator_first = np.array([[-1,0],[0,1]])
    operator_second = np.array([[0,-1],[1,0]])
    return np.abs(np.sum(roi[1:,1:]*operator_first))+np.abs(np.sum(roi[1:,1:]*operator_second))
 
def RobertsAlogrithm(image):
    image = cv2.copyMakeBorder(image,1,1,1,1,cv2.BORDER_DEFAULT)
    for i in range(1,image.shape[0]):
        for j in range(1,image.shape[1]):
            image[i,j] = RobertsOperator(image[i-1:i+2,j-1:j+2])
    return image[1:image.shape[0],1:image.shape[1]]
 
Robert_saber = RobertsAlogrithm(gray_saber)
cv2.imshow("Robert_saber",Robert_saber)
# hough霍夫线检测
lines = cv2.HoughLinesP(Robert_saber, 1, 1 * np.pi/180, 10, minLineLength=10, maxLineGap=5)
    # 画出检测的线段
for line in lines:
    for x1, y1, x2, y2 in line:
      cv2.line(saber, (x1, y1), (x2, y2), (255, 0, 0), 1)
    pass
img = Image.fromarray(saber, 'RGB')
img.show() 

实验3 bow(bag of word词袋模型)物体识别

  1. 创建词汇 cocabulary.py

python+tensorflow实现cnn神经网络(Mnist手写数字集的识别)

# -*- coding: utf-8 -*-
"""
Created on Wed Nov 21 17:32:28 2018

@author: zhen
"""
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
#import tensorflow as tf
import input_data

mnist = input_data.read_data_sets('Mnist_data/', one_hot=True)
sess = tf.InteractiveSession()  #compat.v1
#sess = tf.compat.v1.InteractiveSession()

def weight_variable(shape):
    initial = tf.truncated_normal(shape, stddev=0.1)
    return tf.Variable(initial)

def bias_variable(shape):
    initial = tf.constant(0.1, shape=shape)
    return tf.Variable(initial)

def conv2d(x, W):
    return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME')

def max_pool_2x2(x):
    return tf.nn.max_pool(x, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')

#x = tf.compat.v1.placeholder(tf.float32, [None, 784])
#y = tf.compat.v1.placeholder(tf.float32, [None, 10])
x = tf.placeholder(tf.float32, [None, 784])
y = tf.placeholder(tf.float32, [None, 10])
x_image = tf.reshape(x, [-1, 28, 28, 1])

# 第一层卷积核
W_conv = weight_variable([5, 5, 1, 16])
b_conv = bias_variable([16])
h_conv = tf.nn.relu(conv2d(x_image, W_conv) + b_conv)
h_pool = max_pool_2x2(h_conv)

# 第二层卷积核
W_conv2 = weight_variable([5, 5, 16, 32])
b_conv2 = bias_variable([32])
h_conv2 = tf.nn.relu(conv2d(h_pool, W_conv2) + b_conv2)
h_pool2 = max_pool_2x2(h_conv2)

# 全连接层
W_fc = weight_variable([7 * 7 * 32, 512])
b_fc = bias_variable([512])
h_pool_flat = tf.reshape(h_pool2, [-1, 7 * 7 * 32])
h_fc = tf.nn.relu(tf.matmul(h_pool_flat, W_fc) + b_fc)

# 防止过拟合,使用Dropout层
keep_prob = tf.placeholder(tf.float32)
#rate = 1 - keep_prob  # 有问题后加的
#h_fc_drop = tf.nn.dropout(h_fc, keep_prob)
h_fc_drop = tf.nn.dropout(h_fc, rate = 1-keep_prob)
# Softmax分类
W_fc2 = weight_variable([512, 10])
b_fc2 = bias_variable([10])
y_conv = tf.nn.softmax(tf.matmul(h_fc_drop, W_fc2) + b_fc2)

# 定义损失函数
cross_entropy = tf.reduce_mean(-tf.reduce_sum(y * tf.log(y_conv), reduction_indices=[1]))
train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)
correct_prediction = tf.equal(tf.argmax(y_conv, 1), tf.argmax(y, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

# 训练
tf.global_variables_initializer().run()
for i in range(20):
    batch = mnist.train.next_batch(50)
    train_step.run(feed_dict={x:batch[0], y:batch[1], keep_prob:0.5})
    
print("test accuracy %g" % accuracy.eval(feed_dict={x:mnist.test.images, y:mnist.test.labels, keep_prob:1.0}))

其中input_data.py文件如下:

# -*- coding: utf-8 -*-
"""
Created on Wed Nov 21 17:32:28 2018

@author: zhen
"""
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
#import tensorflow as tf
import input_data

mnist = input_data.read_data_sets('Mnist_data/', one_hot=True)
sess = tf.InteractiveSession()  #compat.v1
#sess = tf.compat.v1.InteractiveSession()

def weight_variable(shape):
    initial = tf.truncated_normal(shape, stddev=0.1)
    return tf.Variable(initial)

def bias_variable(shape):
    initial = tf.constant(0.1, shape=shape)
    return tf.Variable(initial)

def conv2d(x, W):
    return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME')

def max_pool_2x2(x):
    return tf.nn.max_pool(x, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')

#x = tf.compat.v1.placeholder(tf.float32, [None, 784])
#y = tf.compat.v1.placeholder(tf.float32, [None, 10])
x = tf.placeholder(tf.float32, [None, 784])
y = tf.placeholder(tf.float32, [None, 10])
x_image = tf.reshape(x, [-1, 28, 28, 1])

# 第一层卷积核
W_conv = weight_variable([5, 5, 1, 16])
b_conv = bias_variable([16])
h_conv = tf.nn.relu(conv2d(x_image, W_conv) + b_conv)
h_pool = max_pool_2x2(h_conv)

# 第二层卷积核
W_conv2 = weight_variable([5, 5, 16, 32])
b_conv2 = bias_variable([32])
h_conv2 = tf.nn.relu(conv2d(h_pool, W_conv2) + b_conv2)
h_pool2 = max_pool_2x2(h_conv2)

# 全连接层
W_fc = weight_variable([7 * 7 * 32, 512])
b_fc = bias_variable([512])
h_pool_flat = tf.reshape(h_pool2, [-1, 7 * 7 * 32])
h_fc = tf.nn.relu(tf.matmul(h_pool_flat, W_fc) + b_fc)

# 防止过拟合,使用Dropout层
keep_prob = tf.placeholder(tf.float32)
#rate = 1 - keep_prob  # 有问题后加的
#h_fc_drop = tf.nn.dropout(h_fc, keep_prob)
h_fc_drop = tf.nn.dropout(h_fc, rate = 1-keep_prob)
# Softmax分类
W_fc2 = weight_variable([512, 10])
b_fc2 = bias_variable([10])
y_conv = tf.nn.softmax(tf.matmul(h_fc_drop, W_fc2) + b_fc2)

# 定义损失函数
cross_entropy = tf.reduce_mean(-tf.reduce_sum(y * tf.log(y_conv), reduction_indices=[1]))
train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)
correct_prediction = tf.equal(tf.argmax(y_conv, 1), tf.argmax(y, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

# 训练
tf.global_variables_initializer().run()
for i in range(20):
    batch = mnist.train.next_batch(50)
    train_step.run(feed_dict={x:batch[0], y:batch[1], keep_prob:0.5})
    
print("test accuracy %g" % accuracy.eval(feed_dict={x:mnist.test.images, y:mnist.test.labels, keep_prob:1.0}))

训练过程如下:

1.算法模型不变,增大训练集数据【隐藏一层16个卷积核,隐藏二层32个卷积核,全连接层512,10分类】:

数据集为1000条:,10000,1000000
2.训练集数据不变,增大卷积核数【数据集为10000,全连接层512,10分类】:
隐藏一层16个卷积核,隐藏二层32个卷积核:

隐藏一层32个卷积核,隐藏二层64个卷积核:
 
隐藏一层64个卷积核,隐藏二层128个卷积核:

转自:https://www.cnblogs.com/yszd/p/10003087.html

# -*- coding: utf-8 -*-
"""
Created on Wed Nov 21 17:32:28 2018

@author: zhen
"""
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
#import tensorflow as tf
import input_data

mnist = input_data.read_data_sets('Mnist_data/', one_hot=True)
sess = tf.InteractiveSession()  #compat.v1
#sess = tf.compat.v1.InteractiveSession()

def weight_variable(shape):
    initial = tf.truncated_normal(shape, stddev=0.1)
    return tf.Variable(initial)

def bias_variable(shape):
    initial = tf.constant(0.1, shape=shape)
    return tf.Variable(initial)

def conv2d(x, W):
    return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME')

def max_pool_2x2(x):
    return tf.nn.max_pool(x, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')

#x = tf.compat.v1.placeholder(tf.float32, [None, 784])
#y = tf.compat.v1.placeholder(tf.float32, [None, 10])
x = tf.placeholder(tf.float32, [None, 784])
y = tf.placeholder(tf.float32, [None, 10])
x_image = tf.reshape(x, [-1, 28, 28, 1])

# 第一层卷积核
W_conv = weight_variable([5, 5, 1, 16])
b_conv = bias_variable([16])
h_conv = tf.nn.relu(conv2d(x_image, W_conv) + b_conv)
h_pool = max_pool_2x2(h_conv)

# 第二层卷积核
W_conv2 = weight_variable([5, 5, 16, 32])
b_conv2 = bias_variable([32])
h_conv2 = tf.nn.relu(conv2d(h_pool, W_conv2) + b_conv2)
h_pool2 = max_pool_2x2(h_conv2)

# 全连接层
W_fc = weight_variable([7 * 7 * 32, 512])
b_fc = bias_variable([512])
h_pool_flat = tf.reshape(h_pool2, [-1, 7 * 7 * 32])
h_fc = tf.nn.relu(tf.matmul(h_pool_flat, W_fc) + b_fc)

# 防止过拟合,使用Dropout层
keep_prob = tf.placeholder(tf.float32)
#rate = 1 - keep_prob  # 有问题后加的
#h_fc_drop = tf.nn.dropout(h_fc, keep_prob)
h_fc_drop = tf.nn.dropout(h_fc, rate = 1-keep_prob)
# Softmax分类
W_fc2 = weight_variable([512, 10])
b_fc2 = bias_variable([10])
y_conv = tf.nn.softmax(tf.matmul(h_fc_drop, W_fc2) + b_fc2)

# 定义损失函数
cross_entropy = tf.reduce_mean(-tf.reduce_sum(y * tf.log(y_conv), reduction_indices=[1]))
train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)
correct_prediction = tf.equal(tf.argmax(y_conv, 1), tf.argmax(y, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

# 训练
"""
# 保存网络训练的参数 
saver = tf.train.Saver() 
sess.run(tf.initialize_all_variables()) 
 """
 saver = tf.train.Saver() 
sess.run(tf.initialize_all_variables()) 
#tf.global_variables_initializer().run()
for i in range(200):
    batch = mnist.train.next_batch(50)
    train_step.run(feed_dict={x:batch[0], y:batch[1], keep_prob:0.5})
    
print("test accuracy %g" % accuracy.eval(feed_dict={x:mnist.test.images, y:mnist.test.labels, keep_prob:1.0}))

save_path = saver.save(sess, "model_mnist.ckpt") 
print("Model saved in life:", save_path) 
"""
save_path = saver.save(sess, "model_mnist.ckpt") 
print("Model saved in life:", save_path) 
"""
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值