如何提取这个等距视图中的粗线

huangapple go评论67阅读模式
英文:

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(&#39;schematic.jpg&#39;)
    
    # 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(&#39;schematic_thick_lines.jpg&#39;, result)
    
    # show result
    cv2.imshow(&#39;morph&#39;, morph)
    cv2.imshow(&#39;result&#39;, 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>



huangapple
  • 本文由 发表于 2023年5月11日 02:02:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/76221395.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定