英文:
How can I extract the thick line in this isometric view
问题
我有一个元素的二维绘图(等距视图),如下所示:
[![点击查看图像描述](https://i.stack.imgur.com/O6zRi.jpg)](https://i.stack.imgur.com/O6zRi.jpg)
我想从中提取只有粗的、最长的黑线(带有嵌入箭头的元素,不包括尺寸)。
如何实现?
目前我的代码如下
```python
import cv2
import numpy as np
inputImage = cv2.imread("iso.jpg")
inputImageGray = cv2.cvtColor(inputImage, cv2.COLOR_BGR2GRAY)
edges = cv2.Canny(inputImageGray, 150, 200, apertureSize=3)
minLineLength = 30
maxLineGap = 5
lines = cv2.HoughLinesP(
image=edges,
rho=cv2.HOUGH_PROBABILISTIC,
theta=np.pi / 180,
threshold=30,
minLineLength=minLineLength,
maxLineGap=maxLineGap,
)
for x in range(0, len(lines)):
for x1, y1, x2, y2 in lines[x]:
pts = np.array([[x1, y1], [x2, y2]], np.int32)
cv2.polylines(inputImage, [pts], True, (0, 255, 0))
cv2.imshow("Result", inputImage)
cv2.imshow("Edges", edges)
cv2.waitKey(0)
这会得到以下结果 - 它检测到所有线条,包括细线、文本括号等。
<details>
<summary>英文:</summary>
I have a 2d drawing of a element (isometric view) as follow:
[![enter image description here](https://i.stack.imgur.com/O6zRi.jpg)](https://i.stack.imgur.com/O6zRi.jpg)
I would like to extract only the thick, longest black line (element with arrows embedded in it not dimensions) from it.
How to achieve that ?
For now my code goes as follow
import cv2
import numpy as np
inputImage = cv2.imread("iso.jpg")
inputImageGray = cv2.cvtColor(inputImage, cv2.COLOR_BGR2GRAY)
edges = cv2.Canny(inputImageGray, 150, 200, apertureSize=3)
minLineLength = 30
maxLineGap = 5
lines = cv2.HoughLinesP(
image=edges,
rho=cv2.HOUGH_PROBABILISTIC,
theta=np.pi / 180,
threshold=30,
minLineLength=minLineLength,
maxLineGap=maxLineGap,
)
for x in range(0, len(lines)):
for x1, y1, x2, y2 in lines[x]:
pts = np.array([[x1, y1], [x2, y2]], np.int32)
cv2.polylines(inputImage, [pts], True, (0, 255, 0))
cv2.imshow("Result", inputImage)
cv2.imshow("Edges", edges)
cv2.waitKey(0)
Which gives fallowing result - it detect all lines also thin ones, text brackets etc.
[![enter image description here](https://i.stack.imgur.com/TxIXK.png)](https://i.stack.imgur.com/TxIXK.png)
</details>
# 答案1
**得分**: 1
对不起,我只会翻译文本内容,不包括代码和图片。如果您有任何文本需要翻译,请提供文本,我将尽力帮助您翻译。
<details>
<summary>英文:</summary>
Sorry, I posted this under a misunderstanding. This is not what the OP wanted. The post was not clear. He apparently wants the medium thick lines with the arrows (from his subsequent comment).
Nevertheless, perhaps this will help someone else on a different project. So here is one way to get the thickest black lines using Python/OpenCV.
- Read the input
- Convert to grayscale
- Apply morphology close such that the kernel is thicker than the thin lines and thinner than the thick lines
- Threshold
- Save the results
Input:
[![enter image description here][1]][1]
import cv2
import numpy as np
# read the input
img = cv2.imread('schematic.jpg')
# convert to grayscale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# apply morphology close
kernel = cv2.getStructuringElement(cv2.MORPH_RECT , (3,3))
morph = cv2.morphologyEx(gray, cv2.MORPH_DILATE, kernel)
# threshold
result = cv2.threshold(gray, 32, 255, cv2.THRESH_BINARY)[1]
# save results
cv2.imwrite('schematic_thick_lines.jpg', result)
# show result
cv2.imshow('morph', morph)
cv2.imshow('result', result)
cv2.waitKey(0)
Results:
[![enter image description here][2]][2]
You can use contours to extract each one separately if that is needed.
[1]: https://i.stack.imgur.com/W2Mjv.jpg
[2]: https://i.stack.imgur.com/lADlv.jpg
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论