一.经典霍夫变换

霍夫变换是图像处理中的一种特征提取技术,该方法通过在一个参数空间中通过计算累计结果的局部最大值来得到一个符合该特定形状的集合,作为结果.

运用两个坐标空间之间的变换,将一个空间中具有相同形状的曲线或者是直线映射到另一个坐标空间中的一个点形成峰值,从而将统计任意性状化为统计峰值问题.

opencv中,霍夫线变换市一中寻找直线的方法,在使用霍夫线变换之前,要先对图像进行边缘检测的处理,霍夫变换的直接输入为阈值化之后的二值图像

opencv至此三种不同的霍夫线变换,标准霍夫变换(SHT),多尺度霍夫变换(MSHT),累计概率霍夫变换(PPHT).

PPHT是标准霍夫变换的一种改进方式,并不像标准霍夫变换一样将累加平面的所有的点累加,而只是累加其中的一小部分,该想法是如果峰值能够足够高,那么只需要一小部分时间来寻找它就够了.

霍夫变化的基本原理是:任意一条笛卡尔坐标下的直线,都可以化为极坐标形式,直线上的某一点其经过的所有直线的参数构成一条曲线,如果图片中有足够多的点,这些点的曲线会有一个共同的焦点,那么这个交点处的值就是这一条直线,只要设置足够多的阈值,就可以找出这一条直线.

API:void houghLins(源图像,输出寻找到的直线,double 像素为单位的距离精度,double 弧度为单位的角度精度,int 累加平面的阈值,double 多尺度变换的除数距离,double 多尺度时的单位角度的除数距离);

注:源为单通道,二值化图像,目标为输出矢量,并非mat,而是以极坐标参数对定义,一个vector里面包含若干参数对,距离精度为直线搜索时,距离尺寸的增长单位,角度精度为直线搜索时,每次增长的角度精度,阈值参数是当某一条直线被查找出来的时候,他在累加器中必须达到的累加器阈值,多尺度的角度精度和距离精度除数如果都为0,那么这是经典的霍夫变换.

