openCV - 5~7 图像混合、调整图像亮度与对比度、绘制形状与文字
5. 图像混合
理论-线性混合操作、相关API(addWeighted)
理论-线性混合操作
用到的公式 (其中 α 的取值范围为0~1之间)
相关API(addWeighted)
参数1:输入图像Mat – src1
参数2:输入图像src1的alpha值
参数3:输入图像Mat – src2
参数4:输入图像src2的alpha值
参数5:gamma值
参数6:输出混合图像
注意点:两张图像的大小和类型必须一致才可以
代码演示
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace std;
using namespace cv;
int main(int argc, char** argv) {
Mat src1, src2, dest;
src1 = imread("D:/vcprojects/images/LinuxLogo.jpg");
src2 = imread("D:/vcprojects/images/win7logo.jpg");
if (!src1.data) {
printf("could not load LinuxLogo image...\n");
return -1;
}
if (!src2.data) {
printf("could not load win7logo image...\n");
return -1;
}
if (src1.rows == src2.rows && src1.cols == src2.cols) {
double alpha = 0.5;
namedWindow("line-blend", CV_WINDOW_AUTOSIZE);
addWeighted(src1, (1 - alpha), src2, alpha, 0.0, dest);
imshow("line-blend", dest);
waitKey(0);
return 0;
}
else {
printf("image size is not same...\n");
return -1;
}
}
6. 调整图像亮度与对比度
理论
图像变换可以看作如下:
-
像素变换 – 点操作
-
邻域操作 – 区域
调整图像亮度和对比度属于像素变换 - 点操作
重要的api
Mat new_image = Mat::zeros( image.size(), image.type() );
创建一张跟原图像大小和类型一致的空白图像、像素值初始化为0saturate_cast<uchar>(value)
确保值大小范围为0~255之间Mat.at<Vec3b>(y,x)[index]=value
给每个像素点每个通道赋值
代码演示
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
int main(int argc, char** argv) {
Mat src, dst;
src = imread("D:/vcprojects/images/test.png");
if (!src.data) {
printf("could not load image...\n");
return -1;
}
char input_win[] = "input image";
cvtColor(src, src, CV_BGR2GRAY);
namedWindow(input_win, CV_WINDOW_AUTOSIZE);
imshow(input_win, src);
// contrast and brigthtness changes
int height = src.rows;
int width = src.cols;
dst = Mat::zeros(src.size(), src.type());
float alpha = 1.2;
float beta = 30;
Mat m1;
src.convertTo(m1, CV_32F);
for (int row = 0; row < height; row++) {
for (int col = 0; col < width; col++) {
if (src.channels() == 3) {
float b = m1.at<Vec3f>(row, col)[0];// blue
float g = m1.at<Vec3f>(row, col)[1]; // green
float r = m1.at<Vec3f>(row, col)[2]; // red
// output
dst.at<Vec3b>(row, col)[0] = saturate_cast<uchar>(b*alpha + beta);
dst.at<Vec3b>(row, col)[1] = saturate_cast<uchar>(g*alpha + beta);
dst.at<Vec3b>(row, col)[2] = saturate_cast<uchar>(r*alpha + beta);
}
else if (src.channels() == 1) {
float v = src.at<uchar>(row, col);
dst.at<uchar>(row, col) = saturate_cast<uchar>(v*alpha + beta);
}
}
}
char output_title[] = "contrast and brightness change demo";
namedWindow(output_title, CV_WINDOW_AUTOSIZE);
imshow(output_title, dst);
waitKey(0);
return 0;
}
7. 绘制形状与文字
使用cv::Point与cv::Scalar、绘制线、矩形、园、椭圆等基本几何形状、随机生成与绘制文本
使用cv::Point与cv::Scalar
-
Point表示2D平面上一个点x,y
Point p; p.x = 10; p.y = 8; or p = Pont(10,8);
-
Scalar表示四个元素的向量
Scalar(a, b, c);// a = blue, b = green, c = red表示RGB三个通道
绘制线、矩形、园、椭圆等基本几何形状
- 画线 cv::line (LINE_4\LINE_8\LINE_AA)
- 画椭圆cv::ellipse
- 画矩形cv::rectangle
- 画圆cv::circle
- 画填充cv::fillPoly
随机数生成cv::RNG
- 生成高斯随机数
gaussian (double sigma)
- 生成正态分布随机数
uniform (int a, int b)
绘制添加文字
putText函数 中设置fontFace(cv::HersheyFonts)
fontFace, CV_FONT_HERSHEY_PLAIN
fontScale , 1.0, 2.0~ 8.0
代码演示
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace std;
using namespace cv;
Mat bgImage;
const char* drawdemo_win = "draw shapes and text demo";
void MyLines();
void MyRectangle();
void MyEllipse();
void MyCircle();
void MyPolygon();
void RandomLineDemo();
int main(int argc, char** argv) {
bgImage = imread("D:/vcprojects/images/test1.png");
if (!bgImage.data) {
printf("could not load image...\n");
return -1;
}
//MyLines();
//MyRectangle();
//MyEllipse();
//MyCircle();
//MyPolygon();
//putText(bgImage, "Hello OpenCV", Point(300, 300), CV_FONT_HERSHEY_COMPLEX, 1.0, Scalar(12, 23, 200), 3, 8);
//namedWindow(drawdemo_win, CV_WINDOW_AUTOSIZE);
//imshow(drawdemo_win, bgImage);
RandomLineDemo();
waitKey(0);
return 0;
}
void MyLines() {
Point p1 = Point(20, 30);
Point p2;
p2.x = 400;
p2.y = 400;
Scalar color = Scalar(0, 0, 255);
line(bgImage, p1, p2, color, 1, LINE_AA);
}
void MyRectangle() {
Rect rect = Rect(200, 100, 300, 300);
Scalar color = Scalar(255, 0, 0);
rectangle(bgImage, rect, color, 2, LINE_8);
}
void MyEllipse() {
Scalar color = Scalar(0, 255, 0);
ellipse(bgImage, Point(bgImage.cols / 2, bgImage.rows / 2), Size(bgImage.cols / 4, bgImage.rows / 8), 90, 0, 360, color, 2, LINE_8);
}
void MyCircle() {
Scalar color = Scalar(0, 255, 255);
Point center = Point(bgImage.cols / 2, bgImage.rows / 2);
circle(bgImage, center, 150, color, 2, 8);
}
void MyPolygon() {
Point pts[1][5];
pts[0][0] = Point(100, 100);
pts[0][1] = Point(100, 200);
pts[0][2] = Point(200, 200);
pts[0][3] = Point(200, 100);
pts[0][4] = Point(100, 100);
const Point* ppts[] = { pts[0] };
int npt[] = { 5 };
Scalar color = Scalar(255, 12, 255);
fillPoly(bgImage, ppts, npt, 1, color, 8);
}
void RandomLineDemo() {
RNG rng(12345);
Point pt1;
Point pt2;
Mat bg = Mat::zeros(bgImage.size(), bgImage.type());
namedWindow("random line demo", CV_WINDOW_AUTOSIZE);
for (int i = 0; i < 100000; i++) {
pt1.x = rng.uniform(0, bgImage.cols);
pt2.x = rng.uniform(0, bgImage.cols);
pt1.y = rng.uniform(0, bgImage.rows);
pt2.y = rng.uniform(0, bgImage.rows);
Scalar color = Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255));
if (waitKey(50) > 0) {
break;
}
line(bg, pt1, pt2, color, 1, 8);
// 生成随机彩线
imshow("random line demo", bg);
}
}
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
static const char WINTITLE[] = "randomlines-demo";
int drawRandomLines(Mat image) {
RNG rng(0xffffff);
Point pt1, pt2;
for (int i = 0; i < 100000; i++) {
pt1.x = rng.uniform(0, image.cols);
pt2.x = rng.uniform(0, image.cols);
pt1.y = rng.uniform(0, image.rows);
pt2.y = rng.uniform(0, image.rows);
int r = rng.uniform(0, 255);
int g = rng.uniform(0, 255);
int b = rng.uniform(0, 255);
line(image, pt1, pt2, Scalar(b, g, r), 1, LINE_8);
// 添加文本
putText(image, "Open CV Core Tutorial", Point(image.cols / 2-200, image.rows / 2),
CV_FONT_HERSHEY_PLAIN, 2.0, Scalar(0, 255, 0), 3, LINE_8);
imshow(WINTITLE, image);
if (waitKey(10) >= 0)
{
return -1;
}
}
return 0;
}
int main(int argc, char** argv) {
Mat image = Mat::zeros(Size(450, 450), CV_8UC3);
namedWindow(WINTITLE, CV_WINDOW_AUTOSIZE);
int ok = drawRandomLines(image);
if (ok != 0) {
return 0;
}
return 0;
}