tensorflow和Keras 转换RGB到BGR的实现代码

因为要使用opencv处理图像,就必须将图片的格式从RGB的格式转换成BGR的形式,这里列一下用得比较多TF和Keras版本代码。

tensorflow

import numpy as np
import tensorflow as tf

vgg_mean = [103.939, 116.779, 123.68]

self.tfx = tf.placeholder(tf.float32, [None, 224, 224, 3])
self.tfy = tf.placeholder(tf.float32, [None, 1])

# Convert RGB to BGR
red, green, blue = tf.split(axis=3, num_or_size_splits=3, value=self.tfx * 255.0)
bgr = tf.concat(axis=3, values=[
	blue - self.vgg_mean[0],
    green - self.vgg_mean[1],
    red - self.vgg_mean[2],
])

Keras

这里用到VGG16和手写数字识别mnist数据集

import cv2
from keras import datasets
from keras.applications.vgg16 import VGG16
from keras.datasets import mnist
import numpy as np


(X_train,y_train),(X_test,y_test) = mnist.load_data()
 
#转成VGG16需要的格式
#RGB ->> bgr格式
X_train = [cv2.cvtColor(cv2.resize(i,(ishape,ishape)), cv2.COLOR_GRAY2BGR) for i in X_train]
X_train = np.concatenate([arr[np.newaxis] for arr in X_train]).astype('float32')
 
X_test  = [cv2.cvtColor(cv2.resize(i,(ishape,ishape)), cv2.COLOR_GRAY2BGR) for i in X_test ]
X_test  = np.concatenate([arr[np.newaxis] for arr in X_test] ).astype('float32')
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,数字手写体分割和识别是一个比较复杂的任务,需要多种技术的组合。以下是一个基于Python的简单实现,仅供参考: 1. 导入必要的库 ```python import cv2 import numpy as np import matplotlib.pyplot as plt import tensorflow as tf from tensorflow import keras ``` 2. 加载训练好的数字手写体识别模型 ```python model = keras.models.load_model('digits_model.h5') ``` 3. 定义数字手写体图像分割和识别函数 ```python def recognize_digits(image): # 将图像转换为灰度图 gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) # 对图像进行二值化处理 ret, thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU) # 查找图像中的轮廓 contours, hierarchy = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) # 对轮廓进行排序 contours = sorted(contours, key=lambda ctr: cv2.boundingRect(ctr)[0]) # 遍历每个轮廓 for contour in contours: # 获取轮廓的坐标和大小 x, y, w, h = cv2.boundingRect(contour) # 提取轮廓中的数字图像 digit_image = thresh[y:y+h, x:x+w] # 调整图像大小和形状 resized_digit = cv2.resize(digit_image, (18,18)) padded_digit = np.pad(resized_digit, ((5,5),(5,5)), "constant", constant_values=0) # 将图像转换为模型所需的格式 input_digit = padded_digit.reshape(1,28,28,1).astype("float32") / 255 # 使用模型进行数字识别 digit = np.argmax(model.predict(input_digit), axis=-1)[0] # 在原图像中标记数字的位置和识别结果 cv2.rectangle(image, (x,y), (x+w,y+h), (0,255,0), 2) cv2.putText(image, str(digit), (x+int(w/2),y-int(h/2)), cv2.FONT_HERSHEY_SIMPLEX, 2, (0,255,0), 2) return image ``` 4. 加载测试图像并进行数字手写体分割和识别 ```python # 加载测试图像 image = cv2.imread('test_image.png') # 进行数字手写体分割和识别 result = recognize_digits(image) # 显示结果图像 plt.imshow(cv2.cvtColor(result, cv2.COLOR_BGR2RGB)) plt.show() ``` 请注意,这个实现仅仅是一个简单的示例,可能无法处理所有情况。实际应用中,需要根据具体情况调整算法和参数,并进行更多的优化。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值