我使用HoughLinesP和Python中的OpenCV在这个图像中找到棋盘上的线很困难.
为了理解HoughLinesP的参数,我提出了以下代码:
import numpy as np
import cv2
from matplotlib import pyplot as plt
from matplotlib import image as image
I = image.imread('chess.jpg')
G = cv2.cvtColor(I, cv2.COLOR_BGR2GRAY)
# Canny Edge Detection:
Threshold1 = 150;
Threshold2 = 350;
FilterSize = 5
E = cv2.Canny(G, Threshold1, Threshold2, FilterSize)
Rres = 1
Thetares = 1*np.pi/180
Threshold = 1
minLineLength = 1
maxLineGap = 100
lines = cv2.HoughLinesP(E,Rres,Thetares,Threshold,minLineLength,maxLineGap)
N = lines.shape[0]
for i in range(N):
x1 = lines[i][0][0]
y1 = lines[i][0][1]
x2 = lines[i][0][2] …
Run Code Online (Sandbox Code Playgroud) 我正在开发一个使用 OpenCV 的项目,Python 在项目的某些部分使用概率霍夫线变换函数“HoughLinesP”。我的代码工作得很好,没有问题。然后我想到将相同的代码转换为 C++。
将代码转换为 C++ 后,输出与 Python 代码的输出不一样。经过长时间的调试,我发现其他一切正常,但“HoughLinesP”函数在 C++ 的情况下给出了不同的输出。两种语言对这个函数的输入是一样的,参数的值也一样,但是输出是不同的。
有人可以解释一下为什么会发生这种情况以及任何可能的修复方法吗?
此外,我已经检查了两种语言的 OpenCV 版本,它是相同的:
4.5.0 dev
此外,我尝试使用传递给 C++ 代码的值,但我无法获得类似的结果。
输入边缘图像:
Python HoughLinesP() 输出:
C++ HoughLinesP() 输出:
以下是每种语言的代码: Python:
Lines = cv2.HoughLinesP(EdgeImage, 1, np.pi / 180, 50, 10, 15)
Run Code Online (Sandbox Code Playgroud)
C++:
std::vector<cv::Vec4i> Lines;
cv::HoughLinesP(EdgeImage, Lines, 1, CV_PI / 180, 50, 10, 15);
Run Code Online (Sandbox Code Playgroud)
如果有人可以提出建议,那将是一个很大的帮助。
我试图获得他们在本教程中为HoughLinesP过滤器获得的相同结果.我拍摄了相同的图像和相同的阈值,如下所示:
import cv2
from line import Line
import numpy as np
img = cv2.imread('building.jpg',1)
cannied = cv2.Canny(img, 50, 200, 3)
lines = cv2.HoughLinesP(cannied, 1, np.pi / 180, 80, 30, 10)
for leftx, boty, rightx, topy in lines[0]:
line = Line((leftx, boty), (rightx,topy))
line.draw(img, (255, 255, 0), 2)
cv2.imwrite('lines.png',img)
cv2.imwrite('canniedHouse.png',cannied)
cv2.waitKey(0)
cv2.destroyAllWindows()
Run Code Online (Sandbox Code Playgroud)
Line
class是一个自定义类,它不会做任何有趣的事情只计算一些东西并且可以画线.然后我得到这两个图像:
所以你可以看到我在图像的中间只有一条litle线.
不确定出了什么问题.我错过了什么?
谢谢.
我有下面的乐高板图像,上面有一些积木
现在我尝试用 OpenCV 检测粗黑线(连接白色方块)。我已经用 HoughLinesP 进行了很多实验,之前将图像转换为灰色或黑白,应用了模糊,...Nonthing 产生了可用的结果。
# Read image
img = cv2.imread('image.jpg', cv2.IMREAD_GRAYSCALE)
# Resize Image
img = cv2.resize(img, (0,0), fx=0.25, fy=0.25)
# Initialize output
out = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
# Median blurring to get rid of the noise; invert image
img = cv2.medianBlur(img, 5)
# Adaptive Treshold
bw = cv2.adaptiveThreshold(img,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,\
cv2.THRESH_BINARY,15,8)
# HoughLinesP
linesP = cv2.HoughLinesP(bw, 500, np.pi / 180, 50, None, 50, 10)
# Draw Lines
if linesP is not None:
for i in range(0, len(linesP)):
l = …
Run Code Online (Sandbox Code Playgroud) 我试图floor plan
通过检测直线和边缘来检测许多图片的主要结构,参考here。
上面的例子是我需要处理的一个例子,是否可以通过检测线来获得主要结构opencv HoughLinesP
?提前感谢您的帮助。
import cv2
import numpy as np
def get_lines(lines_in):
if cv2.__version__ < '3.0':
return lines_in[0]
return [l[0] for l in lines]
img = cv2.imread('./test.jpg', 1)
img_gray = gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
cannied = cv2.Canny(img_gray, threshold1=50, threshold2=200, apertureSize=3)
lines = cv2.HoughLinesP(cannied, rho=1, theta=np.pi / 180, threshold=80, minLineLength=30, maxLineGap=10)
for line in get_lines(lines):
leftx, boty, rightx, topy = line
cv2.line(img, (leftx, boty), (rightx,topy), (255, 255, 0), 2)
cv2.imwrite('./lines.png', img)
cv2.imwrite('./canniedHouse.png', cannied)
cv2.waitKey(0)
cv2.destroyAllWindows() …
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用opencv 4 android sdk检测矩形文档。首先我试图通过找到轮廓来检测它,但是它不能用于多彩色文档。您可以检查此链接以获得更好的主意: 使用OpenCV4Android检测多彩色文档
我进行了很多研究,发现可以使用houghline变换来完成它,因此我遵循以下方法来检测文档:
原始图像-> cvtColor->高斯模糊滤波器->对其进行扩张以锐化边缘->应用分水岭图像分割算法->具有动态otsu阈值的canny边缘检测->然后应用霍夫线变换
我为霍夫线变换所做的是:
Imgproc.HoughLinesP(watershedMat, lines, 1, Math.PI / 180, 50, 100, 50);
List<Line> horizontals = new ArrayList<>();
List<Line> verticals = new ArrayList<>();
for (int x = 0; x < lines.rows(); x++)
{
double[] vec = lines.get(x, 0);
double x1 = vec[0],
y1 = vec[1],
x2 = vec[2],
y2 = vec[3];
Point start = new Point(x1, y1);
Point end = new Point(x2, y2);
Line line = new Line(start, end);
if (Math.abs(x1 - x2) > Math.abs(y1-y2)) …
Run Code Online (Sandbox Code Playgroud) opencv edge-detection hough-transform opencv4android houghlinesp
我正在使用OpenCV HoughlinesP来查找水平和垂直线.它大部分时间都没有找到任何行.即使它找到一条线,它甚至不接近实际图像.
import cv2
import numpy as np
img = cv2.imread('image_with_edges.jpg')
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
flag,b = cv2.threshold(gray,0,255,cv2.THRESH_OTSU)
element = cv2.getStructuringElement(cv2.MORPH_CROSS,(1,1))
cv2.erode(b,element)
edges = cv2.Canny(b,10,100,apertureSize = 3)
lines = cv2.HoughLinesP(edges,1,np.pi/2,275, minLineLength = 100, maxLineGap = 200)[0].tolist()
for x1,y1,x2,y2 in lines:
for index, (x3,y3,x4,y4) in enumerate(lines):
if y1==y2 and y3==y4: # Horizontal Lines
diff = abs(y1-y3)
elif x1==x2 and x3==x4: # Vertical Lines
diff = abs(x1-x3)
else:
diff = 0
if diff < 10 and diff is not 0:
del lines[index]
gridsize …
Run Code Online (Sandbox Code Playgroud)