- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 OpenCV 进行一个项目,Python 在项目的某些部分使用概率霍夫线变换函数“HoughLinesP”。我的代码工作得很好,没有问题。然后我想到将相同的代码转换为 C++。
将代码转换为C++后,输出与Python代码不一样。经过长时间的调试,我发现其他一切正常,但在 C++ 的情况下,“HoughLinesP”函数给出了不同的输出。两种语言中此函数的输入相同,参数值也相同,但输出不同。
有人可以解释一下为什么会发生这种情况以及任何可能的解决方法吗?
另外,我检查了这两种语言的 OpenCV 版本,它是相同的:4.5.0 dev
此外,我尝试使用传递给 C++ 代码的值,但我无法获得类似的结果。
输入边缘图像:
Python HoughLinesP() 输出:
C++ HoughLinesP() 输出:
以下是每种语言的代码:
Python:
Lines = cv2.HoughLinesP(EdgeImage, 1, np.pi / 180, 50, 10, 15)
C++:
std::vector<cv::Vec4i> Lines;
cv::HoughLinesP(EdgeImage, Lines, 1, CV_PI / 180, 50, 10, 15);
如果有人可以提出建议,那将是一个很大的帮助。
最佳答案
解释和修复
出现问题是因为在 Python 版本中您没有设置您认为正在设置的参数。与在 Python 接口(interface)中调整参数列表的其他一些函数相比,HoughLinesP
不仅返回行,还接受参数 lines
用于线路输出。您可以在 HoughLinesP
的帮助中看到这一点。 :
import cv2
help(cv2.HoughLinesP)
这给了你(省略号我的):
Help on built-in function HoughLinesP:
HoughLinesP(...)
HoughLinesP(image, rho, theta, threshold[, lines[, minLineLength[, maxLineGap]]]) -> lines
. @brief Finds line segments in a binary image using the probabilistic Hough transform.
.
...
. @param lines Output vector of lines. Each line is represented by a 4-element vector
. \f$(x_1, y_1, x_2, y_2)\f$ , where \f$(x_1,y_1)\f$ and \f$(x_2, y_2)\f$ are the ending points of each detected
. line segment.
...
所以基本上,在您的 python 示例中,您通过
10
如
lines
而不是
minLineLength
.要解决此问题,您可以将一个空数组作为
lines
传递。或者您可以将参数作为关键字参数传递:
Lines = cv2.HoughLinesP(EdgeImage, rho=1, theta=np.pi/180,
threshold=50, minLineLength=10, maxLineGap=15)
这样做应该使您的 Python 版本的输出与 C++ 版本的输出匹配。
lines
(即仅将
minLineLength
设置为 15 并为
maxLineGap
[
see docs ] 使用默认值 0):
std::vector<cv::Vec4i> Lines;
cv::HoughLinesP(EdgeImage, Lines, 1, CV_PI / 180, 50, 15);
这应该会重现您的 Python 版本。
HoughLinesP
中列出的示例,您可以看到这解决了问题。
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>
using namespace cv;
using namespace std;
int main(int argc, char** argv)
{
Mat src, dst, color_dst;
if( argc != 3 || !(src=imread(argv[1], 0)).data)
return -1;
Canny( src, dst, 50, 200, 3 );
cvtColor( dst, color_dst, COLOR_GRAY2BGR );
vector<Vec4i> lines;
HoughLinesP( dst, lines, 1, CV_PI/180, 80, 30, 10 );
for( size_t i = 0; i < lines.size(); i++ )
{
line( color_dst, Point(lines[i][0], lines[i][1]),
Point( lines[i][2], lines[i][3]), Scalar(0,0,255), 3, 8 );
}
imwrite( argv[2], color_dst );
return 0;
}
如果你编译它并在文档中提供的示例图片上运行它,你会得到以下结果:
import argparse
import cv2
import numpy as np
parser = argparse.ArgumentParser()
parser.add_argument("input_file", type=str)
parser.add_argument("output_file", type=str)
args = parser.parse_args()
src = cv2.imread(args.input_file, 0)
dst = cv2.Canny(src, 50., 200., 3)
color_dst = cv2.cvtColor(dst, cv2.COLOR_GRAY2BGR)
lines = cv2.HoughLinesP(dst, 1., np.pi/180., 80, 30, 10.)
for this_line in lines:
cv2.line(color_dst,
(this_line[0][0], this_line[0][1]),
(this_line[0][2], this_line[0][3]),
[0, 0, 255], 3, 8)
cv2.imwrite(args.output_file, color_dst)
运行它会给出以下(不同的)结果:
import argparse
import cv2
import numpy as np
parser = argparse.ArgumentParser()
parser.add_argument("input_file", type=str)
parser.add_argument("output_file", type=str)
args = parser.parse_args()
src = cv2.imread(args.input_file, 0)
dst = cv2.Canny(src, 50., 200., 3)
color_dst = cv2.cvtColor(dst, cv2.COLOR_GRAY2BGR)
lines = cv2.HoughLinesP(dst, rho=1., theta=np.pi/180.,
threshold=80, minLineLength=30, maxLineGap=10.)
for this_line in lines:
cv2.line(color_dst,
(this_line[0][0], this_line[0][1]),
(this_line[0][2], this_line[0][3]),
[0, 0, 255], 3, 8)
cv2.imwrite(args.output_file, color_dst)
这给出了正确的结果(即与 C++ 版本相同的结果):
关于python - OpenCV 概率霍夫线变换使用 C++ 和 Python 给出不同的结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64445333/
我正在寻找一种简单的解决方案来将对象从一个地方移动和缩放到另一个地方。 我做了一个JSfiddle用这个代码。问题是我需要它在某个时刻停止变小。所以它有一个最小尺寸,另一个问题是我希望它既缩小又向左移
我正在尝试通过沿 x 轴上下翻转(180 度)来为悬停时的图像设置动画。 就像here 除非我出于某种原因无法让它工作。 img { transition:all 2s ease-in-out
我想实例化一个 slider 约束,它允许 body 在 A 点和 B 点之间滑动。 为了实例化约束,我指定了两个物体进行约束,在这种情况下,一个动态物体被约束到静态世界,比如滑动门。 第三个和第四个
我想使用此功能旋转然后停止在特定点或角度。现在该元素只是旋转而不停止。代码如下: $(function() { var $elie = $("#bkgimg");
我正在尝试使用 CATransform3D 向 View 添加透视图。目前,这就是我得到的: 这就是我想要得到的: 我很难做到这一点。我完全迷失在这里。这是我的代码: CATransform3D t
我编写了一个图形用户界面,用户可以在其中在 (640x480) 窗口中绘制内容。它使该绘图成为一组存储在 Vector 数组中的点。 现在,我如何将这些点集平移到原点(0,0 窗口左上角)或将其放在指
我的应用程序中有两张图像相互叠加,分别表示为 foreground 和 background。对于这两个,我都使用 background-attachment: fixed 来确保图像始终彼此完全相同
如何在不损失质量的情况下应用旋转变换?我试过添加 translateZ(0) 但它无济于事。这是例子: svg { background-color: rgb(93, 193, 93); }
我有一个 div,我试图在悬停时缩放它(只是 Y)。问题是它在没有过渡的情况下运行良好。当我使用过渡时,div 在顶部缩放一点然后下降,检查 fiddle 。问题是如何防止 div 那样缩放?我希望它
我正在尝试使用 transform: scale 图像网格 http://movies.themodern-nerd.com/genre .从左向右滚动时它工作正常,悬停的图像将停留在其他图像之上,但
我正在查看 CSS3 Transform 并且想要一个既倾斜又旋转的盒子。 我试过使用: transform:rotate(80deg); -moz-transform:rotate(80deg);
当用户在图像父元素上执行 mousemove 时,我试图在 img 上添加平滑移动效果(此处为 .carousel-img)但我无法正常运行它。 我做错了什么? $('.carousel-img').
我有 div 元素在其他 div 元素中垂直对齐。 我使用以下方法对齐它们:position: relative;变换:翻译Y(-50%);顶部:50%。这很好用。 我现在想缩放元素(使用 jQuer
我在这个 fiddle 中使用 RotateX 后创建了 3D 效果: http://jsfiddle.net/vEWEL/11/ 但是,我不确定如何在这个 Fiddle 中的红色方 block 上实
使用 transform: scale(x.x) 而不是使用 width 和 height 属性进行传统的调整大小有什么缺点吗?缩放会产生质量较低的图像或其他什么吗? 最佳答案 Scale 生成总体上
我在一个点上有一个对象,比如相对于原点的 x、y、z。 我想对点应用一些变换,比如旋转和平移,并在变换后的点渲染对象。我正在使用 glTranslatef() 和 glRotatef() 函数。它看起
有没有办法将转换应用到插入了 :before 的元素上? 以下方法无效,但我愿意接受其他解决方案。 .itemclass:before { content: "➨"; transform:
我找到了这个:width/height after transform 和其他几个,但没有什么不是我正在寻找的。我想要的是将某些东西缩放到其大小的 50%(当然还有漂亮的动画过渡)并让页面布局重新调整
我想使用变换为元素位置设置动画。我怎么能在这个翻译中添加一些曲线(没什么特别的,只是不是一条完整的直线)?对于 jquery,我会使用效果很好的 easeInSine。 var a = documen
我试着写一个 TransformMesh功能。该函数接受一个 Mesh对象和 Matrix目的。这个想法是使用矩阵来转换网格。为此,我锁定了顶点缓冲区,并在每个顶点上调用了 Vector3::Tran
我是一名优秀的程序员,十分优秀!