将RGB图像转为YCbCr,并显各通道图片

   日期:2020-05-14     浏览:241    评论:0    
核心提示:转换公式按照如下转换公式利用opencv编写代码。运行环境win10+opencv2.4.9+VS2015#include stdafx.h#include#include#include#include#includeusing namespace std;using namespace cv;Mat BGR人工智能

转换公式

按照如下转换公式利用opencv编写代码。
运行环境win10+opencv2.4.9+VS2015

#include "stdafx.h"
#include<opencv2/opencv.hpp>
#include<iostream>
#include<math.h>
#include<math.h>
#include<complex>
using namespace std;
using namespace cv;

Mat BGR2YCbCr(Mat img, Mat out)
{
	int width = img.rows;
	int height = img.cols;

	//cv::Mat out = cv::Mat::zeros(height, width, CV_32F);

	for (int j = 0; j < width    ; j++) {
		for (int i = 0; i < height; i++) {
			// Y
			out.at<cv::Vec3b>(j, i)[0] = (int)((float)img.at<cv::Vec3b>(j, i)[0] * 0.114 + 
				(float)img.at<cv::Vec3b>(j, i)[1] * 0.5870 + 
				(float)img.at<cv::Vec3b>(j, i)[2] * 0.299);

			// Cb
			out.at<cv::Vec3b>(j, i)[1] = (int)((float)img.at<cv::Vec3b>(j, i)[0] * 0.5 + 
				(float)img.at<cv::Vec3b>(j, i)[1] * (-0.3323) + 
				(float)img.at<cv::Vec3b>(j, i)[2] * (-0.1687) + 128);

			// Cr
			out.at<cv::Vec3b>(j, i)[2] = (int)((float)img.at<cv::Vec3b>(j, i)[0] * (-0.0813) + 
				(float)img.at<cv::Vec3b>(j, i)[1] * (-0.4187) + 
				(float)img.at<cv::Vec3b>(j, i)[2] * 0.5 + 128);
		}
	}
	return out;
}

int main()
{
	Mat img = imread("2.jpg", cv::IMREAD_COLOR);
	imshow("齐宝宝", img);
	waitKey(0);
	int width = img.rows;
	int height = img.cols;
	
	
	
	Mat ycbcr = img.clone();
	ycbcr = BGR2YCbCr(img, ycbcr);
	
	imshow("转换后", ycbcr);
	waitKey(0);
	Mat Y,Cb,Cr;
	vector<Mat> Ychannel, Cbchannel, Crchannel, channels;
	
	split(ycbcr, channels);
	Mat Yimage = channels.at(0);
	Mat Cbimage= channels.at(1);
	Mat Crimage = channels.at(2);
	imshow("Yimg", Yimage);
	imshow("Cbimage", Cbimage);
	imshow("Crimage", Crimage);
	waitKey(0);

结果

原图

转换后

Y通道

Cb通道

Cr通道

 
打赏
 本文转载自:网络 
所有权利归属于原作者,如文章来源标示错误或侵犯了您的权利请联系微信13520258486
更多>最近资讯中心
更多>最新资讯中心
更多>相关资讯中心
0相关评论

推荐图文
推荐资讯中心
点击排行
最新信息
新手指南
采购商服务
供应商服务
交易安全
关注我们
手机网站:
新浪微博:
微信关注:

13520258486

周一至周五 9:00-18:00
(其他时间联系在线客服)

24小时在线客服