opencv学习之core模块核心功能2

4 使用OpenCV对两幅图像求和(求混合(blending))

线性混合操作也是一种典型的二元(两个输入)的 像素操作在这里插入图片描述

通过在范围0—1内改变 alpha,这个操可以用来对两幅图像或两段视频产生时间上的画面叠化

#include <cv.h>
#include <highgui.h>
#include <iostream>

using namespace cv;

int main( int argc, char** argv )
{
 double alpha = 0.5; double beta; double input;

 Mat src1, src2, dst;

 /// Ask the user enter alpha
 std::cout<<" Simple Linear Blender "<<std::endl;
 std::cout<<"-----------------------"<<std::endl;
 std::cout<<"* Enter alpha [0-1]: ";
 std::cin>>input;

 /// We use the alpha provided by the user iff it is between 0 and 1
 if( alpha >= 0 && alpha <= 1 )
   { alpha = input; }

 /// 加载图像,必须相同类型和大小。
 src1 = imread("../../images/LinuxLogo.jpg");
 src2 = imread("../../images/WindowsLogo.jpg");

 if( !src1.data ) { printf("Error loading src1 \n"); return -1; }
 if( !src2.data ) { printf("Error loading src2 \n"); return -1; }

 /// Create Windows
 namedWindow("Linear Blend", 1);

 beta = ( 1.0 - alpha );
 addWeighted( src1, alpha, src2, beta, 0.0, dst);//计算如下图,0.0为gamma

 imshow( "Linear Blend", dst );

 waitKey(0);
 return 0;
}

在这里插入图片描述

5 改变图像的对比度和亮度

图像处理算子是带有一幅或多幅输入图像、产生一幅输出图像的函数。图像变换可分为以下两种:点算子(像素变换)与邻域(基于区域的)算子。

5.1 像素变换

在这一类图像处理变换中,仅仅根据输入像素值(有时可加上某些全局信息或参数)计算相应的输出像素值。这类算子包括 亮度和对比度调整 ,以及颜色校正和变换。

5.2亮度和对比度调整

两种常用的点过程(即点算子),是用常数对点进行 乘法和加法运算:

在这里插入图片描述

两个参数 alpha > 0和 beta一般称作 增益和偏置参数。

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>

using namespace std;
using namespace cv;

double alpha; /**< 控制对比度 */
int beta;  /**< 控制亮度 */

int main( int argc, char** argv )
{
    /// 读入用户提供的图像
    Mat image = imread( argv[1] );
    Mat new_image = Mat::zeros( image.size(), image.type() );//Matlab风格的初始化方式

    /// 初始化
    cout << " Basic Linear Transforms " << endl;
    cout << "-------------------------" << endl;
    cout << "* Enter the alpha value [1.0-3.0]: ";
    cin >> alpha;
    cout << "* Enter the beta value [0-100]: ";
    cin >> beta;

    /// 执行运算 new_image(i,j) = alpha*image(i,j) + beta
    for( int y = 0; y < image.rows; y++ )
    {
        for( int x = 0; x < image.cols; x++ )
        {
            for( int c = 0; c < 3; c++ )
            {
                new_image.at<Vec3b>(y,x)[c] = saturate_cast<uchar>( alpha*( image.at<Vec3b>(y,x)[c] ) + beta );//y 是像素所在的行, x 是像素所在的列, saturate_cast 对结果进行转换,以确保它为有效值。
            }
        }
    }

    /// 创建窗口
    namedWindow("Original Image", 1);
    namedWindow("New Image", 1);

    /// 显示图像
    imshow("Original Image", image);
    imshow("New Image", new_image);

    /// 等待用户按键
    waitKey();
    return 0;
}

可以不用 for循环来访问每个像素,而是直接采用下面这个命令

image.convertTo(new_image, -1, alpha, beta);

第二个参数为type,是需要的输出矩阵类型,或者更明确的,是输出矩阵的深度,如果是负值(常用-1)则输出矩阵和输入矩阵类型相同;

6 基本绘图

在这里插入图片描述

次数据结构表示了由其图像坐标 x和 y指定的2D点。可定义为:

Point pt;
pt.x = 10;
pt.y = 8;
//或者
Point pt =  Point(10, 8);

Scalar表示了具有4个元素的数组。次类型在OpenCV中被大量用于传递像素值。如果用不到第四个参数,则无需定义。

Scalar( a, b, c )

那么定义的RGB颜色值为: Red = c, Green = b and Blue = a

/// 窗口名字
char atom_window[] = "Drawing 1: Atom";
char rook_window[] = "Drawing 2: Rook";

/// 创建空全黑像素的空图像
Mat atom_image = Mat::zeros( w, w, CV_8UC3 );
Mat rook_image = Mat::zeros( w, w, CV_8UC3 );
/// 1. 画一个简单的原子。

/// 1.a. 创建椭圆.用 MyEllipse 和 MyFilledCircle 来画原子。
MyEllipse( atom_image, 90 );
MyEllipse( atom_image, 0 );
MyEllipse( atom_image, 45 );
MyEllipse( atom_image, -45 );

/// 1.b. 创建圆
MyFilledCircle( atom_image, Point( w/2.0, w/2.0) );
/// 2. 画一个赌棍

/// 2.a. 创建一个凸多边形
MyPolygon( rook_image );

/// 2.b. 创建矩形
rectangle( rook_image,
           Point( 0, 7*w/8.0 ),
           Point( w, w),
           Scalar( 0, 255, 255 ),
           -1,
           8 );

/// 2.c. 画几条直线
MyLine( rook_image, Point( 0, 15*w/16 ), Point( w, 15*w/16 ) );
MyLine( rook_image, Point( w/4, 7*w/8 ), Point( w/4, w ) );
MyLine( rook_image, Point( w/2, 7*w/8 ), Point( w/2, w ) );
MyLine( rook_image, Point( 3*w/4, 7*w/8 ), Point( 3*w/4, w ) );
//函数的定义
void MyLine( Mat img, Point start, Point end )
{
  int thickness = 2;
  int lineType = 8;
  line( img,               //写入的图像
        start,             //起点
        end,               //终点
        Scalar( 0, 0, 0 ), //颜色
        thickness,         //线的粗细
        lineType );        //线的类型,可以取值8, 4, 和CV_AA, 分别代表8邻接连接线,4邻接连接线和反锯齿连接线。默认值为8邻接。
}
void MyEllipse( Mat img, double angle )
{
  int thickness = 2;
  int lineType = 8;
  ellipse( img,
           Point( w/2.0, w/2.0 ),     //椭圆中心
           Size( w/4.0, w/16.0 ),     //大小
           angle,                     //椭圆旋转角度
           0,
           360,                       //椭圆扩展的弧度从 0 度到 360 度
           Scalar( 255, 0, 0 ),
           thickness,
           lineType );
}
void MyFilledCircle( Mat img, Point center )
{
 int thickness = -1;
 int lineType = 8;

 circle( img,
         center,
         w/32.0,                        //圆的半径
         Scalar( 0, 0, 255 ),
         thickness,                      //圆被填充
         lineType );
}
rectangle( rook_image,
           Point( 0, 7*w/8.0 ),          //矩形两个对角顶点
           Point( w, w),
           Scalar( 0, 255, 255 ),
           -1,
           8 );
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值