头文件部分
#include<opencv.hpp>
#include<iostream>
#include<vector>
#include"quickopencv.h" // 自定义类文件
using namespace cv;
using namespace std;
自定义头文件部分,本文件主要是自定义学习过程中的函数
Mat Quick::RotationMatrix2D(const Mat& img, double angle, Point2f center) //图像旋转函数,实际是图像仿射变换的简化版
{
if (center == Point2f(-1, -1))
{
center.x = img.cols / 2.0;
center.y = img.rows / 2.0;
}
Size dst_size(img.rows,img.cols);
Mat rotationMat = getRotationMatrix2D(center, angle, 1);
Mat dst;
RotatedRect bbox = RotatedRect(center, img.size(), angle);
Rect box = bbox.boundingRect();
rotationMat.at<double>(0, 2) += box.width / 2.0 - center.x; //保证原图像和输出图像中心点一致
rotationMat.at<double>(1, 2) += box.height / 2.0 - center.y;
warpAffine(img, dst, rotationMat, dst_size);
return dst;
}
void Quick::mat_creation_demo(Mat& image){ //深拷贝和浅拷贝的问题
Mat m1, m2;
m1 = image.clone();
image.copyTo(m2);
Mat m3 = Mat::zeros(Size(8,8),CV_8UC1);
m3 = Scalar(127,127,127);//操作符重载,三通道全部赋值
std::cout << m3 << std::endl;
Mat m4 = m3;//指针赋值
//Mat m4 = m3.clone();//对象赋值
}
void drawHist(Mat& hist, int type, string name)
{
int hist_w = 512;
int hist_h = 400;
int width = 2;
Mat histImage = Mat::zeros(hist_h, hist_w, CV_8UC3);
Mat hist_norm = Mat::zeros(hist_h, hist_w, CV_8UC3);
normalize(hist, hist_norm, 1, 0, type, -1, Mat());
hist = hist_norm;
for (int i = 0; i < hist.rows; i++)
{
rectangle(histImage,
Point(width * (i - 1), hist_h - 1),//在 OpenCV 中,图像的坐标原点通常位于左上角
Point(width * i - 1, hist_h - cvRound(hist.at<float>(i - 1) / 15)),
Scalar(255, 255, 255), -1);
}
imshow(name, histImage);
}
源文件程序部分
Mat img = imread("C:\\Users\\10751\\Desktop\\222.png");
Mat noobcv = imread("C:\\Users\\10751\\Desktop\\111.jpg");
Mat mask,img_copy2, img_copy;
resize(noobcv, mask, Size(200, 200));
Mat img2 = img;
img.copyTo(img_copy2);
copyTo(img, img_copy, img);
Mat ROI1 = img(Rect(206, 206, 200, 200));
Mat ROI2 = img(Range(300, 500), Range(300, 500));
Mat img = imread("C:\\Users\\10751\\Desktop\\222.png");
Point2f src_point[4];
Point2f dst_point[4];
src_point[0] = Point2f(94.0, 374.0);
src_point[1] = Point2f(507.0, 380.0);
src_point[2] = Point2f(1.0, 623.0);
src_point[3] = Point2f(627.0, 627.0);
dst_point[0] = Point2f(0,0);
dst_point[1] = Point2f(627, 0);
dst_point[2] = Point2f(0, 627);
dst_point[3] = Point2f(627,627);
Mat rotation, img_wrap;
rotation = getPerspectiveTransform(src_point, dst_point);
warpPerspective(img, img_wrap, rotation, img.size());
imshow("img", img);
imshow("dst", img_wrap);
Mat src = imread("C:\\Users\\10751\\Desktop\\111.jpg");
Quick qd;
Mat dst= qd.RotationMatrix2D(src, 90);
double angle=30;
Size dst_size(src.rows, src.cols);
Point2f src_point[3];
Point2f dst_point[3];
src_point[0] = Point2f(0, 0);
src_point[1] = Point2f(0,float(src.cols-1));
src_point[2] = Point2f(float(src.rows-1), float(src.cols - 1));
dst_point[0] = Point2f(float(src.rows)*0.11, float(src.cols)*0.20);
dst_point[1] = Point2f(float(src.rows) * 0.15, float(src.cols) * 0.70);
dst_point[2] = Point2f(float(src.rows) * 0.81, float(src.cols) * 0.85);
Mat rotation0, rotation1, src_warp0, src_warp1;
rotation1 = getAffineTransform(src_point, dst_point);
warpAffine(src, src_warp1, rotation1, Size(src.rows, src.cols));
Mat matArray[] = { Mat(1,2,CV_32FC1,Scalar(1)),Mat(1,2,CV_32FC1,Scalar(2)) };
Mat vout0, hout0;
vconcat(matArray, 2, vout0);
hconcat(matArray, 2, hout0);
Mat A = (Mat_<float>(2, 2) << 1, 7, 2, 8);
Mat B= (Mat_<float>(2, 2) << 4, 10, 5, 11);
Mat vout1, hout1;
vconcat(A,B,vout1);
hconcat(A, B, hout1);
Mat src = imread("C:\\Users\\10751\\Desktop\\111.jpg");
Mat img0 = Mat::zeros(200, 200, CV_8UC1);
Mat img1 = Mat::zeros(200, 200, CV_8UC1);
Rect rect0(50, 50, 100, 100);
img0(rect0) = Scalar(255);
Rect rect1(100, 100, 100, 100);
img1(rect1) = Scalar(255);
imshow("img0", img0);
imshow("img1", img1);
Mat myand ,myor , myxor, mynot,imgnot;
bitwise_and(img0, img1, myand);
bitwise_not(src, imgnot);
imshow("dst", imgnot);
直方图绘制
Mat img = imread("C:\\Users\\1022751\\Desktop\\111.jpg");
Mat gray, hist, gray2, hist2, gray3, hist3;
cvtColor(img, gray, 6);
resize(gray, gray2, Size(), 0.5, 0.5);//ize()中的参数为空,表示不指定调整后的图像大小,而是根据缩放比例进行自动计算。
gray3 = imread("C:\\Users\\2251\\Desktop\\lena.png", 0);
const int channels[1] = { 0 };
float inRanges[2] = { 0,255 };
const float* ranges[1] = { inRanges };
const int bins[1] = { 256 };
calcHist(&gray, 1, channels, Mat(), hist, 1, bins, ranges);
calcHist(&gray2, 1, channels, Mat(), hist2, 1, bins, ranges);
calcHist(&gray3, 1, channels, Mat(), hist3, 1, bins, ranges);
drawHist(hist, NORM_INF, "hist");
drawHist(hist2, NORM_INF, "hist");
drawHist(hist3, NORM_INF, "hist");
double hist_hist = compareHist(hist, hist, HISTCMP_CORREL);
cout << "hist_hist" << hist_hist<<endl;
double hist_hist2 = compareHist(hist, hist2, HISTCMP_CORREL);
cout << "hist_hist2" << hist_hist2 << endl;
double hist_hist3 = compareHist(hist, hist3, HISTCMP_CORREL);
cout << "hist_hist3" << hist_hist3 << endl;