【超分辨率】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

相关文章

基于Linux的ffmpeg python的关键帧抽取

《基于Linux的ffmpegpython的关键帧抽取》本文主要介绍了基于Linux的ffmpegpython的关键帧抽取,实现以按帧或时间间隔抽取关键帧,文中通过示例代码介绍的非常详细,对大家的学... 目录1.FFmpeg的环境配置1) 创建一个虚拟环境envjavascript2) ffmpeg-py

python使用库爬取m3u8文件的示例

《python使用库爬取m3u8文件的示例》本文主要介绍了python使用库爬取m3u8文件的示例,可以使用requests、m3u8、ffmpeg等库,实现获取、解析、下载视频片段并合并等步骤,具有... 目录一、准备工作二、获取m3u8文件内容三、解析m3u8文件四、下载视频片段五、合并视频片段六、错误

Python中提取文件名扩展名的多种方法实现

《Python中提取文件名扩展名的多种方法实现》在Python编程中,经常会遇到需要从文件名中提取扩展名的场景,Python提供了多种方法来实现这一功能,不同方法适用于不同的场景和需求,包括os.pa... 目录技术背景实现步骤方法一:使用os.path.splitext方法二:使用pathlib模块方法三

Python打印对象所有属性和值的方法小结

《Python打印对象所有属性和值的方法小结》在Python开发过程中,调试代码时经常需要查看对象的当前状态,也就是对象的所有属性和对应的值,然而,Python并没有像PHP的print_r那样直接提... 目录python中打印对象所有属性和值的方法实现步骤1. 使用vars()和pprint()2. 使

CSS实现元素撑满剩余空间的五种方法

《CSS实现元素撑满剩余空间的五种方法》在日常开发中,我们经常需要让某个元素占据容器的剩余空间,本文将介绍5种不同的方法来实现这个需求,并分析各种方法的优缺点,感兴趣的朋友一起看看吧... css实现元素撑满剩余空间的5种方法 在日常开发中,我们经常需要让某个元素占据容器的剩余空间。这是一个常见的布局需求

使用Python和OpenCV库实现实时颜色识别系统

《使用Python和OpenCV库实现实时颜色识别系统》:本文主要介绍使用Python和OpenCV库实现的实时颜色识别系统,这个系统能够通过摄像头捕捉视频流,并在视频中指定区域内识别主要颜色(红... 目录一、引言二、系统概述三、代码解析1. 导入库2. 颜色识别函数3. 主程序循环四、HSV色彩空间详解

一文深入详解Python的secrets模块

《一文深入详解Python的secrets模块》在构建涉及用户身份认证、权限管理、加密通信等系统时,开发者最不能忽视的一个问题就是“安全性”,Python在3.6版本中引入了专门面向安全用途的secr... 目录引言一、背景与动机:为什么需要 secrets 模块?二、secrets 模块的核心功能1. 基

python常见环境管理工具超全解析

《python常见环境管理工具超全解析》在Python开发中,管理多个项目及其依赖项通常是一个挑战,下面:本文主要介绍python常见环境管理工具的相关资料,文中通过代码介绍的非常详细,需要的朋友... 目录1. conda2. pip3. uvuv 工具自动创建和管理环境的特点4. setup.py5.

Python常用命令提示符使用方法详解

《Python常用命令提示符使用方法详解》在学习python的过程中,我们需要用到命令提示符(CMD)进行环境的配置,:本文主要介绍Python常用命令提示符使用方法的相关资料,文中通过代码介绍的... 目录一、python环境基础命令【Windows】1、检查Python是否安装2、 查看Python的安

Python UV安装、升级、卸载详细步骤记录

《PythonUV安装、升级、卸载详细步骤记录》:本文主要介绍PythonUV安装、升级、卸载的详细步骤,uv是Astral推出的下一代Python包与项目管理器,主打单一可执行文件、极致性能... 目录安装检查升级设置自动补全卸载UV 命令总结 官方文档详见:https://docs.astral.sh/