经典霍夫变换的使用代码

  1. Mat srcImage,srcImageGray,cannyImage;
  2. vector<Vec2f>lines;
  3.  
  4. //三个变量,距离精度rho 角度精度 theta 阈值参数 threshold
  5. const int g_rhoMax = ;
  6. int g_rhoValue;
  7.  
  8. const int g_thetaMax = ;
  9. int g_thetaValue;
  10.  
  11. const int g_thresholdMax = ;
  12. int g_thresholdValue;
  13.  
  14. void onTrackBarrho(int pos,void* userData);
  15. void onTrackBartheta(int pos,void* userData);
  16. void onTrackBarThreshold(int pos,void* userData);
  17.  
  18. int main(int argc,char* argv[])
  19. {
  20. srcImage = imread("F:\\opencv\\OpenCVImage\\HoughLine.jpg");
  21. if(srcImage.channels() == )
  22. {
  23. srcImageGray = srcImage.clone();
  24. }
  25. else
  26. {
  27. cvtColor(srcImage, srcImageGray, CV_RGB2GRAY);
  28. }
  29.  
  30. namedWindow("src image");
  31. namedWindow("line image");
  32.  
  33. g_rhoValue = ;
  34. g_thetaValue = ;
  35. g_thresholdValue = ;
  36. createTrackbar("rho value", "line image", &g_rhoValue, g_rhoMax,onTrackBarrho,);
  37. createTrackbar("theta value", "line image", &g_thetaValue, g_thetaMax,onTrackBartheta ,);
  38. createTrackbar("threshold value", "line image", &g_thresholdValue, g_thresholdMax,onTrackBarThreshold,);
  39. onTrackBarThreshold(g_thresholdValue,);
  40.  
  41. imshow("src image", srcImageGray);
  42.  
  43. moveWindow("src image", , );
  44. moveWindow("line image", srcImageGray.cols*, );
  45.  
  46. waitKey();
  47. return ;
  48. }
  49.  
  50. void onTrackBarrho(int pos,void* userData)
  51. {
  52. int rhoValue = g_rhoValue+;
  53. int thetaValue = g_thetaValue+;
  54. int threshold = g_thresholdValue+;
  55. double theta = CV_PI/(double)thetaValue;
  56. Canny(srcImageGray, cannyImage, , ,);
  57. HoughLines(cannyImage, lines, rhoValue, theta, threshold,,);
  58. imshow("canny image", cannyImage);
  59. moveWindow("canny image", srcImageGray.cols, );
  60. Mat tempImage(srcImageGray.rows,srcImageGray.cols,CV_8UC3,Scalar(,,));
  61. for(size_t i = ; i < lines.size(); i++)
  62. {
  63. float rhoResult = lines[i][];
  64. float thetaResult = lines[i][];
  65. Point pt1,pt2;
  66. double a = cos(thetaResult);
  67. double b = sin(thetaResult);
  68. double x0 = a*rhoResult;
  69. double y0 = b*rhoResult;
  70. pt1.x = cvRound(x0 + *(-b));
  71. pt1.y = cvRound(y0 + *(a));
  72. pt2.x = cvRound(x0 - *(-b));
  73. pt2.y = cvRound(y0 - *(a));
  74. line(tempImage, pt1, pt2, Scalar(,,),);
  75. }
  76. imshow("line image", tempImage);
  77. }
  78. void onTrackBartheta(int pos,void* userData)
  79. {
  80. int rhoValue = g_rhoValue+;
  81. int thetaValue = g_thetaValue+;
  82. int threshold = g_thresholdValue+;
  83. double theta = CV_PI/(double)thetaValue;
  84. Canny(srcImageGray, cannyImage, , ,);
  85. HoughLines(cannyImage, lines, rhoValue, theta, threshold,,);
  86. imshow("canny image", cannyImage);
  87. moveWindow("canny image", srcImageGray.cols, );
  88. Mat tempImage(srcImageGray.rows,srcImageGray.cols,CV_8UC3,Scalar(,,));
  89. for(size_t i = ; i < lines.size(); i++)
  90. {
  91. float rhoResult = lines[i][];
  92. float thetaResult = lines[i][];
  93. Point pt1,pt2;
  94. double a = cos(thetaResult);
  95. double b = sin(thetaResult);
  96. double x0 = a*rhoResult;
  97. double y0 = b*rhoResult;
  98. pt1.x = cvRound(x0 + *(-b));
  99. pt1.y = cvRound(y0 + *(a));
  100. pt2.x = cvRound(x0 - *(-b));
  101. pt2.y = cvRound(y0 - *(a));
  102. line(tempImage, pt1, pt2, Scalar(,,),);
  103. }
  104. imshow("line image", tempImage);
  105. }
  106. void onTrackBarThreshold(int pos,void* userData)
  107. {
  108. int rhoValue = g_rhoValue+;
  109. int thetaValue = g_thetaValue+;
  110. int threshold = g_thresholdValue+;
  111. double theta = CV_PI/(double)thetaValue;
  112. Canny(srcImageGray, cannyImage, , ,);
  113. HoughLines(cannyImage, lines, rhoValue, theta, threshold,,);
  114. imshow("canny image", cannyImage);
  115. moveWindow("canny image", srcImageGray.cols, );
  116. Mat tempImage(srcImageGray.rows,srcImageGray.cols,CV_8UC3,Scalar(,,));
  117. for(size_t i = ; i < lines.size(); i++)
  118. {
  119. float rhoResult = lines[i][];
  120. float thetaResult = lines[i][];
  121. Point pt1,pt2;
  122. double a = cos(thetaResult);
  123. double b = sin(thetaResult);
  124. double x0 = a*rhoResult;
  125. double y0 = b*rhoResult;
  126. pt1.x = cvRound(x0 + *(-b));
  127. pt1.y = cvRound(y0 + *(a));
  128. pt2.x = cvRound(x0 - *(-b));
  129. pt2.y = cvRound(y0 - *(a));
  130. line(tempImage, pt1, pt2, Scalar(,,),);
  131. }
  132. imshow("line image", tempImage);
  133. }

二.累计概率霍夫变换

累计概率霍夫变换是使用的比较多的霍夫变换,因为和标准霍夫变换比起来,累计概率霍夫变换计算快一些,并且结果也足够精确.

API:void houghLineP(源图像,结果向量,double 进步距离,double 进角弧度,int 累计阈值,int 最小线段长度,int 同一行点与点之间的最小连接距离);

注:最小线段长度,默认为0,比这个参数短的线将不输出在结果中,连接最大距离,默认值为0,低于这个距离的将不构成同一根线,输出的点为四个元素的矢量,分别是(x1,y1,x2,y2);用vetc(vec4i)lines来表示,起点和终点坐标.

