【学习笔记】OpenCV + C++(三)

回眸只為那壹抹淺笑 提交于 2020-02-08 05:22:16

调整图像亮度和对比度
        理论
            像素变换---点操作
            邻域操作---区域
                  做图像的卷积,图像特征提取,图像梯度计算,图像更深入模式匹配,模糊,平滑等
            调整图像亮度和对比度属于像素变换---点操作
            g(i,j) = a*f(i,j)+P  其中a>0,P是增益变量
          重要API
             Mat new_image = Mat::zeros(image.size(),image.type());创建一张跟原图像大小和类型一致的空白图像、像素值初始化为0

             saturate_cast<uchar>(value)确保值大小范围为0~255之间
             Mat.at<Vec3b>(y,x)[index]=value给每个像素点每个通道赋值

    #include<opencv2/opencv.hpp>
             #include<iostream>
             using namespace cv;
             int main(int argc,char** argv){
                 Mat src,dst;
                 src = imread();
                 if(!src.data){
                     printf("could not load image...\n");
                     return -1;
                 }
                 char input_win[] = "input image";
                 namedWindow(input_win,CV_WINDOW_AUTOSIZE);
                 imshow(input_win,src);

                 int height = src.rows;
                 int width = src.cols;
                 dst = Mat::zeros(src.size(),src.type());
                 float alpha = 1.2;
                 float beta = 30;
                 for(int row = 0; row < height; row++){
                     for(int col = 0; col < width; col++){
                         if(src.channels() == 3){
                             float b = src.at<Vec3b>(row,col)[0];
                             float g = src.at<Vec3b>(row,col)[1];
                             float r = src.at<Vec3b>(row,col)[2];
                             
                             dst.at<Vec3b>(row,col)[0] = saturate_cast<uchar>(b*alpha+beta);
                             dst.at<Vec3b>(row,col)[1] = saturate_cast<uchar>(g*alpha+beta);
                             dst.at<Vec3b>(row,col)[2] = saturate_cast<uchar>(r*alpha+beta);

                         }else if(src.channels()==1){
                             float v = src.at<uchar>(row,col);
                             dst.at<Vec3b>(row,col) = saturate_cast<uchar>(v*alpha+beta);
                         }
                     }
                 }
                 char output_title[] = "contrast and brightness change demo";
                 namedWindow(output_title,CV_WINDOW_AUTOSIZE);
                 imshow(output_title,dst);
                 waitKey(0);
                 return 0;
             }

 绘制形状与文字

       使用cv::Point与cv::Scalar

           Point表示2D平面上一个点x,y

           Point p;

           p.x = 10;

           p.y = 8;

           or

           p = Point(10,8);

           Scalar表示四个元素的向量

           Scalar(a,b,c);//a = blue,b = green,c = red表示RGB三个通道

       绘制线、矩形、圆、椭圆等基本几何形状

           画线cv::line(LINE_4\LINE_8\LINE_AA)注意:LINE_AA反锯齿

           画椭圆cv::ellipse

           画矩形cv::rectangle

           画圆cv::circle

           画填充cv::fillPoly

       随机生成与绘制文本

#include<opencv2/opencv.hpp>
            #include<iostream>
            using namespace std;
            using namespace cv;
            Mat bgImage;
            const char* drawdemo_win = "draw shapes and text demo";
            //画线
            void MyLines();
            //画矩形
            void MyRectangle();
            //画椭圆
            void MyEllipse();
            //画圆
            void MyCircle();
            //画多边形
            void MyPolygen();
            //画随机线
            void  RandomLineDemo();
            int main(int argc,char** argv){
                bgImage = imread("");
                if(!bgImage.data){
                    cout<<"could not load image..."<<endl;
                    return -1;
                }
                MyLines();
                MyRectangle();
                MyEllipse();
                MyCircle();
                MyPolygen();
                  //输出文字
                putText(bgImage,"Hello OpenCV",Point(300,300),CV_FONT_HERSHEY_COMPLEX,1.0,Scalar(120,12,200),1,8);//注意:CV_FONT_HERSHEY_COMPLEX是字体,1.0是字体大小
                //随机图形
                RandomLineDemo();
                namedWindow(drawdemo_win,CV_WINDOW_AUTOSIZE);
                imshow(drawdemo_win,bgImage);
                waitKey(0);
                return 0;
            }  
            void MyLines(){
                Point p1 = Point(20,30);
                Point p2;
                p2.x = 300;
                p2.y = 300;
                Scalar color = Scalar(0,0,255);
                line(bgImage,p1,p2,color,1,LINE_8);
            }
            void MyRectangle(){
                Rect rect = Rect(200,100,300,300);
                Scalar color = Scalar(255,0,0);
                rectangle(bgImage,rect,color,2,LINE_8);
                //rectangle(cv::Mat &img,cv::Rectt rec,const cv::Scalar &color,int thickness = 1,int lineType = 8,int shift = 0)
                //thickness 线宽
            }
            void MyEllipse(){
                ellipse (bgImage,Point(bgImage.cols/2,bgImage.rows/2),Size(bgImage.cols/4,bgImage.rows/8),90,0,360,Scalar(0,255,0),2,LINE_8);//注意:90是旋转角度
            }
            void MyCircle(){
                Scalar color = Scalar(0,255,255);
                Point center = Point(bgImage.cols/2,bgImage.rows/2);
                circle(bgImage,center,150,color,2,8);
            }
            void MyPolygen(){
                Scalar color = Scalar(255,0,255);
                Point pts[1][5];
                pts[0][0] = Point(100,100);
                pts[0][1] = Point(100,200);
                pts[0][2] = Point(200,200);
                pts[0][3] = Point(200,100);
                pts[0][4] = Point(100,100);
                const Point* ppts[] = {pts[0]};
                int[] npt = {5};
                fillPoly(bgImage,ppts,npt,1,color,LINE_8);
            }
            void  RandomLineDemo(){
                RNG rng(12345);
                Mat bg = Mat::zeros(bgImage.size(),bgImage.type());
                namedWindow("Line Demo",CV_WINDOW_AUTOSIZE);
                Point pt1;
                Point pt2;
                for(int i = 0; i < 10000; i++){
                    pt1.x = rng.uniform(0,bgImage.cols);
                    pt2.x = rng.uniform(0,bgImage.cols);
                    pt1.y = rng.uniform(0,bgImage.rows);
                    pt2.y = rng.uniform(0,bgImage.rows);

                    Scalar color = Scalar(200,rng.uniform(0,255),rng.uniform(0,255));
                    
                    if(WaitKey(50)>0){
                        break;
                    }
                    line(bgImage,pt1,pt2,color,1,8);
                    imshow("Line Demo",bg);
                }
                

            }

     随机数生成CV::RNG

                生成高斯随机数gaussian(double sigma)

                生成正态分布随机数uniform(int a,int b)

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!