您的位置:首页 > 其它

Core_划分YUV颜色平面,并打乱亮度通道,合并输出

2014-03-29 20:57 381 查看
Core_划分YUV颜色平面,并打乱亮度通道,合并输出-----------

#include <Windows.h>

#include <iostream>

#include "opencv.hpp"

using namespace cv;

using namespace std;

#define DEMO_MIXED_API_USE

int main()

{
char* filename="..//images//chicky_512.png";

#ifdef DEMO_MIXED_API_USE
Ptr<IplImage> IplI=cvLoadImage(filename);

if(IplI.empty())
{
cerr<<"cant not load image"<<filename<<endl;
return -1;
}
Mat I(IplI);

#else
Mat I=imread(filename);
if(I.empty())
{
cerr<<"can not load image "<<filename<<endl;
return -1;

#endif

Mat I_YUV;
cvtColor(I,I_YUV,CV_BGR2YCrCb);

vector<Mat> planes;
split(I_YUV,planes);

MatIterator_<uchar> it=planes[0].begin<uchar>(),it_end=planes[0].end<uchar>();
for (;it!=it_end;++it)
{
double v=*it*1.7 + rand()%21 -10;
*it=saturate_cast<uchar>(v*v/255);


Mat noisyI(I.size(),CV_8U);
randn(noisyI,Scalar::all(128),Scalar::all(20));

GaussianBlur(noisyI,noisyI,Size(3,3),0.5,0.5);

const double brightness_gain=0;
const double contrast_gain=1.7;

#ifdef DEMO_MIXED_API_USE
 IplImage cv_planes_0=planes[0],cv_noisy=noisyI;
 cvAddWeighted(&cv_planes_0,contrast_gain,&cv_noisy,1,-128+brightness_gain,&cv_planes_0);

#else

addWeighted(planes[0],contrast_gain,noisyI,1,-128+brightness_gain,planes[0]);

#endif

const double color_scale=0.5;

planes[1].convertTo(planes[1],planes[1].type(),color_scale,128*(1-color_scale));
planes[2]=Mat_<uchar>(planes[2]*color_scale + 128*(1-color_scale));

planes[0]=planes[0].mul(planes[0],1.0/255);

merge(planes,I_YUV);
cvtColor(I_YUV,I,CV_YCrCb2BGR);

namedWindow("image with grain",CV_WINDOW_AUTOSIZE);

#ifdef DEMO_MIXED_API_USE

    // this is to demonstrate that I and IplI really share the data - the result of the above

    // processing is stored in I and thus in IplI too.

    cvShowImage("image with grain", IplI);

#else

    imshow("image with grain", I); // the new MATLAB style function show

#endif

waitKey(0);

return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