使用代码

  1. Mat srcImage,srcGrayImage,cannyImage;
  2. vector<Vec4i>lines;
  3.  
  4. //lowThreshold 默?认¨?upThreshold为alowthtreshold的Ì?三¨y倍À?
  5. const int g_lowThresholdMax = ;
  6. int g_lowThresholdValue;
  7. void onTrackBarCannyLowThreshold(int pos,void* userData);
  8. #define CALC_UPTHRESHOLD_VALUE(value) value*3;
  9.  
  10. //canny sobel 孔¡Á径?
  11. const int g_cannySobelSizeMax = ;
  12. int g_cannySobelSizeValue;
  13. void onTrackBarCannySobelSize(int pos,void* userData);
  14.  
  15. //hough 步?进?精?度¨¨
  16. const int g_houghlineRhoMax = ;
  17. int g_houghlineRhoValue;
  18. void onTrackBarHoughlineRhoValue(int pos,void* userData);
  19.  
  20. //houghlinep 角?度¨¨精?度¨¨
  21. const int g_houghlineThetaMax = ;
  22. int g_houghlineThetaValue;
  23. void onTrackBarHoughlineThetaValue(int pos,void* userData);
  24.  
  25. //累¤?计?阈D值¦Ì
  26. const int g_houghlineThresholdMax = ;
  27. int g_houghlineThresholdValue;
  28. void onTrackBarHoughlineThresholdValue(int pos,void* userData);
  29.  
  30. //最Á?小?线?段?长¡è度¨¨
  31. const int g_houghlineMindistMax = ;
  32. int g_houghlineMindistValue;
  33. void onTrackBarHoughlineMindistValue(int pos,void* userData);
  34.  
  35. int main(int argc,char* argv[])
  36. {
  37. srcImage = imread("F:\\opencv\\OpenCVImage\\HoughLineP.jpg");
  38. if(srcImage.channels() == )
  39. {
  40. srcGrayImage = srcImage.clone();
  41. }
  42. else
  43. {
  44. cvtColor(srcImage, srcGrayImage, CV_RGB2GRAY);
  45. }
  46.  
  47. namedWindow("src image");
  48. namedWindow("canny image");
  49. namedWindow("dst image");
  50.  
  51. g_lowThresholdValue = ;
  52. g_cannySobelSizeValue = ;
  53. createTrackbar("low threshold", "canny image", &g_lowThresholdValue, g_lowThresholdMax,onTrackBarCannyLowThreshold,);
  54. createTrackbar("sobel size", "canny image", &g_cannySobelSizeValue, g_cannySobelSizeMax,onTrackBarCannySobelSize,);
  55. onTrackBarCannySobelSize(g_cannySobelSizeValue, );
  56.  
  57. g_houghlineRhoValue = ;
  58. g_houghlineThetaValue = ;
  59. g_houghlineMindistValue = ;
  60. g_houghlineThresholdValue = ;
  61. createTrackbar("rho value", "dst image", &g_houghlineRhoValue, g_houghlineRhoMax,onTrackBarHoughlineRhoValue,);
  62. createTrackbar("theta value", "dst image", &g_houghlineThetaValue,g_houghlineThetaMax,onTrackBarHoughlineThetaValue,);
  63. createTrackbar("threshold value", "dst image", &g_houghlineThresholdValue, g_houghlineThresholdMax,onTrackBarHoughlineThresholdValue,);
  64. createTrackbar("min dist", "dst image", &g_houghlineMindistValue, g_houghlineMindistMax,onTrackBarHoughlineMindistValue,);
  65. onTrackBarHoughlineRhoValue(g_houghlineRhoValue, );
  66.  
  67. imshow("src image", srcImage);
  68.  
  69. moveWindow("src image", , );
  70. moveWindow("canny image", srcGrayImage.cols, );
  71. moveWindow("dst image", srcImage.cols*, );
  72.  
  73. waitKey();
  74. return ;
  75. }
  76.  
  77. void onTrackBarCannyLowThreshold(int pos,void* userData)
  78. {
  79. int sobelSize = g_cannySobelSizeValue*+;
  80. int lowThreshold = g_lowThresholdValue+;
  81. int upThreshold = CALC_UPTHRESHOLD_VALUE(lowThreshold);
  82. Canny(srcGrayImage, cannyImage, lowThreshold, upThreshold,sobelSize);
  83. imshow("canny image", cannyImage);
  84. }
  85.  
  86. void onTrackBarCannySobelSize(int pos,void* userData)
  87. {
  88. int sobelSize = g_cannySobelSizeValue*+;
  89. int lowThreshold = g_lowThresholdValue+;
  90. int upThreshold = CALC_UPTHRESHOLD_VALUE(lowThreshold);
  91. Canny(srcGrayImage, cannyImage, lowThreshold, upThreshold,sobelSize);
  92. imshow("canny image", cannyImage);
  93. }
  94.  
  95. void onTrackBarHoughlineRhoValue(int pos,void* userData)
  96. {
  97. double rhoValue = g_houghlineRhoValue+;
  98. double thetaValue = g_houghlineThetaValue+;
  99. int thresholdValue = g_houghlineThresholdValue+;
  100. int minDist = g_houghlineMindistValue+;
  101. HoughLinesP(cannyImage, lines, rhoValue, CV_PI/thetaValue, thresholdValue,minDist);
  102. Mat dstImage(srcGrayImage.rows,srcGrayImage.cols,CV_8UC3,Scalar(,,));
  103. for (size_t i = ; i < lines.size(); i++) {
  104. line(dstImage, Point(lines[i][],lines[i][]), Point(lines[i][],lines[i][]),Scalar(,,),LINE_4);
  105. }
  106. imshow("dst image", dstImage);
  107. }
  108.  
  109. void onTrackBarHoughlineThetaValue(int pos,void* userData)
  110. {
  111. double rhoValue = g_houghlineRhoValue+;
  112. double thetaValue = g_houghlineThetaValue+;
  113. int thresholdValue = g_houghlineThresholdValue+;
  114. int minDist = g_houghlineMindistValue+;
  115. HoughLinesP(cannyImage, lines, rhoValue, CV_PI/thetaValue, thresholdValue,minDist);
  116. Mat dstImage(srcGrayImage.rows,srcGrayImage.cols,CV_8UC3,Scalar(,,));
  117. for (size_t i = ; i < lines.size(); i++) {
  118. line(dstImage, Point(lines[i][],lines[i][]), Point(lines[i][],lines[i][]),Scalar(,,),LINE_4);
  119. }
  120. imshow("dst image", dstImage);
  121. }
  122.  
  123. void onTrackBarHoughlineThresholdValue(int pos,void* userData)
  124. {
  125. double rhoValue = g_houghlineRhoValue+;
  126. double thetaValue = g_houghlineThetaValue+;
  127. int thresholdValue = g_houghlineThresholdValue+;
  128. int minDist = g_houghlineMindistValue+;
  129. HoughLinesP(cannyImage, lines, rhoValue, CV_PI/thetaValue, thresholdValue,minDist);
  130. Mat dstImage(srcGrayImage.rows,srcGrayImage.cols,CV_8UC3,Scalar(,,));
  131. for (size_t i = ; i < lines.size(); i++) {
  132. line(dstImage, Point(lines[i][],lines[i][]), Point(lines[i][],lines[i][]),Scalar(,,),LINE_4);
  133. }
  134. imshow("dst image", dstImage);
  135. }
  136.  
  137. void onTrackBarHoughlineMindistValue(int pos,void* userData)
  138. {
  139. double rhoValue = g_houghlineRhoValue+;
  140. double thetaValue = g_houghlineThetaValue+;
  141. int thresholdValue = g_houghlineThresholdValue+;
  142. int minDist = g_houghlineMindistValue+;
  143. HoughLinesP(cannyImage, lines, rhoValue, CV_PI/thetaValue, thresholdValue,minDist);
  144. Mat dstImage(srcGrayImage.rows,srcGrayImage.cols,CV_8UC3,Scalar(,,));
  145. for (size_t i = ; i < lines.size(); i++) {
  146. line(dstImage, Point(lines[i][],lines[i][]), Point(lines[i][],lines[i][]),Scalar(,,),LINE_4);
  147. }
  148. imshow("dst image", dstImage);
  149. }

