图像处理基础

void test() {

    Mat src = imread("D:/opencvsp/pic.png");

    if (src.empty())

    {

         printf("could not load image\n");

         return -1;

    }

    namedWindow("test opencv setup", WINDOW_AUTOSIZE);

    imshow("test opencv setup", src);

    namedWindow("output windows", WINDOW_AUTOSIZE);

    Mat output_image;

    cvtColor(src, output_image, COLOR_BGR2HLS);///

    imshow("output windows", output_image);

 

    imwrite("D:/opencvsp/hsls.png", output_image);///

}

//提高对比度 矩阵的掩膜操作

 void test02(){

     Mat src, dst;

     src = imread("D:/opencvsp/pic.png");

     if (!src.data)

     {

          printf("could not load image\n");

          return -1;

     }

     namedWindow("intput image", WINDOW_AUTOSIZE);

     imshow("input image", src);

     

     int cols = (src.cols - 1) * src.channels();

     int offsetx = src.channels();

     int rows = src.rows;

     dst = Mat::zeros(src.size(),src.type());

     for (int row = 0; row < rows - 1; row++)

     {

          const uchar* previous = src.ptr<uchar>(row - 1);

          const uchar* current = src.ptr<uchar>(row);

          const uchar* next = src.ptr<uchar>(row + 1);

          uchar* output = dst.ptr<uchar>(row);

          for (int col = 0; col < cols; col++)

          {

              output[col] = saturate_cast<uchar>(5 * current[col] - (current[col - offsetx] + current[col + offsetx] + previous[col] + next[col]));

          }

     }

   

     

     Mat kernel = (Mat_<char>(3, 3) << 0,-1,0,-1,5,-1,0,-1,0 );

     filter2D(src,dst,src.depth(),kernel);/////提高对比度

     namedWindow("contrast image demo", WINDOW_AUTOSIZE);

     imshow("contrast image demo",dst);

 }

 

 void test03() {

     Mat src, dst;

     src = imread("D:/opencvsp/pic.png");

     namedWindow("input",WINDOW_AUTOSIZE);

     namedWindow("out",WINDOW_AUTOSIZE);

     cvtColor(src,dst,COLOR_BGR2GRAY);

     printf("input image channels: %d\n",src.channels());

     printf("out image channels: %d \n",dst.channels());

 

     dst = Mat(src.size(),src.type());

     dst = Scalar(127,0,255);

     const uchar* firstRow = dst.ptr<uchar>(0);

 

     Mat M(3, 3, CV_8UC3, Scalar(0, 0, 255));

 

     Mat m2 = Mat(2, 2, CV_8UC1);//

     Mat m22 = Mat::eye(2, 2, CV_8UC1);

     imshow("output",dst);

 } 

 

 void test04() {

      Mat src, gray_src;

     src = imread("D:/opencvsp/pic.png");

     if (src.empty())

     {

          std::cout << "could not load image" << std::endl;

          return -1;

     }

     namedWindow("input", WINDOW_AUTOSIZE);

     imshow("input", src);

 

     cvtColor(src, gray_src, COLOR_BGR2GRAY);

     namedWindow("output", WINDOW_AUTOSIZE);

     imshow("output", gray_src);

     int height = gray_src.rows;

     int width = gray_src.cols;

     //单通道

     for (int row = 0; row < height; row++)

     {

          for (int col = 0; col < width; col++)

          {

              int gray = gray_src.at<uchar>(row, col);

              gray_src.at<uchar>(row, col) = 255 - gray;

          }

     }

     namedWindow("gray_invert", WINDOW_AUTOSIZE);

     imshow("gray_invert", gray_src);

 

     Mat dst,dst2;

     src = imread("D:/opencvsp/pic.png");

     dst.create(src.size(), src.type());

     cvtColor(src, gray_src, COLOR_BGR2GRAY);

     namedWindow("output", WINDOW_AUTOSIZE);

     imshow("output", gray_src);

     int heightt = gray_src.rows;

     int widthh = gray_src.cols;

     int nc = src.channels();

     for (int row = 0; row < heightt; row++)

     {

          for (int col = 0; col < widthh; col++)

          {

              if (nc == 1)

              {

                  int gray = gray_src.at<uchar>(row, col);

                  gray_src.at<uchar>(row, col) = 255 - gray;

              }

              else

              {

                  int b = src.at<Vec3b>(row, col)[0];

                  int g = src.at<Vec3b>(row, col)[1];

                  int 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;

                  //灰度图

                  gray_src.at<uchar>(row, col) = max(r, max(b,g));

                  gray_src.at<uchar>(row, col) = min(r, min(b, g));

              }

              

          }

     }

     namedWindow("dst", WINDOW_AUTOSIZE);

     imshow("dst", dst);

     //取反像素

     bitwise_not(src, dst2);

     namedWindow("dstt", WINDOW_AUTOSIZE);

     imshow("dstt", dst2);

 }

 

