当前位置:   article > 正文

OpenCv_could not locate the image

could not locate the image

目录

一:阈值

阈值类型:

API

 代码

效果:​

 二:自定义线性滤波

1.定义

2.常见算子

Robert算子

 Sobel算子

拉普拉斯算子

3.自定义卷积模糊 

代码

效果:​

 三:处理边缘

API:

 四:Sobel算子

水平梯度

垂直梯度

 Scharr函数

API:

代码:

效果

五:Laplance算子

 过程

API

代码

效果:


一:阈值

阈值:把图像分割的标尺

阈值类型:

 

 

 

API

 

 代码

  1. #include<opencv2/opencv.hpp>
  2. #include<iostream>
  3. #include<math.h>
  4. using namespace cv;
  5. Mat src, dst, dst1;
  6. int threshold_value = 127;
  7. int threshold_max = 255;
  8. int type = 2;
  9. int type_max =4;
  10. char result[] = "threshold image";
  11. //或者定义一个指针形式:const char* result="threshold image";
  12. void threshold_Demo(int, void*);
  13. int main(int argc, int argv)
  14. {
  15. src = imread("D:/opencvtu/1.jpg");
  16. if (!src.data)
  17. {
  18. printf("could not load image...\n");
  19. return -1;
  20. }
  21. else
  22. {
  23. printf("ok....");
  24. }
  25. namedWindow("input", CV_WINDOW_AUTOSIZE);
  26. imshow("input", src);
  27. namedWindow(result, CV_WINDOW_AUTOSIZE);
  28. createTrackbar("threshold value:", result, &threshold_value, threshold_max, threshold_Demo);
  29. createTrackbar("threshold type:", result, &type, type_max, threshold_Demo);
  30. threshold_Demo(0, 0);
  31. waitKey(0);
  32. return 0;
  33. }
  34. //阈值化实现函数
  35. void threshold_Demo(int, void*)
  36. {
  37. cvtColor(src, dst, CV_BGR2GRAY);
  38. imshow("GRAY", dst);
  39. threshold(dst, dst1, threshold_value, threshold_max, type);
  40. imshow(result, dst1);
  41. }

效果:

 二:自定义线性滤波

1.定义

卷积是图像处理中一个操作,是kernel在图像的每个像素上的操作。

Kernel本质上一个固定大小的矩阵数组,其中心点称为锚点(anchor point);

把kernel放到像素数组之上,求锚点周围覆盖的像素乘积之和(包括锚点),用来替换锚点覆盖下像素点值称为卷积处理。数学表达如下:

 

2.常见算子

Robert算子

 

 Sobel算子

拉普拉斯算子

3.自定义卷积模糊 

filter2D方法filter2D(

Mat src, //输入图像

Mat dst, // 模糊图像

int depth, // 图像深度32/8

Mat kernel, // 卷积核/模板

Point anchor, // 锚点位置

double delta // 计算出来的像素+delta

)

其中 kernel是可以自定义的卷积核

