文章目录
-
- ROI区域
- 灰度图转换
- 图像增益设计
- 图像尺寸调整
- 图像混合,设定透明度
- 图像合并,并列显示
基本图像操作
方法 |
描述 |
示例 |
rect |
设定矩形框 |
Rect rect(100, 100, 200, 200); Mat roi = src(rect);//提取src数据中矩形框标定的数据,此时roi指向了src图片对应的矩形数据,是指针! |
cvtColor |
图片类型转换 |
cvtColor(src, gray, COLOR_BGR2GRAY); //BGR转灰度图 |
threshold |
二进制阈值化 |
threshold(gray, bin, 100, 255, THRESH_BINARY); //大于100全部设为255:白色; 小于100的设为0:黑色; threshold(gray,ibin, 100, 255, THRESH_BINARY_INV);//反二进制阈值化 |
resize |
图片尺寸调整 |
resize(src, des1024, Size(1024, 1024), 0, 0, INTER_LINEAR); |
pyrDown |
高斯金字塔,下采样 |
pyrDown(src, gsrc);高斯金字塔(Gaussian pyramid):用来向下采样–下采样:图像成倍缩小 |
pyrUp |
拉普拉斯金字塔,上采样 |
pyrUp(src, lsrc);//拉普拉斯金字塔(Laplacian pyramid):用来从金字塔低层图像重建上层未采样图像 — 上采样:图像成倍增大 |
addWeighted |
图像混合 |
addWeighted(img1, a, img2, 1 - a, 80, dst); // addWeighted:加权; //a:img1的权重,取值范围[0,1]; dst:生成图像 |
rotate |
图像旋转 |
cv::rotate(img, rot, ROTATE_90_CLOCKWISE); // cv::rotate cv命名空间下的rotate函数,因此函数太容易被其他库中的函数名冲突。 |
flip |
图像镜像 |
cv::flip(img, fl, 1); // type:0(围绕x轴镜像),1(围绕y轴镜像),-1(围绕x和y轴做镜像) |
ROI区域
#include
#include
#include
#include
using namespace cv;
using namespace std;
int main(int argc, char *argv[])
{
Mat src = imread("1.png");
Rect rect(100, 100, 200, 200);
Mat roi = src(rect);
namedWindow("src");
namedWindow("roi");
moveWindow("roi", 512, 0);
imshow("src", src);
imshow("roi", roi);
waitKey(0);
return 0;
}
Mat binNowX = matFenge(rectBinX).clone();
binNowX.copyTo(BinaryTemplete(rectBinX));
灰度图转换
void RGBToGray(Mat &src, Mat &des)
{
des.create(src.rows,src.cols,CV_8UC1);
for (int r = 0; r < src.rows; r++)
{
for (int c = 0; c < src.cols; c++)
{
Vec3b &m = src.at<Vec3b>(r, c);
int gray = (m[2] * 30 + m[1] * 59 + m[0] * 11 + 50) / 100;
des.at<uchar>(r, c) = gray;
}
}
}
int main(int argc, char *argv[])
{
Mat src = imread("1.png");
src.create(3000, 4000, CV_8UC3);
Mat gray;
PrintMs("");
cvtColor(src, gray, COLOR_BGR2GRAY);
PrintMs("cvtColor1");
cvtColor(src, gray, COLOR_BGR2GRAY);
PrintMs("cvtColor2");
Mat mygray;
RGBToGray(src, mygray);
PrintMs("RGBToGray");
namedWindow("src");
namedWindow("gray");
namedWindow("mygray");
imshow("src", src);
imshow("gray", gray);
imshow("mygray", mygray);
waitKey(0);
return 0;
}
图像增益设计
void ChangeGain(Mat &src, Mat &des, float a, int b)
{
des.create(src.rows, src.cols, src.type());
for (int r = 0; r < src.rows; r++)
{
for (int c = 0; c < src.cols; c++)
{
for (int i = 0; i < 3; i++)
{
des.at<Vec3b>(r, c)[i] = saturate_cast<uchar>(a * src.at<Vec3b>(r, c)[i] + b);
}
}
}
}
int main(int argc, char *argv[])
{
Mat src = imread("2.jpg");
Mat des;
PrintMs("");
ChangeGain(src,des, 2.0,50);
PrintMs("ChangeGain");
Mat des2;
src.convertTo(des2, -1, 2.0, 50);
PrintMs("convertTo");
namedWindow("src");
namedWindow("des");
namedWindow("des2");
imshow("src", src);
imshow("des", des);
imshow("des2", des2);
waitKey(0);
return 0;
}
图像尺寸调整
void xresize(Mat &src, Mat &des, Size size)
{
des.create(size, src.type());
int sx, sy = 0;
float fx = (float)src.cols / des.cols;
float fy = (float)src.rows / des.rows;
for (int x = 0; x < des.cols; x++)
{
sx = fx * x + 0.5;
for (int y = 0; y <des.rows; y++)
{
sy = fy * y + 0.5;
des.at<Vec3b>(y, x) = src.at<Vec3b>(sy, sx);
}
}
}
int main(int argc, char *argv[])
{
Mat src = imread("1.png");
Mat img256,img1024,des256,des1024;
resize(src, des256, Size(256, 256), 0, 0, INTER_NEAREST);
PrintMs();
xresize(src, img1024, Size(1024, 1024));
PrintMs("img1024");
resize(src, des1024, Size(1024, 1024), 0, 0, INTER_LINEAR);
PrintMs("des1024");
namedWindow("src");
namedWindow("des1024");
namedWindow("img1024");
imshow("src", src);
imshow("img1024", img1024);
imshow("des1024", des1024);
moveWindow("des1024", 512, 0);
waitKey(0);
return 0;
}
图像混合,设定透明度
int main(int argc, char *argv[])
{
Mat img1 = imread("1.png");
Mat img2 = imread("2.png");
resize(img2, img2, img1.size());
Mat dst;
float a = 0.8;
addWeighted(img1, a, img2, 1 - a, 80, dst);
namedWindow("blending");
imshow("blending", dst);
waitKey(0);
return 0;
}
图像合并,并列显示
#include
int main(int argc, char *argv[])
{
Mat img1 = imread("1.png");
Mat img2 = imread("2.png");
int height = img1.rows;
int width1 = img1.cols;
int width2 = img2.cols;
if (img1.rows > img2.rows)
{
height = img2.rows;
width1 = img1.cols * ((float)img2.rows / (float)img1.rows);
resize(img1, img1, Size(width1, height));
}
else if(img1.rows < img2.rows)
{
width2 = img2.cols * ((float)img1.rows / (float)img2.rows);
resize(img2, img2, Size(width2, height));
}
Mat des;
des.create(height, width1 + width2, img1.type());
Mat r1 = des(Rect(0, 0, width1, height));
img1.copyTo(r1);
Mat r2 = des(Rect(width1, 0, width2, height));
img2.copyTo(r2);
namedWindow("des");
imshow("des", des);
waitKey(0);
destroyAllWindows();
return 0;
}