如何检测图像中的单独图形? [英] How to detect separate figures in an image?
问题描述
我有一个类似于以下的图像。我想将两个数字 7
和 4
分开,因为我想要一个边框
如何我可以用OpenCV做到吗?我不知道如何执行此操作,并在考虑是否可以通过使用Sobel运算符进行某些操作。
s = cv2.Sobel(img,cv2.CV_64F,1,0 ,ksize = 5)
,但不知道如何从此处继续。
细分并检测图像中的图形,主要思想如下:
- 使用
检测到的轮廓
裁剪并保存的投资回报率
输出
检测到的轮廓:2
代码
将numpy导入为np
import cv2
from imutils导入轮廓
#加载图像,灰度,高斯模糊,Canny边缘检测
image = cv2.imread( 1.png)
original = image.copy()
grey = cv2。 cvtColor(图像,cv2.COLOR_BGR2GRAY)
模糊= cv2.GaussianBlur(灰色,(3,3),0)
canny = cv2.Canny(模糊,120,255,1)
#查找轮廓
outline_list = []
ROI_number = 0
cnts = cv2.findContours(canny,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts [ 0]如果len(cnts)== 2 else cnts [1]
cnts,_ = outlines.sort_contours(cnts,method = left-to-right)
for c in cnts:
#获取每个轮廓的边界矩形
x,y,w,h = cv2.boundingRect(c)
#查找轮廓的ROI
roi = image [y: y + h,x:x + w]
#绘制边界框矩形,使用Numpy切片
裁剪cv2.rectangle(image,(x,y),(x + w,y + h),(0,255,0),3)
ROI =原始[y:y + h, x:x + w]
cv2.imwrite('ROI _ {}。png'.format(ROI_number),ROI)
outline_list.append(c)
ROI_number + = 1
print('检测到轮廓:{}'。format(len(contour_list)))
cv2.imshow( image,image)
cv2.imshow( canny,canny )
cv2.waitKey()
I have an image similar to the following. I want to separate two numbers
7
and4
as shown in the image, in that I want to have a bounding box for each of these two objects.How could I do this with OpenCV? I have no idea, how could I do this and was thinking if there is some way by using Sobel operator. The only thing that I tired was getting the Sobel.
s = cv2.Sobel(img, cv2.CV_64F,1,0,ksize=5)
but have no idea on how to proceed from here.
解决方案To segment and detect figures in an image, the main idea is as follows:
- Convert image into grayscale using
cv2.cvtColor
- Blur image with
cv2.GaussianBlur
- Find edges with
cv2.Canny
- Find contours with
cv2.findContours
and sort from left-to-right usingimutils.contours.sort_contours()
to ensure that when we iterate through contours, they are in the correct order - Iterate through each contour
- Obtain bounding rectangle using
cv2.boundingRect
- Find ROI of each contour with Numpy slicing
- Draw bounding box rectangle using
cv2.rectangle
- Obtain bounding rectangle using
Canny Edge Detection
Detected Contours
Cropped and saved ROIs
Output
Contours Detected: 2
Code
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()
这篇关于如何检测图像中的单独图形?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
- Convert image into grayscale using