findContours python
时间: 2023-10-14 18:02:28 浏览: 56
Python通过OpenCV的findContours获取轮廓并切割实例
5星 · 资源好评率100%
Sure! Here's an example of how you can use the `findContours` function in Python using OpenCV library:
```python
import cv2
# Read image
image = cv2.imread("image.jpg")
# Convert image to grayscale
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# Apply thresholding if needed
_, threshold = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)
# Find contours
contours, _ = cv2.findContours(threshold, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
# Draw contours on the original image
cv2.drawContours(image, contours, -1, (0, 255, 0), 2)
# Display the image with contours
cv2.imshow("Contours", image)
cv2.waitKey(0)
cv2.destroyAllWindows()
```
In this example, we first read an image and convert it to grayscale. Then we apply thresholding to create a binary image. Next, we use the `findContours` function to detect contours in the binary image. Finally, we draw the contours on the original image using the `drawContours` function and display it.
Make sure you have OpenCV installed (`pip install opencv-python`) before running this code. Also, replace "image.jpg" with the path to your own image.
阅读全文