三.霍夫圆变换

霍夫圆变换与霍夫线变换的原理基本一致,任意一个圆上任意一个点也可以表示为以圆心,半径为代表的一个三维空间中的曲线,多个曲线的交点,代表一个确定的圆.

在OPENCV中,使用霍夫梯度法解决圆变换问题,具体过程是,首先对图像进行边缘检测,然后计算结果中不为0的值的XY方向的梯度,利用梯度累加斜率指定直线上的没一个点(斜率为指定最大值和最小值之间的距离),最后标记边缘图像中每一个非零像素的位置.,从二维累加器中选择候选中心.

霍夫圆变换的缺陷在于阈值设置如果比较低,那么算法消耗时间长,同心圆的间隔比较近的时候,倾向于保存半径最大的圆,并且霍夫梯度法在某些时候也会产生噪声.

API:void houghCircles(源图,结果向量,int 检测方法,int 输入图像和累加器的分辨率之比,int 圆心之间最小距离,double 算法高阈值,低阈值默认为高阈值1/2,double 圆心累累加器阈值,int 半径最小值,int 半径最大值);

注:源图像为八位单通道灰度图,目标向量为vecter<vec3f>,向量中存储的依次是圆心x,圆心y,半径r,检测方法目前只有霍夫梯度法HOUGH_GRENIENT      ,霍夫梯度法默认高阈值100.低阈值为其1/2,圆心间距太小,多个相邻的圆会重合,太大,某些圆不能被检测出,累加器阈值太小,会检测出很多不存在的圆,阈值越大,越容易检测出完美的圆

另外,函数可能找不到合适的半径供用户使用,可以忽略半径,用额外的步骤确定.

