圖像混合,其實就是兩張圖像依照一定的比例進行疊加 公式如下
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);
} //圓的繪製與橢圓的繪製大同小異