图像混合,其实就是两张图像依照一定的比例进行叠加 公式如下
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的比列叠加而成
图像的对比度与亮度调节
其公式为 其中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);
} //圆的绘制与椭圆的绘制大同小异