使用例程

  1. Mat srcImage,srcGrayImage;
  2. vector<Vec3f>circles;
  3.  
  4. //圆2形?最Á?小?距¨¤离¤?
  5. const int g_houghcircleMinDistMax = ;
  6. int g_houghcircleMinDistValue;
  7. void onTrackBarMinDist(int pos,void* userData);
  8.  
  9. //canny算?子Á¨®高?阈D值¦Ì
  10. const int g_houghcircleThresholdUpMax = ;
  11. int g_houghcircleThresholdUpValue;
  12. void onTrackBarThresholdUp(int pos,void* userData);
  13.  
  14. //圆2心?累¤?加¨®器¡Â阈D值¦Ì
  15. const int g_houghcircleCenterThresholdMax = ;
  16. int g_houghcircleCenterThresholdValue;
  17. void onTrackBarCenterThreshold(int pos,void* userData);
  18.  
  19. //最Á?小?半ã?径?
  20. const int g_houghcircleMinRadiusMax = ;
  21. int g_houghcircleMinRadiusValue;
  22. void onTrackBarRadiusMin(int pos,void* userData);
  23.  
  24. //最Á?大䨮半ã?径?
  25. const int g_houghcircleMaxRadiusMax = ;
  26. int g_houghcircleMaxRadiusValue;
  27. void onTrackBarRadiusMax(int pos,void* userData);
  28.  
  29. int main(int argc,char* argv[])
  30. {
  31. srcImage = imread("F:\\opencv\\OpenCVImage\\HoughCircle.jpg");
  32. if(srcImage.channels() == )
  33. {
  34. cvtColor(srcImage, srcGrayImage, CV_RGB2GRAY);
  35. }
  36. else
  37. {
  38. srcGrayImage = srcImage.clone();
  39. }
  40.  
  41. namedWindow("src gray image");
  42. namedWindow("dst image");
  43.  
  44. g_houghcircleMinDistValue = ;
  45. g_houghcircleThresholdUpValue = ;
  46. g_houghcircleCenterThresholdValue = ;
  47. g_houghcircleMinRadiusValue = ;
  48. g_houghcircleMaxRadiusValue = ;
  49. createTrackbar("center min dist", "dst image", &g_houghcircleMinDistValue, g_houghcircleMinDistMax,onTrackBarMinDist,);
  50. createTrackbar("canny threshold", "dst image", &g_houghcircleThresholdUpValue, g_houghcircleThresholdUpMax,onTrackBarThresholdUp,);
  51. createTrackbar("center threshold", "dst image", &g_houghcircleCenterThresholdValue, g_houghcircleCenterThresholdMax,onTrackBarCenterThreshold,);
  52. createTrackbar("min radius", "dst image", &g_houghcircleMinRadiusValue, g_houghcircleMinRadiusMax,onTrackBarRadiusMin,);
  53. createTrackbar("max radius", "dst image", &g_houghcircleMaxRadiusValue, g_houghcircleMaxRadiusMax,onTrackBarRadiusMax,);
  54. onTrackBarRadiusMin(g_houghcircleMinDistValue,);
  55.  
  56. imshow("src gray image", srcGrayImage);
  57.  
  58. moveWindow("src gray image", , );
  59. moveWindow("dst image", srcGrayImage.cols, );
  60.  
  61. waitKey();
  62. return ;
  63. }
  64.  
  65. void onTrackBarMinDist(int pos,void* userData)
  66. {
  67. int minDist = g_houghcircleMinDistValue+;
  68. double cannyThreshold = (double)g_houghcircleThresholdUpValue+;
  69. double centerThreshold = (double)g_houghcircleCenterThresholdValue+;
  70. int minRadius = g_houghcircleMinRadiusValue+;
  71. int maxRadius = g_houghcircleMaxRadiusValue+;
  72. if(minRadius >= maxRadius)
  73. {
  74. imshow("dst image", srcGrayImage);
  75. }
  76. else
  77. {
  78. Mat dstImage(srcGrayImage.rows,srcGrayImage.cols,CV_8UC3,Scalar(,,));
  79. HoughCircles(srcGrayImage, circles, HOUGH_GRADIENT, , minDist,cannyThreshold,centerThreshold,minRadius,maxRadius);
  80. for (size_t i = ; i < circles.size(); i++) {
  81. circle(dstImage, Point(circles[i][],circles[i][]), circles[i][], Scalar(,,));
  82. }
  83. imshow("dst image", dstImage);
  84. }
  85. }
  86. void onTrackBarThresholdUp(int pos,void* userData)
  87. {
  88. int minDist = g_houghcircleMinDistValue+;
  89. double cannyThreshold = (double)g_houghcircleThresholdUpValue+;
  90. double centerThreshold = (double)g_houghcircleCenterThresholdValue+;
  91. int minRadius = g_houghcircleMinRadiusValue+;
  92. int maxRadius = g_houghcircleMaxRadiusValue+;
  93. if(minRadius >= maxRadius)
  94. {
  95. imshow("dst image", srcGrayImage);
  96. }
  97. else
  98. {
  99. Mat dstImage(srcGrayImage.rows,srcGrayImage.cols,CV_8UC3,Scalar(,,));
  100. HoughCircles(srcGrayImage, circles, HOUGH_GRADIENT, , minDist,cannyThreshold,centerThreshold,minRadius,maxRadius);
  101. for (size_t i = ; i < circles.size(); i++) {
  102. circle(dstImage, Point(circles[i][],circles[i][]), circles[i][], Scalar(,,),LINE_AA);
  103. }
  104. imshow("dst image", dstImage);
  105. }
  106. }
  107. void onTrackBarCenterThreshold(int pos,void* userData)
  108. {
  109. int minDist = g_houghcircleMinDistValue+;
  110. double cannyThreshold = (double)g_houghcircleThresholdUpValue+;
  111. double centerThreshold = (double)g_houghcircleCenterThresholdValue+;
  112. int minRadius = g_houghcircleMinRadiusValue+;
  113. int maxRadius = g_houghcircleMaxRadiusValue+;
  114. if(minRadius >= maxRadius)
  115. {
  116. imshow("dst image", srcGrayImage);
  117. }
  118. else
  119. {
  120. Mat dstImage(srcGrayImage.rows,srcGrayImage.cols,CV_8UC3,Scalar(,,));
  121. HoughCircles(srcGrayImage, circles, HOUGH_GRADIENT, , minDist,cannyThreshold,centerThreshold,minRadius,maxRadius);
  122. for (size_t i = ; i < circles.size(); i++) {
  123. circle(dstImage, Point(circles[i][],circles[i][]), circles[i][], Scalar(,,));
  124. }
  125. imshow("dst image", dstImage);
  126. }
  127. }
  128. void onTrackBarRadiusMax(int pos,void* userData)
  129. {
  130. int minDist = g_houghcircleMinDistValue+;
  131. double cannyThreshold = (double)g_houghcircleThresholdUpValue+;
  132. double centerThreshold = (double)g_houghcircleCenterThresholdValue+;
  133. int minRadius = g_houghcircleMinRadiusValue+;
  134. int maxRadius = g_houghcircleMaxRadiusValue+;
  135. if(minRadius >= maxRadius)
  136. {
  137. imshow("dst image", srcGrayImage);
  138. }
  139. else
  140. {
  141. Mat dstImage(srcGrayImage.rows,srcGrayImage.cols,CV_8UC3,Scalar(,,));
  142. HoughCircles(srcGrayImage, circles, HOUGH_GRADIENT, , minDist,cannyThreshold,centerThreshold,minRadius,maxRadius);
  143. for (size_t i = ; i < circles.size(); i++) {
  144. circle(dstImage, Point(circles[i][],circles[i][]), circles[i][], Scalar(,,));
  145. }
  146. imshow("dst image", dstImage);
  147. }
  148. }
  149. void onTrackBarRadiusMin(int pos,void* userData)
  150. {
  151. int minDist = g_houghcircleMinDistValue+;
  152. double cannyThreshold = (double)g_houghcircleThresholdUpValue+;
  153. double centerThreshold = (double)g_houghcircleCenterThresholdValue+;
  154. int minRadius = g_houghcircleMinRadiusValue+;
  155. int maxRadius = g_houghcircleMaxRadiusValue+;
  156. if(minRadius >= maxRadius)
  157. {
  158. imshow("dst image", srcGrayImage);
  159. }
  160. else
  161. {
  162. Mat dstImage(srcGrayImage.rows,srcGrayImage.cols,CV_8UC3,Scalar(,,));
  163. HoughCircles(srcGrayImage, circles, HOUGH_GRADIENT, , minDist,cannyThreshold,centerThreshold,minRadius,maxRadius);
  164. for (size_t i = ; i < circles.size(); i++) {
  165. circle(dstImage, Point(circles[i][],circles[i][]), circles[i][], Scalar(,,));
  166. }
  167. imshow("dst image", dstImage);
  168. }
  169. }

