基于opencv对图像进行任意角度旋转,翻转,缩放,加噪,去噪,亮度均匀,反色代码演示
opencv
OpenCV: 开源计算机视觉库
项目地址:https://gitcode.com/gh_mirrors/opencv31/opencv
免费下载资源
·
目录
全程opencv+vs
很多都是opencv封装的库函数
拼凑一下,调调参就出了
程序设计毒瘤课
任意角度旋转:
原理可以参考(63条消息) 经验 | OpenCV图像旋转的原理与技巧_小白学视觉的博客-CSDN博客
#include<bits/stdc++.h>
#include<opencv2/opencv.hpp>
using namespace std;
const double pii = acosl(-1.0);
//计算点point2绕点point1逆时针旋转angle度后得到新的点newPoint
void rotatePoint(cv::Point& point1, cv::Point& point2, cv::Point& newPoint, double angle)
{
int dx, dy;
double dx1, dy1;
dy1 = -((double)point2.x - point1.x) * sin(angle) + ((double)point2.y - point1.y) * cos(angle);
dx1 = ((double)point2.x - point1.x) * cos(angle) + ((double)point2.y - point1.y) * sin(angle);
if (dx1 - (int)dx1 > 0.5) //做一个四舍五入
dx = (int)dx1 + 1;
else
{
if (dx1 - (int)dx1 < -0.5)
dx = (int)dx1 - 1;
else
dx = (int)(dx1);
}
if (dy1 - (int)dy1 > 0.5) //做一个四舍五入
dy = (int)dy1 + 1;
else
{
if (dy1 - (int)dy1 < -0.5)
dy = (int)dy1 - 1;
else
dy = (int)(dy1);
}
newPoint.x = point1.x + dx;
newPoint.y = point1.y + dy;
}
void translationPoint(cv::Point& point, int x, int y) //平移运算
{
point.x = point.x + x;
point.y = point.y + y;
}
int Max4(int a[4]) //获取四个数中的最大值
{
int max = a[0];
for (int i = 1; i < 4; i++)
{
if (max < a[i])
max = a[i];
}
return max;
}
int Min4(int a[4]) //获取四个数中的最小值
{
int min = a[0];
for (int i = 1; i < 4; i++)
{
if (min > a[i])
min = a[i];
}
return min;
}
int absMax4(int a[4])
{
int max = 0, m;
for (int i = 0; i < 4; i++)
{
if (a[i] < 0)
m = -a[i];
else m = a[i];
if (max < m)
max = m;
}
return max;
}
void rotateImage(cv::Mat inputMat, cv::Mat& outputMat, std::vector<cv::Point> points, cv::Point point, double angle)
{
std::vector<cv::Point> newPoints;
cv::Point newP;
for (int i = 0; i < 4; ++i)
{
if (points[i] != point) //判断输入的4个顶点是否与旋转点point相同
{
rotatePoint(point, points[i], newP, angle); //顶点points[i]与旋转点point不同,则进行旋转计算
newPoints.push_back(newP);
}
else
{
newPoints.push_back(points[i]);
}
}
//获取经旋转后,新图像的大小,其中w表示图像宽长,h表示图像高长。
int w = 0, h = 0;
int suw[4] = { newPoints[1].x - newPoints[0].x,newPoints[1].x - newPoints[3].x,
newPoints[2].x - newPoints[0].x,newPoints[2].x - newPoints[3].x };
int suh[4] = { newPoints[2].y - newPoints[0].y ,newPoints[2].y - newPoints[1].y,
newPoints[3].y - newPoints[0].y,newPoints[3].y - newPoints[1].y };
w = absMax4(suw);
h = absMax4(suh);
//获取需要旋转的四边形区域的外接矩形表示区域范围(x_min,y_min)、(x_max,y_max)
int y_max, y_min, x_max, x_min;
int points_x[4] = { points[0].x,points[1].x,points[2].x,points[3].x };
int points_y[4] = { points[0].y,points[1].y,points[2].y,points[3].y };
y_max = Max4(points_y);
y_min = Min4(points_y);
x_max = Max4(points_x);
x_min = Min4(points_x);
//计算向x轴的平移量dx,向y轴的平移量dy
int dx, dy;
int a[4] = { newPoints[0].x,newPoints[1].x,newPoints[2].x,newPoints[3].x };
int b[4] = { newPoints[0].y,newPoints[1].y,newPoints[2].y,newPoints[3].y };
dx = Min4(a);
dy = Min4(b);
//初始化输出矩阵
if (inputMat.type() == CV_8UC1)
cv::Mat(h, w, CV_8UC1, cv::Scalar::all(255)).copyTo(outputMat);
if (inputMat.type() == CV_8UC3)
cv::Mat(h, w, CV_8UC3, cv::Scalar(255, 255, 255)).copyTo(outputMat);
//实现I(x',y')=I(x,y)
double z1, z2, z3, z4;
for (int i = y_min; i < y_max; ++i)
{
for (int j = x_min; j < x_max; ++j)
{
//四边形顶点A为points[0],顶点B为points[1],顶点C为points[2],顶点D为points[3].
//直线AB
z1 = i - (double)points[0].y -
(j - (double)points[0].x) * ((double)points[0].y - points[1].y) / ((double)points[0].x - points[1].x);
//直线BC
z2 = j - (double)points[1].x -
(i - (double)points[1].y) * ((double)points[1].x - points[2].x) / ((double)points[1].y - points[2].y);
//直线CD
z3 = i - (double)points[2].y -
(j - (double)points[2].x) * ((double)points[2].y - points[3].y) / ((double)points[2].x - points[3].x);
//直线AD
z4 = j - (double)points[0].x -
(i - (double)points[0].y) * ((double)points[0].x - points[3].x) / ((double)points[0].y - points[3].y);
if (z1 >= 0 && z2 <= 0 && z3 <= 0 && z4 >= 0)
{
cv::Point point0(j, i);
rotatePoint(point, point0, point0, angle); //将点point0绕点point旋转angle度得到新的点point0
translationPoint(point0, -dx, -dy);
if (point0.x >= 0 && point0.x < w && point0.y >= 0 && point0.y < h)
{
if (inputMat.type() == CV_8UC1)
{
uchar* str = inputMat.ptr<uchar>(i);
outputMat.at<uchar>(point0.y, point0.x) = str[j];
}
if (inputMat.type() == CV_8UC3)
{
cv::Vec3b* str = inputMat.ptr<cv::Vec3b>(i);
outputMat.at<cv::Vec3b>(point0.y, point0.x) = str[j];
}
}
}
}
}
if (inputMat.type() == CV_8UC1)
{ //插值
for (int i = 1; i < outputMat.rows - 1; ++i)
{
for (int j = 1; j < outputMat.cols - 1; ++j)
{
if (outputMat.at<uchar>(i, j) == 255)
{
int sum = 0;
uchar* str1 = outputMat.ptr<uchar>(i - 1);
sum = str1[j - 1] + str1[j] + str1[j + 1];
uchar* str2 = outputMat.ptr<uchar>(i);
sum = sum + str2[j - 1] + str2[j + 1];
uchar* str3 = outputMat.ptr<uchar>(i + 1);
sum = sum + str3[j - 1] + str3[j] + str3[j + 1];
sum = sum / 8;
outputMat.at<uchar>(i, j) = (uchar)sum;
}
}
}
}
if (inputMat.type() == CV_8UC3)
{ //插值
for (int i = 1; i < outputMat.rows - 1; ++i)
{
for (int j = 1; j < outputMat.cols - 1; ++j)
{
if (outputMat.at<cv::Vec3b>(i, j) == cv::Vec3b(255, 255, 255))
{
int sum[3] = { 0,0,0 };
uchar r, g, b;
for (int k = 0; k < 3; k++)
{
cv::Vec3b* str1 = outputMat.ptr<cv::Vec3b>(i - 1);
sum[k] = str1[j - 1][k] + str1[j][k] + str1[j + 1][k];
cv::Vec3b* str2 = outputMat.ptr<cv::Vec3b>(i);
sum[k] = sum[k] + str2[j - 1][k] + str2[j + 1][k];
cv::Vec3b* str3 = outputMat.ptr<cv::Vec3b>(i + 1);
sum[k] = sum[k] + str3[j - 1][k] + str3[j][k] + str3[j + 1][k];
sum[k] = sum[k] / 8;
}
r = (uchar)sum[0];
g = (uchar)sum[1];
b = (uchar)sum[2];
outputMat.at<cv::Vec3b>(i, j) = cv::Vec3b(r, g, b);
}
}
}
}
}
int main()
{
cv::Mat srcImage = cv::imread("D:\\Cpan\\Download\\12.jpg");
if (srcImage.empty())
{
printf("图片读取失败!\n");
return -1;
}
//需要旋转的图像区域四个顶点
std::vector<cv::Point> points;
points.push_back(cv::Point(0, 0)); //顶点A
points.push_back(cv::Point(srcImage.cols, 0)); //顶点B
points.push_back(cv::Point(srcImage.cols, srcImage.rows)); //顶点C
points.push_back(cv::Point(0, srcImage.rows));
cv::Mat outputImage;
cout << "请输入旋转中心" << endl;
cout << "为方便用户在不知道总像素时操作,把图看成100*100的矩形,请输入相对旋转中心(例:50 50为中心点)" << endl;
int starx, stary;
cin >> starx >> stary;
starx = (double)starx / 100.0 * srcImage.cols;
stary = (double)stary / 100.0 * srcImage.rows;
cout << "请输入旋转的角度(逆时针)" << endl;
double poi;
cin >> poi;
rotateImage(srcImage, outputImage, points, cv::Point(starx, stary), poi / 180 * pii);
cv::imshow("原图", srcImage);
cv::imshow("旋转得到的图像", outputImage);
cv::waitKey(0);
return 0;
}
任意方向翻转
#include<opencv2/opencv.hpp>
#include<iostream>
using namespace cv;
using namespace std;
int main() {
Mat src = cv::imread("D:\\Cpan\\Download\\12.jpg");
if (src.empty()) {
printf("could not load image...\n");
return -1;
}
Mat dst;
// X Flip 倒影
char s[10] = "";
cout << "请输入翻转对称轴(x/y/xy)" << endl;
cin >> s;
imshow("input", src);
if (s[0] == 'x' && s[1] == 'y') {
flip(src, dst, -1);
imshow("xy-flip", dst);
}
else if (s[0] == 'y') {
flip(src, dst, 1);
imshow("y-flip", dst);
}
else if (s[0] == 'x') {
flip(src, dst, 0);
imshow("x-flip", dst);
}
else {
cout << "请输入正确的反转对称轴" << endl;
}
waitKey(0);
return 0;
}
缩放
#include<iostream>
#include"opencv2/imgproc/imgproc.hpp"
#include"opencv2/highgui/highgui.hpp"
#include<opencv2/core/core.hpp>
using namespace std;
using namespace cv;
int main()
{
//用mat读取
Mat src = cv::imread("D:\\Cpan\\Download\\12.jpg");
if (src.empty()) {
cout << "文件读取失败!" << endl;
system("pause");
return -1;
}
cout << "输入您想缩小/放大的比例(小于100为缩小,大于100为放大)" << endl;
cout << "x轴->____ " << "y轴变成->____" << endl;
double szx, szy;
cin >> szx >> szy;
szx /= 100;
szy /= 100;
imshow("src", src);//原图像显示
Mat res;
resize(src, res, Size(src.cols * szx, src.rows * szy), 0, 0, INTER_LINEAR);// X Y各缩小一半
imshow("res", res);//显示缩放过后的结果
waitKey(0);
return 0;
}
加噪(两种方法)
#include<opencv2/opencv.hpp>
#include<iostream>
using namespace cv;
using namespace std;
void addguessnoise(Mat src,int fz,int fm) {
Mat img_noise = Mat::zeros(src.rows, src.cols, src.type());
RNG rng; //创建一个RNG类
rng.fill(img_noise, RNG::NORMAL, 50, 50*fz*2/fm);
imshow("三通道高斯噪声", img_noise);
src = src + img_noise;
imshow("result", src);
}
void addluffynoise(Mat src,int fz,int fm) {
RNG rng(12345);
int w = src.cols;
int h = src.rows;
int nums = 100000;
for (int i = 0; i < nums; i++) {
if (i % fm >= fz) continue;
int x = rng.uniform(0, w);
int y = rng.uniform(0, h);
if (i % 2 == 1) {
src.at<Vec3b>(y, x) = Vec3b(255, 255, 255);
}
else {
src.at<Vec3b>(y, x) = Vec3b(0, 0, 0);
}
}
imshow("src", src);
}
int main() {
Mat src = cv::imread("D:\\Cpan\\Download\\12.jpg");
if (src.empty()) {
printf("could not load image...\n");
return -1;
}
char s[50];
cout << "请输入您想加噪的类型(g:高斯噪声)(l:椒盐噪声)" << endl;
cin >> s;
int fz, fm;
cout << "请输入您想加噪的密度(输入a,b代表a/b,越大密度越大)" << endl;
cin >> fz >> fm;
if (s[0] == 'g') {
addguessnoise(src,fz,fm);
}
else {
addluffynoise(src, fz, fm);
}
waitKey(0);
return 0;
}
去噪(四种方法)
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;
void add_salt_pepper_noise(Mat& image) {
RNG rng(12345);
int h = image.rows;
int w = image.cols;
int nums = 10000;
for (int i = 0; i < nums; i++) {
int x = rng.uniform(0, w);
int y = rng.uniform(0, h);
if (i % 2 == 1) {
image.at<Vec3b>(y, x) = Vec3b(255, 255, 255);
}
else {
image.at<Vec3b>(y, x) = Vec3b(0, 0, 0);
}
}
imshow("salt pepper", image);
}
void gaussian_noise(Mat& image) {
Mat noise = Mat::zeros(image.size(), image.type());
randn(noise, (15, 15, 15), (30, 30, 30));
Mat dst;
add(image, noise, dst);
imshow("gaussian noise", dst);
dst.copyTo(image);
}
int main() {
Mat src = cv::imread("D:\\Cpan\\Download\\12.jpg");
if (src.empty()) {
printf("could not load image...\n");
return -1;
}
cout << "请输入您想去噪的方法" << endl;
cout << "(1:均值滤波)(2:高斯滤波)(3:中值滤波)(4:非局部均值去噪)" << endl;
int ind;
cin >> ind;
namedWindow("input", WINDOW_AUTOSIZE);
gaussian_noise(src);
Mat result1, result2, result3, result4;
if (ind == 1) {
blur(src, result1, Size(5, 5));
imshow("result-1", result1);
}
else if (ind == 2) {
GaussianBlur(src, result2, Size(5, 5), 0);
imshow("result-2", result2);
}
else if (ind == 3) {
medianBlur(src, result3, 5);
imshow("result-3", result3);
}
else {
fastNlMeansDenoisingColored(src, result4, 15, 15, 10, 30);
imshow("result-4", result4);
}
waitKey(0);
return 0;
}
亮度均匀与反色
#include<opencv2/opencv.hpp>
#include<iostream>
#include <opencv2\imgproc\types_c.h>
using namespace cv;
using namespace std;
void light_avg(Mat srcImage) {
imshow("srcImage", srcImage);
Mat srcGray;
cvtColor(srcImage, srcGray, CV_BGR2GRAY);
Mat heqResult;
equalizeHist(srcGray, heqResult);
imshow("heqResult", heqResult);
}
void recolor(Mat src) {
imshow("src", src);
Mat dst;
dst.create(src.size(), src.type());
int height = src.rows;
int width = src.cols;
int nc = src.channels();
//b,g,r 三通道
int b;
int g;
int r;
for (int row = 0; row < height; row++)
{
for (int col = 0; col < width; col++)
{
b = src.at<Vec3b>(row, col)[0];
g = src.at<Vec3b>(row, col)[1];
r = src.at<Vec3b>(row, col)[2];
dst.at<Vec3b>(row, col)[0] = 255 - b;
dst.at<Vec3b>(row, col)[1] = 255 - g;
dst.at<Vec3b>(row, col)[2] = 255 - r;
}
}
imshow("dst", dst);
}
int main() {
Mat src = cv::imread("D:\\Cpan\\Download\\12.jpg");
if (src.empty()) {
printf("could not load image...\n");
return -1;
}
cout << "亮度均匀处理 or 反色处理(1或2)" << endl;
int ind;
cin >> ind;
if (ind == 1) {
light_avg(src);
}
else {
recolor(src);
}
waitKey(0);
return 0;
}
GitHub 加速计划 / opencv31 / opencv
77.36 K
55.71 K
下载
OpenCV: 开源计算机视觉库
最近提交(Master分支:1 个月前 )
3919f33e
G-API: Introduce level optimization flag for ONNXRT backend #26293
### Pull Request Readiness Checklist
See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request
- [x] I agree to contribute to the project under Apache 2 License.
- [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV
- [x] The PR is proposed to the proper branch
- [ ] There is a reference to the original bug report and related work
- [ ] There is accuracy test, performance test and test data in opencv_extra repository, if applicable
Patch to opencv_extra has the same branch name.
- [x] The feature is well documented and sample code can be built with the project CMake
1 天前
489df18a
Use border value in ipp version of warp affine #26313
### Pull Request Readiness Checklist
See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request
- [x] I agree to contribute to the project under Apache 2 License.
- [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV
- [x] The PR is proposed to the proper branch
- [ ] There is a reference to the original bug report and related work
- [ ] There is accuracy test, performance test and test data in opencv_extra repository, if applicable
Patch to opencv_extra has the same branch name.
- [ ] The feature is well documented and sample code can be built with the project CMake
1 天前
更多推荐
已为社区贡献2条内容
所有评论(0)