图像混合,对比度亮度调节,绘制线条,矩形,圆,椭圆--OpenCV03

图像混合,其实就是两张图像依照一定的比例进行叠加 公式如下

 

g(x)=(1-alpha)*f_{1} (x)+ alpha*f_{2}(x)

alpha为0-1的数字 代表了图像的占比 

void MixImg(Mat& img1, Mat& img2, Mat& outimg)
{
    //开始进行图像的混合
    double alpha = 0.5;
    addWeighted(img1, alpha, img2, 1 - alpha, 0, outimg);

    imshow("after mix", outimg);
}

效果如上,图像为lena与纯绿图像按0.5的比列叠加而成

 

图像的对比度与亮度调节

其公式为 g(x) = alpha*f(x)+gate  其中alpha的增益代表对比度强度 , gate代表亮度加强

void StrangerImg(Mat& Img1)
{
    Mat OutImg(Img1.size(), Img1.type());
    //MixImg(Img1, Img2, OutImg); //图像混合

    //调整图像对比度与亮度
    double alpha = 1.2; //调整对比度
    double gate = 10;   //调整亮度
    for (int row = 0;row < OutImg.rows;row++)
        for (int col = 0;col < OutImg.cols;col++)
        {
            double B = Img1.at<Vec3b>(row, col)[0];
            double G = Img1.at<Vec3b>(row, col)[1];
            double R = Img1.at<Vec3b>(row, col)[2];

            //开始元素赋值操作
            OutImg.at<Vec3b>(row, col)[0] = saturate_cast<uchar>(B * alpha + gate);
            OutImg.at<Vec3b>(row, col)[1] = saturate_cast<uchar>(G * alpha + gate);
            OutImg.at<Vec3b>(row, col)[2] = saturate_cast<uchar>(R * alpha + gate);
            // saturate_cast函数可以把后面的值限制在0-255以内 超过255的都是255 小于0的都是0
        }

    //操作完成
    imshow("Output Img", OutImg);
}

 

绘制线条

void DrawLine(Mat& Img)
{
    Point P1 = Point(20, 30);  //在第30行20列
    //x元素代表列 y元素代表行 坐标轴在左上角建立
    Point P2;
    P2.x = 300;
    P2.y = 300;
    Scalar color = Scalar(0, 0, 255);

    line(Img, P1, P2, color, 2, LINE_8); //2代表线宽 LINE_8使用的是默认值

    imshow("Output image", Img);
}

绘制矩形框 

void DrawRect(Mat& Img)
{
    //首先要确定矩形的大小
    Rect rect;
    rect.x = 20;
    rect.y = 30;
    rect.height = 200; //代表矩形的长度 所占行
    rect.width = 300;  //矩形的宽度 所占列

    rectangle(Img, rect, Scalar(255, 0, 0), 1, LINE_8);

    imshow("Output image", Img);
}

绘制椭圆

void DrawEllipse(Mat& Img)
{
    //找出椭圆的圆心
    Point center = Point(Img.cols / 2, Img.rows / 2);//选择圆心为图像的中心
    //规定椭圆长半轴 短半轴
    Size  R = Size(200, 100);

    Scalar color = Scalar(255, 0, 0);
    ellipse(Img, center, R, 0, 0, 360, color, 1,LINE_AA);
    //要画椭圆的图像 中心点 长短半轴尺寸 0代表开始时偏移的角度  
    //0-360代表画一整个椭圆 颜色 1代表粗细 LINE_AA表示描绘出来的线条
    //LINE_AA为画出的线条 线条比LINE_8更加平滑
    imshow("Output image", Img);

}

绘制圆形 

void DrawCirle(Mat& Img)
{
    Point center(Img.cols/2,Img.rows/2);
    int R = 150;//半径元素

    Scalar color(0, 0, 255);

    circle(Img, center, R, color, 1, LINE_AA);

    imshow("Output image", Img);
} //圆的绘制与椭圆的绘制大同小异

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值