//图像混合

void(){

    Mat src1,src2, dst;

     src1 = imread("D:/opencvsp/pic.png");

     src1 = imread("D:/opencvsp/ad.png");

     if (!src1.empty())

     {

          std::cout << "could not load image" << std::endl;

          return -1;

     }

     if (!src2.empty())

     {

          std::cout << "could not load image" << std::endl;

          return -1;

     }

     double alpha = 0.5;

     if (src1.rows == src2.rows && src1.cols == src2.cols && src1.type == src2.type)

     {

          //

          addWeighted(src1,alpha,src2,(1-alpha),0.0,dst);

          //直接相加的函数,效果不佳

          //add(src1,src2,dst,Mat());

          multiply(src1,src2,dst,1.0);

          namedWindow("input", WINDOW_AUTOSIZE);

          imshow("input", src1);

          imshow("input", src2);

          namedWindow("blend", WINDOW_AUTOSIZE);

          imshow("blend", dst);

 

     }

     else

     {

          printf("could not blend iamges");

     }

}

 

 

//

 //调整图像的亮度和对比度

     Mat src, dst;

     src = imread("D:/opencvsp/pic.png");

     if (!src.data)

     {

          printf("could not load image");

          return -1;

     }

     //如果转灰度图片

     cvtColor(src,src,COLOR_BGR2GRAY);

     namedWindow("inputimage",WINDOW_AUTOSIZE);

     imshow("inputimage",src);

     int height = src.rows;

     int width = src.cols;

     dst = Mat::zeros(src.size(),src.type());

     float alpha = 1.2;

     float beta = 30;

     Mat m1;

     src.convertTo(m1,CV_32F);

     for (int row = 0; row < height; row++)

     {

          for (int col = 0; col < width; col++)

          {

              if (src.channels() == 3)

              {

                  float b = m1.at<Vec3f>(row, col)[0];

                  float g = m1.at<Vec3f>(row, col)[1];

                  float r = m1.at<Vec3f>(row, col)[2];

                  dst.at<Vec3b>(row, col)[0] = saturate_cast<uchar>(b * alpha + beta);

                  dst.at<Vec3b>(row, col)[1] = saturate_cast<uchar>(r * alpha + beta);

                  dst.at<Vec3b>(row, col)[2] = saturate_cast<uchar>(g * alpha + beta);

              }

              else

              {

                  float v = src.at<uchar>(row, col);

                  dst.at<uchar>(row, col) = saturate_cast<uchar>(v * alpha + beta);

              }

          }

     }

     namedWindow("output", WINDOW_AUTOSIZE);

 

 

Mat bgImage;

const char* drawdemo_win = "draw shapes text";

void MyLines();

void MyRectangle();

void MyEllipse();

void MyCircle();

void MyPolygon();