四.重映射

重映射是指将一张图片上的某个指定位置的像素拷贝到目标图像的某一个位置上

API:void remap(源图,目的图,映射数组1,映射数组2,int 插值方式,int 边界模式,int 边界值)

注:源和目标都必须为八位单通道图像或者浮点单通道图像

使用例程

  1. int main(int argc,char* argv[])
  2. {
  3. Mat srcImage,mapx,mapy,dstImage;
  4. srcImage = imread("F:\\opencv\\OpenCVImage\\remap.jpg");
  5. mapx.create(srcImage.size(), CV_32FC1);
  6. mapy.create(srcImage.size(), CV_32FC1);
  7. dstImage.create(srcImage.rows, srcImage.cols, srcImage.type());
  8.  
  9. //核?心?就¨ª是º?这a个?mapx 和¨ªmapy
  10. for(int j = ; j < srcImage.rows;j++)
  11. {
  12. for (int i = ; i < srcImage.cols; i++)
  13. {
  14. mapx.at<float>(j,i) = static_cast<float>(i);
  15. mapy.at<float>(j,i) = static_cast<float>(srcImage.rows-j);
  16. }
  17. }
  18. remap(srcImage, dstImage, mapx, mapy, INTER_LINEAR,BORDER_CONSTANT,Scalar(,,));
  19. imshow("src image", srcImage);
  20. imshow("dst image", dstImage);
  21.  
  22. moveWindow("src image", , );
  23. moveWindow("dst image", srcImage.cols, );
  24.  
  25. waitKey();
  26. return ;
  27. }

五.仿射变换

集合中,一个向量空间进行一次线性变换并加上那个一次平移,变成另一个向量空间,叫做仿射变换,一个任意的仿射变换都可以表示成乘以一个矩阵,再加上一个向量的形式,

仿射变换可以实现图片的缩放,旋转,平移,其代表的是两张图片之间的映射关系.

API:void warpAffine(源图像,目标图像,变换矩阵,Size 输出图像尺寸,int 插值方式,int 边界模式,int 恒定边界取值)

