【超分辨率】python中的图像空间的转换 RGB--YCBCR

2023-12-13 16:08

本文主要是介绍【超分辨率】python中的图像空间的转换 RGB--YCBCR,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

由于人眼对颜色不敏感,而对光亮通道更加敏感。因此在超分辨率任务中,我们通常需要将RGB通道转换为Ycbcr通道。在Python的代码实现中,我发现opencv的RGB转Ycbcr的计算方式和Matlab的实现方式有些不同,而NTIRE的评估往往是在matlab平台的。因此,这里需要注意。

Python RGB转Ycbcr通道

对于Set5中的baby图像

Code:

img = cv2.imread(imgpath)
img = cv2.cvtColor(img, cv2.COLOR_BGR2YCR_CB)
img_y = img[:,:,0]

Result:

array([[253, 253, 253, ..., 254, 254, 254],[253, 253, 253, ..., 254, 254, 254],[253, 253, 253, ..., 254, 254, 254],...,[ 62,  70,  72, ...,  67,  67,  67],[ 54,  58,  59, ...,  69,  68,  68],[ 49,  52,  53, ...,  70,  70,  69]], dtype=uint8)

实验原理:
在这里插入图片描述

参考链接:https://docs.opencv.org/3.0.0/de/d25/imgproc_color_conversions.html


Matlab RGB转Ycbcr通道

Code:

im  = imread(imgpath);
im = rgb2ycbcr(im);
im = im(:, :, 1);

Result:
在这里插入图片描述

Matlab实现方式:

function ycbcr = rgb2ycbcr(varargin)
%RGB2YCBCR Convert RGB color values to YCbCr color space.
%   YCBCRMAP = RGB2YCBCR(MAP) converts the RGB values in MAP to the YCBCR
%   color space. MAP must be a M-by-3 array. YCBCRMAP is a M-by-3 matrix
%   that contains the YCBCR luminance (Y) and chrominance (Cb and Cr) color
%   values as columns.  Each row represents the equivalent color to the
%   corresponding row in the RGB colormap.
%
%   YCBCR = RGB2YCBCR(RGB) converts the truecolor image RGB to the
%   equivalent image in the YCBCR color space. RGB must be a M-by-N-by-3
%   array.
%
%   If the input is uint8, then YCBCR is uint8 where Y is in the range [16
%   235], and Cb and Cr are in the range [16 240].  If the input is a double,
%   then Y is in the range [16/255 235/255] and Cb and Cr are in the range
%   [16/255 240/255].  If the input is uint16, then Y is in the range [4112
%   60395] and Cb and Cr are in the range [4112 61680].
%
%   Class Support
%   -------------
%   If the input is an RGB image, it can be uint8, uint16, or double. If the
%   input is a colormap, then it must be double. The output has the same class
%   as the input.
%
%   Examples
%   --------
%   Convert RGB image to YCbCr.
%
%      RGB = imread('board.tif');
%      YCBCR = rgb2ycbcr(RGB);
%
%   Convert RGB color space to YCbCr.
%
%      map = jet(256);
%      newmap = rgb2ycbcr(map);
%
%   See also NTSC2RGB, RGB2NTSC, YCBCR2RGB.%   Copyright 1993-2010 The MathWorks, Inc.  %   References: 
%     C.A. Poynton, "A Technical Introduction to Digital Video", John Wiley
%     & Sons, Inc., 1996, p. 175
% 
%     Rec. ITU-R BT.601-5, "STUDIO ENCODING PARAMETERS OF DIGITAL TELEVISION
%     FOR STANDARD 4:3 AND WIDE-SCREEN 16:9 ASPECT RATIOS",
%     (1982-1986-1990-1992-1994-1995), Section 3.5.rgb = parse_inputs(varargin{:});%initialize variables
isColormap = false;%must reshape colormap to be m x n x 3 for transformation
if (ndims(rgb) == 2)%colormapisColormap=true;colors = size(rgb,1);rgb = reshape(rgb, [colors 1 3]);
end% This matrix comes from a formula in Poynton's, "Introduction to
% Digital Video" (p. 176, equations 9.6). % T is from equation 9.6: ycbcr = origT * rgb + origOffset;
origT = [65.481 128.553 24.966;...-37.797 -74.203 112; ...112 -93.786 -18.214];
origOffset = [16;128;128];% The formula ycbcr = origT * rgb + origOffset, converts a RGB image in the range
% [0 1] to a YCbCr image where Y is in the range [16 235], and Cb and Cr
% are in that range [16 240]. For each class type (double,uint8,
% uint16), we must calculate scaling factors for origT and origOffset so that
% the input image is scaled between 0 and 1, and so that the output image is
% in the range of the respective class type.scaleFactor.double.T = 1/255;      % scale output so in range [0 1].
scaleFactor.double.offset = 1/255; % scale output so in range [0 1].
scaleFactor.uint8.T = 1/255;       % scale input so in range [0 1].
scaleFactor.uint8.offset = 1;      % output is already in range [0 255].
scaleFactor.uint16.T = 257/65535;  % scale input so it is in range [0 1]  % and scale output so it is in range % [0 65535] (255*257 = 65535).
scaleFactor.uint16.offset = 257;   % scale output so it is in range [0 65535].% The formula ycbcr = origT*rgb + origOffset is rewritten as 
% ycbcr = scaleFactorForT * origT * rgb + scaleFactorForOffset*origOffset.  
% To use imlincomb, we rewrite the formula as ycbcr = T * rgb + offset, where T and
% offset are defined below.
classIn = class(rgb);
T = scaleFactor.(classIn).T * origT;
offset = scaleFactor.(classIn).offset * origOffset;%initialize output
ycbcr = zeros(size(rgb),classIn);for p = 1:3ycbcr(:,:,p) = imlincomb(T(p,1),rgb(:,:,1),T(p,2),rgb(:,:,2), ...T(p,3),rgb(:,:,3),offset(p));
end  if isColormapycbcr = reshape(ycbcr, [colors 3 1]);
end%%%
%Parse Inputs
%%%
function X = parse_inputs(varargin)narginchk(1,1);
X = varargin{1};if ndims(X)==2% For backward compatibility, this function handles uint8 and uint16% colormaps. This usage will be removed in a future release.validateattributes(X,{'uint8','uint16','double'},{'nonempty'},mfilename,'MAP',1);if (size(X,2) ~=3 || size(X,1) < 1)error(message('images:rgb2ycbcr:invalidSizeForColormap'))endif ~isa(X,'double')warning(message('images:rgb2ycbcr:notAValidColormap'))X = im2double(X);endelseif ndims(X)==3validateattributes(X,{'uint8','uint16','double'},{},mfilename,'RGB',1);if (size(X,3) ~=3)error(message('images:rgb2ycbcr:invalidTruecolorImage'))end
elseerror(message('images:rgb2ycbcr:invalidInputSize'))
end

