1、调整图像对比度
#include<opencv2/opencv.hpp>
#include<iostream>
#include<math.h>
using namespace std;
using namespace cv;
int main()
{
Mat src = imread("C:/Users/admin/Desktop/1.JPG");
if (!src.data)
{
printf("没找到");
return -1;
}
Mat src1;
cvtColor(src, src1, COLOR_BGR2GRAY);
Mat dst = Mat::zeros(src.size(), src.type());
float k = 1.2, c = 80;
int width = src.cols;
int height = src.rows;
Mat m1;
src.convertTo(m1, CV_32F);//转float
for (int row = 0; row < height; row++)
{
for (int col = 0; col < width; col++)
{
if (src.channels()==3)
{
//拿到src的每个像素点
/*int b = src.at<Vec3b>(row, col)[0];
int g = src.at<Vec3b>(row, col)[1];
int r = src.at<Vec3b>(row, col)[2];*/
float b = m1.at<Vec3f>(row, col)[0];
float g = m1.at<Vec3f>(row, col)[1];
float r = m1.at<Vec3f>(row, col)[2];
dst.at<Vec3b>(row, col)[0] = saturate_cast<uchar>(k*b + c);
dst.at<Vec3b>(row, col)[1] = saturate_cast<uchar>(k*b + c);
dst.at<Vec3b>(row, col)[2] = saturate_cast<uchar>(k*b + c);
}
else if (src.channels()==1)
{
int r = src.at<uchar>(row, col);
dst.at<uchar>(row, col) = saturate_cast<uchar>(k*r + c);
}
}
}
namedWindow("dstImg window", CV_WINDOW_AUTOSIZE);
imshow("dstImg window", dst);
namedWindow("srcImg window", CV_WINDOW_AUTOSIZE);
imshow("srcImg window", src);
waitKey(0);
return 0;
}
2、绘制形状文字
#include <iostream>
#include<opencv2/opencv.hpp>
using namespace std;
using namespace cv;
Mat bgImg;
const char* drawDome = "draw shapes and text Dome";
void myLines();
void myRectangle();
void myEllipse();
void myCircle();
void myPolygon();
void randomLine();
int main()
{
bgImg = imread("C:/Users/admin/Desktop/1.JPG");
if (!bgImg.data)
{
printf("未找到图像");
return -1;
}
/*putText(bgImg, "Hello world", Point(100, 100), CV_FONT_HERSHEY_COMPLEX, 2.0, Scalar(23, 155, 11), 1, CV_AA);
myLines();
myCircle();
myRectangle();
myEllipse();
myPolygon();*/
randomLine();
namedWindow(drawDome, CV_WINDOW_AUTOSIZE);
imshow(drawDome, bgImg);
waitKey(0);
return 0;
}
void myLines()
{
Point p1 = Point(20, 30);
Point p2;
p2.x = 300;
p2.y = 300;
Scalar color = Scalar(0, 0, 255);
line(bgImg, p1, p2, color, 1, CV_AA);
}
void myRectangle()
{
Rect rect = Rect(200, 100, 300, 300);
rectangle(bgImg, rect, Scalar(255, 0, 0), 1, 8, 0);
}
void myEllipse()
{
ellipse(bgImg, Point(bgImg.cols / 2, bgImg.rows / 2), Size(bgImg.cols / 4, bgImg.rows / 8), 90, 0, 360, Scalar(0, 255, 0), 1, CV_AA);
}
void myCircle()
{
Scalar color = Scalar(0, 255, 255);
Point center = Point(bgImg.cols / 2, bgImg.rows / 2);
circle(bgImg, center, 100, color, 2, CV_AA);
}
//绘制多边形
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(bgImg, ppts, npt, 1, color, CV_AA);
}
void randomLine()
{
RNG reg(12345);//生成随机数,传入随机种子
Point p1, p2;
Mat randomImg = Mat::zeros(bgImg.size(), bgImg.type());
for (int i = 0; i < 10000; i++)
{
p1.x = reg.uniform(0, randomImg.cols);
p1.y = reg.uniform(0, randomImg.rows);
p2.x = reg.uniform(0, randomImg.cols);
p2.y = reg.uniform(0, randomImg.rows);
line(randomImg, p1, p2, Scalar(reg.uniform(0, 255), reg.uniform(0, 255), reg.uniform(0, 255)), 1, CV_AA);
if (waitKey(50)>0)
{
break;
}
namedWindow("randomImg window", CV_WINDOW_AUTOSIZE);
imshow("randomImg window", randomImg);
}
}