注:源可以使多通道或者单通道,目标和源的类型一致,变换矩阵一般来说可以通过getRotationMatrix2D来获得,而不需要自己去实现.获得的是一个2*3的变换矩阵,插值方式默认为INTER_LINEAR,和RESIZE插值方式相似,但是又添加了两种新的插值方式,CV_WARP_FILL_OUTLIERS填充所有输出图像的像素,CV_WARP_INVERSE_MAP表明该变换试一次输出图像到输入图像的逆变换.

API Mat getRotationMatrix2D(Point源图的旋转中心,double 旋转角度,double 缩放系数)

注:该API反悔的矩阵可以作为仿射变换的变换矩阵使用

使用代码实际例程如下

  1. Mat srcImage,dstImage,rotationImage;
  2. const int g_angelMax = ;
  3. int g_angelValue;
  4. void onTrackBarRotationAngel(int pos,void* userData);
  5.  
  6. const int g_scaleMax = ;//最Á?大䨮10倍À?缩?放¤?系¦Ì数ºy
  7. int g_scaleValue;
  8. void onTrackBarScale(int pos,void* userData);
  9.  
  10. int main(int argc,char* argv[])
  11. {
  12. srcImage = imread("F:\\opencv\\OpenCVImage\\warpAffine.jpg");
  13. namedWindow("src image");
  14. namedWindow("dst image");
  15.  
  16. g_angelValue = ;
  17. g_scaleValue = ;
  18. createTrackbar("angel value", "dst image", &g_angelValue, g_angelMax,onTrackBarRotationAngel,);
  19. createTrackbar("scale value", "dst image", &g_scaleValue, g_scaleMax,onTrackBarScale,);
  20. onTrackBarRotationAngel(g_angelValue,);
  21.  
  22. imshow("src image", srcImage);
  23.  
  24. moveWindow("src image", , );
  25. moveWindow("dst image", srcImage.cols, );
  26.  
  27. waitKey();
  28. return ;
  29. }
  30.  
  31. void onTrackBarRotationAngel(int pos,void* userData)
  32. {
  33. if(g_scaleValue == )g_scaleValue = ;
  34. double scale = 1.0/g_scaleValue;
  35. rotationImage = getRotationMatrix2D(Point(srcImage.cols/,srcImage.rows/), g_angelValue, scale);
  36. warpAffine(srcImage, dstImage, rotationImage, srcImage.size(),INTER_LINEAR);
  37. imshow("dst image", dstImage);
  38. }
  39.  
  40. void onTrackBarScale(int pos,void* userData)
  41. {
  42. if(g_scaleValue == )g_scaleValue = ;
  43. double scale = 1.0/g_scaleValue;
  44. rotationImage = getRotationMatrix2D(Point(srcImage.cols/,srcImage.rows/), g_angelValue, scale);
  45. warpAffine(srcImage, dstImage, rotationImage, srcImage.size(),INTER_LINEAR);
  46. imshow("dst image", dstImage);
  47. }

六.直方图均衡化

如果需要扩大图像的动态范围,最常使用的技术就是直方图均衡化,属于弧度变幻的一个重要应用,用一定的算法,使直方图大致平和的过程.拉伸像素强度的分布范围来增强图像的对比度,例如原先图像的像素灰度集中的100-200区域,那么将100-200区域的数据扩展到0-255,就属于直方图均衡化.

均衡化以后的图像只能近似的均匀分布,动态范围扩大,本质是扩大了量化间距,而量化级别反而减少了.这是一个缺点,均衡化以后可能会出现伪轮廓,或者消除原来图像的有效轮廓.

在泛白缓和的图像中,均衡化会合并一些像素灰度,从而增大对比度

原始图像对比度本省就比较高的,在均衡化会造成灰度调和,从而降低对比度

从色彩上说,均衡化会是图像的表现力更出色.

API:void equalizeHist(源图像,目标图像)

源和目标,都必须为八位单通道图像

使用实例

  1. int main(int argc,char* argv[])
  2. {
  3. Mat srcImage,dstImage;
  4. srcImage = imread("F:\\opencv\\OpenCVImage\\equalizeHist.jpg");
  5. if(srcImage.channels() != )
  6. {
  7. cvtColor(srcImage, srcImage, CV_RGB2GRAY);
  8. }
  9.  
  10. equalizeHist(srcImage, dstImage);
  11.  
  12. //Mat dstRgbImage(srcImage.rows,srcImage.cols,CV_8UC3);
  13. //cvtColor(dstImage, dstRgbImage, CV_GRAY2BGR);
  14.  
  15. imshow("src image", srcImage);
  16. imshow("dst image", dstImage);
  17. moveWindow("src image", , );
  18. moveWindow("dst image", srcImage.cols, );
  19. waitKey();
  20. return ;
  21. }