void RandomLineDome();

 

 int main(int argc, char** argv) {

     

    //绘制形状与文字

   

     bgImage = imread("D:/opencvsp/pic.png");

     if (!bgImage.data)

     {

          printf("could not load image ..\n");

          return -1;

     }

     MyEllipse();

     MyCircle();

 

     //打印文字

     putText(bgImage,"Hello opencv",Point(300,300),FONT_HERSHEY_COMPLEX,2.0,Scalar(12,255,200),1,8);

 

     namedWindow(drawdemo_win,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_AA); //反锯齿

 }

 

 void MyRectangle() {

     Rect rect = Rect(200, 100, 300, 300);

     Scalar color = Scalar();

     rectangle(bgImage,rect,color);

 }

 

 void MyEllipse() {

     //椭圆

     Scalar color = Scalar(0,255,0);

     //圆心 长轴  短轴 倾斜度

     ellipse(bgImage,Point(bgImage.cols /2,bgImage.rows / 2),Size(bgImage.cols / 4, bgImage.rows / 8),90,0,360,color,1,LINE_8);

 }

 

 void MyCircle() {

     Scalar color = Scalar(0, 255, 255);

     Point center = Point(bgImage.cols / 2, bgImage.rows / 2);

     circle(bgImage, center, 150,color, 1, 8);

 }

 

 

 void MyPolygon() {

     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 };

     Scalar color = Scalar{255, 12, 255};

     fillPoly(bgImage, ppts, npt, 1, color, 8);

 

 };

 

 void RandomLineDome() {

     RNG rng(12345);

     Point pt1;

     Point pt2;

     Mat bg = Mat::zeros(bgImage.size(), bgImage.type());

     namedWindow("randow",WINDOW_AUTOSIZE);

     for (int i = 0; i < 10; 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(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255));

          if (waitKey(50) > 0)

          {

              break;

          }

          line(bg, pt1, pt2, color, 1, 8);

          imshow("randow", bg);

     }

     

 }

 

 

/*

//图像模糊

Mat src, dst;

     src = imread("D:/opencvsp/pic.png");

     if (!src.data)

     {

          printf("could not load image..\n");

          return -1;

     }

     char intput_title[] = "intput image";

     char output_title[] = "blur_image";

     namedWindow(intput_title, WINDOW_AUTOSIZE);

     namedWindow(output_title, WINDOW_AUTOSIZE);

     imshow(intput_title,src);

     //size(29,3)

     blur(src,dst,Size(3,3),Point(-1,-1));

     imshow(output_title,dst);

 

     Mat gaudst;////size(11,11)

     GaussianBlur(src, gaudst, Size(5, 5), 11, 11);

     imshow("gaussian",gaudst);

     

 

 

 

 

 

     void medianblue() {

    Mat src, dst;

    src = imread("D:/opencvsp/pic.png");

    if (!src.data)

    {

         printf("could not load image..\n");

         return -1;

    }

    namedWindow("input img", WINDOW_AUTOSIZE);

    imshow("input img", src);

    //中间滤波

    medianBlur(src, dst, 3);

    namedWindow("medianBlur", WINDOW_AUTOSIZE);

    imshow("medianBlur", dst);

    //双边滤波

    bilateralFilter(src, dst, 15, 150, 3);

    namedWindow("bilateralFilter", WINDOW_AUTOSIZE);

    imshow("bilateralFilter", dst);

 

    //

    Mat resultimg;

    Mat kernel = (Mat_<int>(3, 3) << 0, -1, 0, -1, -5, -1, 0, -1, 0);

    filter2D(dst,resultimg,-1,kernel,Point(-1,-1),0);

    imshow("final result", resultimg);

}

 **/

 

 

 

Mat src, dst;

int element_size = 3;

int max_size = 21;

//膨胀与腐蚀