代码

  1. #include<opencv2/opencv.hpp>
  2. #include<iostream>
  3. #include<math.h>
  4. using namespace cv;
  5. Mat src, dst;
  6. int main(int argc, int argv)
  7. {
  8. src = imread("D:/opencvtu/1.jpg");
  9. if (!src.data)
  10. {printf("could not load image...\n");
  11. return -1;}
  12. else
  13. {printf("ok....");}
  14. namedWindow("input", CV_WINDOW_AUTOSIZE);
  15. imshow("input", src);
  16. //namedWindow("output_x", CV_WINDOW_AUTOSIZE);
  17. //namedWindow("output_y", CV_WINDOW_AUTOSIZE);
  18. //卷积:kernel在图像的每个像素上的操作
  19. //kernel是一个固定大小的矩阵数组,中心点=锚点;求锚点周围覆盖的像素乘积之和
  20. //替换锚点覆盖下的像素点值;
  21. /*x方向*/
  22. //Mat kernel = (Mat_<int>(3,3) << -1,0,1,-2,0,2,-1,0,1);//算子
  23. //filter2D(src, dst, -1, kernel,Point(-1, -1), 0.0);
  24. //imshow("output_x", dst);
  25. y
  26. //Mat kernel2 = (Mat_<int>(2, 2) << 0,1,-1,0);//算子
  27. //filter2D(src, dst1, -1, kernel2, Point(-1, -1), 0.0);
  28. //imshow("output_y", dst1);
  29. int c = 0;
  30. int index = 0;
  31. int ksize = 0;
  32. while (true)
  33. {
  34. c = waitKey(500);
  35. if ((char)c == 27) // ESC
  36. {
  37. break;
  38. }
  39. ksize = 5 + (index % 8) * 2;
  40. Mat kernel = Mat::ones(Size(ksize, ksize), CV_32F) / (float)(ksize * ksize);
  41. filter2D(src, dst, -1, kernel, Point(-1, -1));
  42. index++;
  43. imshow("output", dst);
  44. }
  45. return 0;
  46. }

效果:

 三:处理边缘

图像卷积的时候边界像素,不能被卷积操作,原因在于边界像素没有完全跟kernel重叠,所以当3x3滤波时候有1个像素的边缘没有被处理,5x5滤波的时候有2个像素的边缘没有被处理。

API:

- BORDER_CONSTANT填充边缘用指定像素值

 - BORDER_REPLICATE 填充边缘像素用已知的边缘像素值

 - BORDER_WRAP用另外一边的像素来补偿填充

  1. #include<opencv2/opencv.hpp>
  2. #include<iostream>
  3. #include<math.h>
  4. using namespace cv;
  5. Mat src, dst;
  6. int main(int argc, int argv)
  7. {
  8. src = imread("D:/opencvtu/1.jpg");
  9. if (!src.data)
  10. {printf("could not load image...\n");
  11. return -1;}
  12. else
  13. {printf("ok....");}
  14. namedWindow("input", CV_WINDOW_AUTOSIZE);
  15. imshow("input", src);
  16. namedWindow("output", CV_WINDOW_AUTOSIZE);
  17. int top = 0.05*src.rows;
  18. int bottom =(int) 0.05*src.rows;
  19. int left =(int) 0.05*src.cols;
  20. int right = (int)0.05*src.cols;
  21. RNG rng(12345);
  22. int bordertype = BORDER_DEFAULT;
  23. int c = 0;
  24. while (true){
  25. c = waitKey(500);
  26. if ((char)c == 27){
  27. break;
  28. }
  29. if ((char)c == 'r'){
  30. bordertype = BORDER_REPLICATE;
  31. }
  32. else if ((char)c == 'v'){
  33. bordertype =BORDER_WRAP;
  34. }
  35. else if ((char)c == 'c'){
  36. bordertype = BORDER_CONSTANT;
  37. }
  38. Scalar color = Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255));
  39. copyMakeBorder(src, dst, top, bottom, left, right, bordertype, color);
  40. imshow("output", dst);
  41. }
  42. //GaussianBlur(src, dst, Size(5, 5), 0, 0, BORDER_CONSTANT);
  43. imshow("output", dst);
  44. waitKey(0);
  45. return 0;
  46. }

输入c的效果:随机变换颜色

 

输入r的效果:

 输入v的效果

 四:Sobel算子

 

 边缘是什么 是像素值发生跃迁的地方,是图像的显著特征之一,在图像特征提取、对象检测、模式识别等方面都有重要的作用。

又被称为一阶微分算子,求导算子,在水平和垂直两个方向上求导,得到图像X方法与Y方向梯度图像

水平梯度

垂直梯度

 

 最终图像梯度

 Scharr函数

API:

