Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/297.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 从轮廓中删除背景_Python_Opencv - Fatal编程技术网

Python 从轮廓中删除背景

Python 从轮廓中删除背景,python,opencv,Python,Opencv,我在删除不需要的轮廓时遇到一些问题 检测到轮廓的图像: 我不希望此图像中显示以下轮廓(以蓝色标记的区域): 但我似乎无法摆脱它们。 我的代码: img = cv2.imread(img_path) edges = cv2.Canny(img, 240, 240) #cv2.imshow('', edges) thresh = cv2.threshold(edges,150, 255,cv2.THRESH_BINARY_INV)[1] cnts, h = cv2.findContours

我在删除不需要的轮廓时遇到一些问题

检测到轮廓的图像:

我不希望此图像中显示以下轮廓(以蓝色标记的区域):

但我似乎无法摆脱它们。 我的代码:

img = cv2.imread(img_path)

edges = cv2.Canny(img, 240, 240)
#cv2.imshow('', edges)


thresh = cv2.threshold(edges,150, 255,cv2.THRESH_BINARY_INV)[1]
cnts, h = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

# Show and Write Threshold Image
#cv2.imshow('thresh', thresh)
#cv2.imwrite('Thresholded_labeled_image.jpg', thresh)

# Find and Draw Contours
contours, h = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
img_contours = cv2.drawContours(thresh, contours, -1, (0,255,0), 3)
cv2.imshow('contours', img_contours)

# Remove Noise
kernel = np.ones((5,5),np.float32)/25
dst = cv2.filter2D(img_contours,-1,kernel)

plt.subplot(121),plt.imshow(img_contours),plt.title('Image_Contours')
plt.xticks([]), plt.yticks([])
plt.subplot(122),plt.imshow(dst),plt.title('Averaging')
plt.xticks([]), plt.yticks([])
plt.show()
我试过:

kernel = np.ones((5,5),np.uint8)
closing = cv2.morphologyEx(edges, cv2.MORPH_CLOSE, kernel)
cv2.imshow('closing', closing)
我试着改变果仁的大小,但仍然不起作用。我仍然看到那些不需要的轮廓

有什么我能做的吗

编辑1:使用boundingRect

kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (4,2))
dilate = cv2.dilate(thresh, kernel, iterations=2)

# Find contours, highlight text areas, and extract ROIs
cnts = cv2.findContours(dilate, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]

ROI_number = 0
ROI_images = []
for c in cnts:
    area = cv2.contourArea(c)
    print("Area is: ", area)
    x,y,w,h = cv2.boundingRect(c)
    print("Height: ", h)
    if area > 100 and 0<h<300:
        cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 3)
        ROI = img[y:y+h, x:x+w]
        # cv2.imwrite('ROI_{}.png'.format(ROI_number), ROI)
        ROI_number += 1
        ROI_images.append(ROI)
kernel=cv2.getStructuringElement(cv2.MORPH_RECT,(4,2))
扩张=cv2。扩张(阈值、内核、迭代次数=2)
#查找轮廓,高亮显示文本区域,并提取ROI
cnts=cv2.找到的孔(扩张,cv2.外部翻新,cv2.链近似简单)
如果len(cnts)==2个其他cnts[1],则cnts=cnts[0]
投资回报率=0
ROI_图像=[]
对于碳纳米管中的碳:
面积=cv2。轮廓面积(c)
打印(“面积为:”,面积)
x、 y,w,h=cv2.boundingRect(c)
打印(“高度:,h)

如果面积>100且0这就是您的工作方式。您必须根据您的要求编辑以下代码

import cv2
import numpy as np
img = cv2.imread('try.png')
img_res = img.copy()
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ret,thresh = cv2.threshold(gray,127,255,1)
contours,h = cv2.findContours(thresh,1,2)
# area_list = []
for cnt in contours:
        area = cv2.contourArea(cnt)
        x,y,w,h = cv2.boundingRect(cnt)
#         print(area,w,h)
        if area<5200:

            img[y:y+h, x:x+w] = (255,255,255)
            cv2.rectangle(img_res, (x, y), (x + w, y + h), (0, 255, 0), 3)
导入cv2
将numpy作为np导入
img=cv2.imread('try.png')
img_res=img.copy()
灰色=cv2.CVT颜色(img,cv2.COLOR\U BGR2GRAY)
ret,thresh=cv2。阈值(灰色,127255,1)
等高线,h=cv2。找到的等高线(阈值,1,2)
#区域列表=[]
对于轮廓中的cnt:
面积=cv2。轮廓面积(cnt)
x、 y,w,h=cv2.boundingRect(cnt)
#打印(面积,宽,高)

如果是面积,则尝试按长度和面积属性过滤轮廓。对不起,你是什么意思?我以前没有这样做过。是的,您也可以使用
cv2获得
x,y,w,h
。boundingrect()
只需创建您自己的阈值条件。这就是您的筛选方式:我投票给boundinrect而不是轮廓长度这很完美。它起作用了,因此我接受了答案,但是,您能解释一下为什么在findContours()函数中使用参数1、2吗?cv.findContours()函数中有三个参数,第一个是源图像,第二个是轮廓检索模式,第三个是轮廓近似方法。看这个:好的,很好。我总是使用(cv.RETR_TREE,cv.CHAIN_APPROX_SIMPLE)这两个参数。很高兴知道。谢谢你的帮助!