void CallBack_Demo(int,void*);

 int main(int argc, char** argv) {

     

     

     char output_win[] = "output image";

     src = imread("D:/opencvsp/pic.png");

     if (!src.data)

     {

          printf("could not load image..\n");

          return -1;

     }

     namedWindow("input img", WINDOW_AUTOSIZE);

     imshow("input img", src);

 

     namedWindow(output_win, WINDOW_AUTOSIZE);

     createTrackbar("Element Size", output_win, &element_size,max_size,CallBack_Demo);

 

     

   

 

     waitKey(0);

     return 0;

 }

 

 void CallBack_Demo(int, void*) {

     int s = element_size * 2 + 1;

     Mat structureElement = getStructuringElement(MORPH_RECT, Size(s, s), Point(-1, -1));

     //膨胀

     dilate(src, dst, structureElement, Point(-1, -1), 1);

     //腐蚀

     erode(src, dst, structureElement);

     imshow("output win", dst);

     return;

 }

 

 

 

 

// 形态学

     char output_win[] = "output image";

     src = imread("D:/opencvsp/pic.png");

     if (!src.data)

     {

          printf("could not load image..\n");

          return -1;

     }

     namedWindow("input img", WINDOW_AUTOSIZE);

     imshow("input img", src);

     char output_title[] = "morphologydemo";

     //先腐蚀后膨胀 可以去掉小的对象,背景是黑色

     Mat kernel = getStructuringElement(MORPH_RECT,Size(11,11),Point(-1,-1));

     morphologyEx(src,dst, MORPH_OPEN,kernel);

     imshow(output_title, dst);

 

     //闭操作  可以填充小洞  先膨胀后腐蚀

     morphologyEx(src, dst, MORPH_CLOSE, kernel);

     imshow("close", dst);

     //形态学梯度

     morphologyEx(src, dst, MORPH_GRADIENT, kernel);

     imshow("gradient",dst);

     //顶帽

     morphologyEx(src, dst, MORPH_TOPHAT, kernel);

     imshow("gradient", dst);

     //黑帽  闭操作图像与源图像的插值图像

     morphologyEx(src, dst, MORPH_BLACKHAT, kernel);

     imshow("gradient", dst);

 

 

     char output_win[] = "output image";

     src = imread("D:/opencvsp/pic.png");

     if (!src.data)

     {

          printf("could not load image..\n");

          return -1;

     }

     namedWindow("input img", WINDOW_AUTOSIZE);

     imshow("input img", src);

    //提取水平线

     Mat gray_src;

     cvtColor(src, gray_src, COLOR_BGR2GRAY);

     imshow("gray_iamge", gray_src);

 

     /***

     adaptiveThreshold(

     Mat src,Mat dest,

     doule maxValue,// 二值图像最大值

     int adaptiveMethod// 自适应方法,只能其中之一 ADAPTIVE_THRESH_MEAN_C, ADAPTIVE_THRESH_GAUSSIAN_C

     int thresholdType,//阈值类型

     int blockSzie,块大小

     double c 常量c可是是正数,0, 负数

     )

     */

     Mat binImg;

     adaptiveThreshold(gray_src, binImg, 255, ADAPTIVE_THRESH_MEAN_C, THRESH_BINARY, 15, -2);

     imshow("binary image", binImg);

     //横线  竖线 矩形

     Mat hline = getStructuringElement(MORPH_RECT, Size(src.cols / 16, 1), Point(-1, -1));

     Mat vline = getStructuringElement(MORPH_RECT, Size(1, src.rows / 16), Point(-1, -1));

     Mat kernel = getStructuringElement(MORPH_RECT, Size(5, 5), Point(-1, -1));

 

     Mat temp;

     erode(binImg, temp, hline);

     dilate(temp, dst, hline);

     imshow("final result", temp);

 

     morphologyEx(binImg, dst, MORPH_OPEN, vline);

 

     bitwise_not(dst, dst);

     blur(dst, dst, Size(3, 3), Point(-1, -1));

     imshow("final tow", dst);

posted @ 2021-01-04 22:10  sundaysc++  阅读(63)  评论(0编辑  收藏  举报