返回> 网站首页 

Opencv图像旋转、镜像、提高亮度

yoours2024-07-31 19:21:25 阅读 211

简介一边听听音乐,一边写写文章。

一、图像中心点旋转

cv::Mat ImageRotate(cv::Mat img, int angle)

{

// 获取图像中心点

int w = img.cols;

int h = img.rows;

float CenterX = 1.0 * w / 2;

float CenterY = 1.0 * h / 2;


// 旋转angle度

cv::Mat m= cv::getRotationMatrix2D(cv::Point2f(CenterX, CenterY), angle, 1.0);

// 计算旋转后图像的边界框

cv::Rect bbox = cv::RotatedRect(cv::Point2f(CenterX, CenterY), cv::Size2f(w, h), angle).boundingRect();


// 创建输出图像

cv::Mat dst(bbox.height, bbox.width, img.type());

// 平移

m.at<double>(0, 2) += bbox.width/2 - CenterX;

m.at<double>(1, 2) += bbox.height/2 - CenterY;

// 仿射变换

cv::warpAffine(img, dst, m, cv::Size(bbox.width, bbox.height));

//cv::imwrite("abc.jpg", dst);

return dst;

}


二、镜像

void MirrorFlip(cv::Mat& img, int type)

{

// 0 垂直翻转 1 水平翻转 -1 水平和垂直翻转

if (type == 0)

return;

if(type==1)

cv::flip(img, img, 0);

if(type==2)

cv::flip(img, img, 1);

if (type == 3)

cv::flip(img, img, -1);

}


三、亮度

// 亮度方法一

cv::add(image, cv::Scalar(Brightness, Brightness, Brightness), image);

// 亮度方法二

image.convertTo(image, -1, 1, Brightness);

// 亮度方法三

cv::Mat lookUpTable(1, 256, CV_8U);

uchar* p = lookUpTable.ptr();

for (int i=0; i < 256; ++i)

p[i] = cv::saturate_cast<uchar>(pow(i / 255.0, Brightness/100.0) * 255.0);

cv::LUT(image, lookUpTable, image);


微信小程序扫码登陆

文章评论

211人参与,0条评论