cv::Sobel (

InputArray Src // 输入图像

OutputArray dst// 输出图像,大小与输入图像一致

int depth // 输出图像深度.

Int dx.  // X方向,几阶导数

int dy // Y方向,几阶导数.

int ksize, SOBEL算子kernel大小,必须是1357

double scale  = 1

double delta = 0

int borderType = BORDER_DEFAULT

)

 convertScaleAbs(A, B)// 计算图像A的像素绝对值,输出到图像B

代码:

  1. #include<opencv2/opencv.hpp>
  2. #include<iostream>
  3. #include<math.h>
  4. using namespace cv;
  5. Mat src, dst;
  6. int main(int argc, int argv)
  7. {
  8. src = imread("D:/opencvtu/2.jpg");
  9. if (!src.data)
  10. {printf("could not load image...\n");
  11. return -1;}
  12. else
  13. {printf("ok....");}
  14. namedWindow("input", CV_WINDOW_AUTOSIZE);
  15. imshow("input", src);
  16. //namedWindow("output", CV_WINDOW_AUTOSIZE);
  17. /*提取边缘,求一阶导数*/
  18. GaussianBlur(src, dst, Size(3, 3), 0, 0);
  19. Mat gray_src;
  20. cvtColor(src, gray_src, CV_BGR2GRAY);
  21. //imshow("output", gray_src);
  22. Mat x, y;
  23. //Scharr(gray_src, x, CV_16S, 1, 0);
  24. //Scharr(gray_src, y, CV_16S, 0, 1);
  25. Sobel(gray_src, x, CV_16S, 1, 0, 3);//x梯度
  26. Sobel(gray_src, y,CV_16S ,0, 1, 3);
  27. convertScaleAbs(x, x);
  28. convertScaleAbs(y, y);
  29. Mat xy = Mat(x.size(), x.type());
  30. int width = x.cols;
  31. int height = y.rows;
  32. for (int row = 0; row < height; row++) {
  33. for (int col = 0; col < width; col++) {
  34. int xg = x.at<uchar>(row, col);
  35. int yg = y.at<uchar>(row, col);
  36. int xy2 = xg + yg;
  37. xy.at<uchar>(row, col) = saturate_cast<uchar>(xy2);
  38. }
  39. }
  40. //addWeighted(x, 0.5, y, 0.5, 0,xy);
  41. imshow("xy", xy);
  42. imshow("x", x);
  43. imshow("y", y);
  44. waitKey(0);
  45. return 0;
  46. }

效果

(1)通过addweighted达到的效果

 (2)

 通过对比,第二种方法更好;

五:Laplance算子

计算二阶导数

 

 过程

高斯模糊 去噪声GaussianBlur()

转换为灰度图像cvtColor()

拉普拉斯 二阶导数计算Laplacian()

取绝对值convertScaleAbs()

显示结果

API

Laplacian(

InputArray src,

OutputArray dst,

int depth, //深度CV_16S

int kisze, // 3

double scale = 1,

double delta =0.0,

int borderType = 4

)

代码

  1. #include<opencv2/opencv.hpp>
  2. #include<iostream>
  3. #include<math.h>
  4. using namespace cv;
  5. Mat src, dst, dst1,dst2;
  6. int main(int argc, int argv)
  7. {
  8. src = imread("D:/opencvtu/1.jpg");
  9. if (!src.data)
  10. {printf("could not load image...\n");
  11. return -1;}
  12. else
  13. {printf("ok....");}
  14. namedWindow("input", CV_WINDOW_AUTOSIZE);
  15. imshow("input", src);
  16. /*高斯模糊,转换为灰度图像,拉普拉斯算法,取绝对值,显示*/
  17. GaussianBlur(src, dst, Size(3, 3), 0, 0);
  18. cvtColor(dst, dst1, CV_BGR2GRAY);
  19. Laplacian(dst1, dst2, CV_16S, 3);
  20. convertScaleAbs(dst2, dst2);
  21. imshow("output", dst2);
  22. waitKey(0);
  23. return 0;
  24. }

效果:

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/959116
推荐阅读
相关标签
  

闽ICP备14008679号