Ama*_*nda 2 python opencv image image-processing computer-vision
我有一个类似于以下的图像。我要分开两个数字7并4显示的图像中,因为我想为每一个这两个对象的边界框。
我怎么能用 OpenCV 做到这一点?我不知道,我怎么能做到这一点,并在想是否有某种方法可以使用 Sobel 运算符。我唯一累的就是买索贝尔。
s = cv2.Sobel(img, cv2.CV_64F,1,0,ksize=5)
Run Code Online (Sandbox Code Playgroud)
但不知道如何从这里开始。
对图像中的图形进行分割和检测,主要思想如下:
cv2.cvtColorcv2.GaussianBlurcv2.Cannycv2.findContours并从左到右排序 using
imutils.contours.sort_contours()以确保当我们遍历轮廓时,它们的顺序是正确的cv2.boundingRectcv2.rectangleCanny 边缘检测
检测到的轮廓
裁剪和保存的投资回报率
输出
Contours Detected: 2
Run Code Online (Sandbox Code Playgroud)
代码
import numpy as np
import cv2
from imutils import contours
# Load image, grayscale, Gaussian blur, Canny edge detection
image = cv2.imread("1.png")
original = image.copy()
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blurred = cv2.GaussianBlur(gray, (3,3), 0)
canny = cv2.Canny(blurred, 120, 255, 1)
# Find contours
contour_list = []
ROI_number = 0
cnts = cv2.findContours(canny, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
cnts, _ = contours.sort_contours(cnts, method="left-to-right")
for c in cnts:
# Obtain bounding rectangle for each contour
x,y,w,h = cv2.boundingRect(c)
# Find ROI of the contour
roi = image[y:y+h, x:x+w]
# Draw bounding box rectangle, crop using Numpy slicing
cv2.rectangle(image,(x,y),(x+w,y+h),(0,255,0),3)
ROI = original[y:y+h, x:x+w]
cv2.imwrite('ROI_{}.png'.format(ROI_number), ROI)
contour_list.append(c)
ROI_number += 1
print('Contours Detected: {}'.format(len(contour_list)))
cv2.imshow("image", image)
cv2.imshow("canny", canny)
cv2.waitKey()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
748 次 |
| 最近记录: |