OPENCV图像变换-2的更多相关文章

  1. OPENCV图像变换-1

    图像变换是指将一幅图像变换为图像数据的另一种表现形式,例如将图像进行傅立叶变换,或者对图像进行X,Y方向的求导等,经过这些变换,可以将图像数据处理中的某些问题换一个别的角度想办法,所以图像变换是图像处 ...

  2. OpenCV图像变换(仿射变换与透视变换)

    仿射变换(affine transform)与透视变换(perspective transform)在图像还原.图像局部变化处理方面有重要意义.通常,在2D平面中,仿射变换的应用较多,而在3D平面中, ...

  3. OpenCV —— 图像变换

    将一副图像转变成另一种表现形式 ,比如,傅里叶变换将图像转换成频谱分量 卷积 —— 变换的基础 cvFilter2D  源图像 src 和目标图像 dst 大小应该相同 注意:卷积核的系数应该是浮点类 ...

  4. python+opencv图像变换的两种方法cv2.warpAffine和cv2.warpPerspective

    本文链接:https://blog.csdn.net/qq_27261889/article/details/80720359 # usr/bin/env python # coding: utf- ...

  5. OpenCV图像变换二 投影变换与极坐标变换实现圆形图像修正

    投影变换 在放射变换中,物体是在二维空间中变换的.如果物体在三维空间中发生了旋转,那么这种变换就成为投影变换,在投影变换中就会出现阴影或者遮挡,我们可以运用二维投影对三维投影变换进行模块化,来处理阴影 ...

  6. <学习opencv>图像变换

    拉伸.收缩.扭曲和旋转 统一调整大小 我们经常会遇到一些我们希望转换为其他尺寸的图像. 我们可能想要扩大或缩小图像; 这两项任务都是由同一个功能完成的. cv::resize() 该cv::resiz ...

  7. [opencv]<学习Opencv>英文原版翻译学习

    [注]下文全部内容为 <<Learning OpenCV 3: Computer Vision in C++ with the OpenCV Library>>经由在线翻译整理 ...

  8. pyhton:图像旋转

    最近一个作业中要用到图像旋转,分享一下学习过程.如有讲错的地方,恳请指正! 图像旋转,想想真简单啊,不就是将图像矩阵乘上一个旋转平移矩阵就完了吗?实际上还真没这么简单.首先这个旋转平移矩阵怎么获得?通 ...

  9. OpenCV-Python Tutorials目录

    版本 3.4.6 1 Introduction to OpenCV OpenCV介绍Learn how to setup OpenCV-Python on your computer! 2 Gui F ...

随机推荐

  1. hash随笔

    hash属性是一个可读可写的字符串,是url的锚部分(从#开始).多用于单页面应用中,使其包含多个页面. 定位:通过id来定位 eg: <div id= "part1"> ...

  2. 转 EXPDP ORA-39095 ORA-3909 错误

    今天帮同事在测试库上导出两张表,第一张表大小为30M,第二章表大小为7G,以下是expdp语法 expdp tranuser/******** parallel=4 content=data_only ...

  3. java跳过构造方法新建对象

    Java有四种创建对象的方法: (1) 用new语句创建对象,这是最常见的创建对象的方法. (2) 运用反射手段,调用java.lang.Class或者java.lang.reflect.Constr ...

  4. Intersecting Lines POJ 1269

    题目大意:给出两条直线,每个直线上的两点,求这两条直线的位置关系:共线,平行,或相交,相交输出交点. 题目思路:主要在于求交点 F0(X)=a0x+b0y+c0==0; F1(X)=a1x+b1y+c ...

  5. 最小点集覆盖=最大匹配<二分图>/证明

    来源 最小点集覆盖==最大匹配. 首先,最小点集覆盖一定>=最大匹配,因为假设最大匹配为n,那么我们就得到了n条互不相邻的边,光覆盖这些边就要用到n个点. 现在我们来思考为什么最小点击覆盖一定& ...

  6. LightOJ 1341 Aladdin and the Flying Carpet(整数拆分定理)

    分析:题目并不难理解,就是一些细节上的优化需要我们注意,我在没有优化前跑了2000多MS,优化了一些细节后就是400多MS了,之前还TLE了好几次. 方法:将整数拆分为质因子以后,表达为这样的形式,e ...

  7. elasticsearch高级配置之(一)----分片分布规则设置

    cluster.routing.allocation.allow_rebalance 设置根据集群中机器的状态来重新分配分片,可以设置为always, indices_primaries_active ...

  8. C#无边框窗体移动 将事件绑定到想实现的控件上

    [DllImport("user32.dll")] public static extern bool ReleaseCapture(); [DllImport("use ...

  9. CodeForces 525D Arthur and Walls

    广搜.看了官方题解才会的..... 定义2*2的小矩阵,有三个是点,一个是星,这样的小矩阵被称为元素块. 首先把所有元素块压入队列,每次取出对头,检查是否还是元素块,如果是 那么将那个*改为点,否则跳 ...

  10. 《云阅》一个仿网易云音乐UI,使用Gank.Io及豆瓣Api开发的开源项目

    CloudReader 一款基于网易云音乐UI,使用GankIo及豆瓣api开发的符合Google Material Desgin阅读类的开源项目.项目采取的是Retrofit + RxJava + ...