实验可发现两种实现方式的结果存在着不同, 这是因为两者的内部实现原理不同。这里提供一个与Matlab的Ycbcr空间转换类似的函数:


def rgb2ycbcr(img, only_y=True):'''same as matlab rgb2ycbcronly_y: only return Y channelInput:uint8, [0, 255]float, [0, 1]'''in_img_type = img.dtypeimg.astype(np.float32)if in_img_type != np.uint8:img *= 255.# convertif only_y:rlt = np.dot(img, [65.481, 128.553, 24.966]) / 255.0 + 16.0else:rlt = np.matmul(img, [[65.481, -37.797, 112.0], [128.553, -74.203, -93.786],[24.966, 112.0, -18.214]]) / 255.0 + [16, 128, 128]if in_img_type == np.uint8:rlt = rlt.round()else:rlt /= 255.return rlt.astype(in_img_type)

这篇关于【超分辨率】python中的图像空间的转换 RGB--YCBCR的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/489073

相关文章

基于人工智能的图像分类系统

目录 引言项目背景环境准备 硬件要求软件安装与配置系统设计 系统架构关键技术代码示例 数据预处理模型训练模型预测应用场景结论 1. 引言 图像分类是计算机视觉中的一个重要任务,目标是自动识别图像中的对象类别。通过卷积神经网络(CNN)等深度学习技术,我们可以构建高效的图像分类系统,广泛应用于自动驾驶、医疗影像诊断、监控分析等领域。本文将介绍如何构建一个基于人工智能的图像分类系统,包括环境

python: 多模块(.py)中全局变量的导入

