Mat img =imread("../assets/itheima.jpg",IMREAD_COLOR);// 定义要截取的矩形框
Rect rect(100,50,200,200);// 这样即可完成截取
Mat roi =img(rect);
5. 直方图获取及绘制,注意,这里的原点是图像的左上角位置,并非左下角
//// Created by itheima on 6/12/19.//#include<iostream>#include<opencv2/opencv.hpp>usingnamespace std;usingnamespace cv;intmain(){// 读取图片
Mat img =imread("../assets/lena.jpg",IMREAD_COLOR);imshow("src",img);// 单通道处理数据
vector<Mat> bgr_channels;// 颜色分割split(img,bgr_channels);int histSize=256;float range[]={0,256};constfloat* histRange ={range};// 用于说明bin宽度是否相等// bool uniform=true;// 当计算多通道直方图的时候,是否进行累加// bool accumulate=false;
Mat b_hist,g_hist,r_hist;calcHist(&bgr_channels[0],1,0,Mat(),b_hist,1,&histSize,&histRange);calcHist(&bgr_channels[1],1,0,Mat(),g_hist,1,&histSize,&histRange);calcHist(&bgr_channels[2],1,0,Mat(),r_hist,1,&histSize,&histRange);int histsize =256;// 创建一个灰色图像int hist_w =512;int hist_h =400;int bin_w =cvRound(512.0/histSize);//将直方图绘制到512*400大小的图像上去,要进行一定的缩放,需要进行归一化//进行归一化处理 最小值 最大值 模式normalize(b_hist,b_hist,0,hist_h,NORM_MINMAX);
cout<<"归一化之后:"<<b_hist<<endl;
Mat histImage(hist_h,hist_w,CV_8UC3,Scalar(0,0,0));// 绘制图像 这里只绘制了一个通道的直方图// 绘制直方图值时候,为更好的观看,做一点偏移// 注意,这里的原点是图像的左上角位置,并非左下角for(int i=0;i<histsize;i++){line(histImage,Point(bin_w*i,hist_h),Point(bin_w*i,hist_h-b_hist.at<float>(i-1)),Scalar(255,0,0),2,LINE_AA);}imshow("histgrom",histImage);waitKey(0);destroyAllWindows();}
6. 直方图均衡化
#include<iostream>#include<opencv2/opencv.hpp>usingnamespace std;usingnamespace cv;intmain(){
Mat img =imread("../assets/itheima.jpg", IMREAD_COLOR);//将图像转成灰度图像
Mat gray;cvtColor(img, gray, COLOR_BGR2GRAY);imshow("gray",gray);// 计算直方图
Mat hist;int histSize =256;float range[]={0,255.0};constfloat*histRange[]={range};calcHist(&gray,1,0,Mat(), hist,1,&histSize, histRange);// 计算直方图的概率
Mat ratios = hist /(img.rows * img.cols);float sum1 =0;// 计算直方图的累加概率for(int i =0; i < ratios.rows; i++){
sum1 += ratios.at<float>(i);
ratios.at<float>(i)= sum1;}
cout<<ratios<<endl;
Mat dst(gray.rows, gray.cols, CV_8UC1);// 对原图像进行处理for(int row =0; row < img.rows;++row){for(int col =0; col < img.cols;++col){// 获取每个位置的灰度值int color = gray.at<uint8_t>(row, col);// 根据颜色值从累计概率中获取累计概率float ratio = ratios.at<float>(color);// 计算新的颜色值
color =cvRound(255* ratio);// 填充到原图中
dst.at<uchar>(row, col)= color;}}imshow("dst", dst);waitKey(0);destroyAllWindows();return0;}
7. 图像的卷积和滤波,注意Mat初始化方法
// 定义卷积核
Mat kernel =(Mat_<char>(3,3)<<-1,-1,-1,-1,9,-1,-1,-1,-1);// 进行图像的卷积操作
Mat dst;filter2D(img,dst,-1,kernel);//
Mat gray;cvtColor(src_img,gray,COLOR_BGR2GRAY);GaussianBlur(gray,gray,Size(3,3),0);Laplacian(img,dst,-1,5);//-1表示输出类型和输入类型一致,5代表卷积核大小// sobel和sharr算子
Mat xgray,ygray;// Sobel(gray,xgray,CV_16S,1,0);// Sobel(gray,ygray,CV_16S,0,1);Scharr(gray,xgray,CV_16S,1,0);Scharr(gray,ygray,CV_16S,0,1);//显示出问题时,若是由数据类型引起,可使用以下函数//功能是 缩放,计算绝对值,然后将结果转换为8位。convertScaleAbs(xgray,xgray);convertScaleAbs(ygray,ygray);add(xgray,ygray,result);