文章目录 global关键字可变类型和不可变类型数据的内存地址单模块(单个py文件)的全局变量示例总结 多模块(多个py文件)的全局变量from x import x导入全局变量示例 import x导入全局变量示例 总结 global关键字 global 的作用范围是模块(.py)级别: 当你在一个模块(文件)中使用 global 声明变量时,这个变量只在该模块的全局命名空

【Python编程】Linux创建虚拟环境并配置与notebook相连接

1.创建 使用 venv 创建虚拟环境。例如,在当前目录下创建一个名为 myenv 的虚拟环境: python3 -m venv myenv 2.激活 激活虚拟环境使其成为当前终端会话的活动环境。运行: source myenv/bin/activate 3.与notebook连接 在虚拟环境中,使用 pip 安装 Jupyter 和 ipykernel: pip instal

【机器学习】高斯过程的基本概念和应用领域以及在python中的实例

引言 高斯过程(Gaussian Process,简称GP)是一种概率模型,用于描述一组随机变量的联合概率分布,其中任何一个有限维度的子集都具有高斯分布 文章目录 引言一、高斯过程1.1 基本定义1.1.1 随机过程1.1.2 高斯分布 1.2 高斯过程的特性1.2.1 联合高斯性1.2.2 均值函数1.2.3 协方差函数(或核函数) 1.3 核函数1.4 高斯过程回归(Gauss

【学习笔记】 陈强-机器学习-Python-Ch15 人工神经网络(1)sklearn

系列文章目录 监督学习:参数方法 【学习笔记】 陈强-机器学习-Python-Ch4 线性回归 【学习笔记】 陈强-机器学习-Python-Ch5 逻辑回归 【课后题练习】 陈强-机器学习-Python-Ch5 逻辑回归(SAheart.csv) 【学习笔记】 陈强-机器学习-Python-Ch6 多项逻辑回归 【学习笔记 及 课后题练习】 陈强-机器学习-Python-Ch7 判别分析 【学

nudepy,一个有趣的 Python 库!

更多资料获取 📚 个人网站:ipengtao.com 大家好,今天为大家分享一个有趣的 Python 库 - nudepy。 Github地址:https://github.com/hhatto/nude.py 在图像处理和计算机视觉应用中,检测图像中的不适当内容(例如裸露图像)是一个重要的任务。nudepy 是一个基于 Python 的库,专门用于检测图像中的不适当内容。该

pip-tools:打造可重复、可控的 Python 开发环境,解决依赖关系,让代码更稳定

在 Python 开发中,管理依赖关系是一项繁琐且容易出错的任务。手动更新依赖版本、处理冲突、确保一致性等等,都可能让开发者感到头疼。而 pip-tools 为开发者提供了一套稳定可靠的解决方案。 什么是 pip-tools? pip-tools 是一组命令行工具,旨在简化 Python 依赖关系的管理,确保项目环境的稳定性和可重复性。它主要包含两个核心工具:pip-compile 和 pip

HTML提交表单给python

python 代码 from flask import Flask, request, render_template, redirect, url_forapp = Flask(__name__)@app.route('/')def form():# 渲染表单页面return render_template('./index.html')@app.route('/submit_form',

PDF 软件如何帮助您编辑、转换和保护文件。

如何找到最好的 PDF 编辑器。 无论您是在为您的企业寻找更高效的 PDF 解决方案,还是尝试组织和编辑主文档,PDF 编辑器都可以在一个地方提供您需要的所有工具。市面上有很多 PDF 编辑器 — 在决定哪个最适合您时,请考虑这些因素。 1. 确定您的 PDF 文档软件需求。 不同的 PDF 文档软件程序可以具有不同的功能,因此在决定哪个是最适合您的 PDF 软件之前,请花点时间评估您的

Python QT实现A-star寻路算法

目录 1、界面使用方法 2、注意事项 3、补充说明 用Qt5搭建一个图形化测试寻路算法的测试环境。 1、界面使用方法 设定起点: 鼠标左键双击,设定红色的起点。左键双击设定起点,用红色标记。 设定终点: 鼠标右键双击,设定蓝色的终点。右键双击设定终点,用蓝色标记。 设置障碍点: 鼠标左键或者右键按着不放,拖动可以设置黑色的障碍点。按住左键或右键并拖动,设置一系列黑色障碍点