微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

OpenCV:如何删除图像中不需要的部分

如何解决OpenCV:如何删除图像中不需要的部分

我正在尝试获取图像中蓝色区域的轮廓,然后计算长度和面积,如图所示(我有许多具有相同分辨率但蓝色区域大小不同的相似图像)。

enter image description here

这是我使用的代码

import cv2
import numpy as np

# read image as grayscale
img = cv2.imread('VF2.jpg',cv2.IMREAD_GRAYSCALE)

# threshold to binary
thresh = cv2.threshold(img,210,255,cv2.THRESH_BINARY)[1]  # the 2nd parameter should be changed.

# apply morphology
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(5,5))
morph = cv2.morphologyEx(thresh,cv2.MORPH_OPEN,kernel)

# find contours - write black over all small contours
letter = morph.copy()
cntrs = cv2.findContours(morph,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)  
# cntrs = cv2.findContours(morph,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)  

cntrs = cntrs[0] if len(cntrs) == 2 else cntrs[1]
# cntrs = cntrs[0]

for c in cntrs:
    area = cv2.contourArea(c)
    print(area)
    if area < 100:
        cv2.drawContours(letter,[c],(0,0),-1)

# do canny edge detection
edges = cv2.Canny(letter,200,200) # the result for edges is good.
length = cv2.arcLength(cntrs[0],False)  # not closed curves
print('length = ',length)  # both length and area need calibration


area = cv2.contourArea(cntrs[0])
print('area = ',area)

# Outputs
print(np.squeeze(cntrs[0]),'\n')                    # Contour
print('Contour points:',cntrs[0].shape[0],'\n')
print('arcLength:',cv2.arcLength(cntrs[0],True))  # closed curves

# write results
# cv2.imwrite("K_thresh.png",thresh)    

# show results
# cv2.imshow("K_thresh",thresh)
# cv2.imshow("K_morph",morph)
cv2.imshow("K_letter",letter)
cv2.imshow("K_edges",edges)
cv2.waitKey(0)
cv2.destroyAllWindows()

我使用了上面的代码并获得了轮廓,但还有一些额外的部分,如下图所示。任何人都可以帮助删除附加部分并使轮廓闭合吗?非常感谢。

enter image description here

解决方法

将内核的大小更改为 (4,4) 并在此处执行腐蚀而不是开放:

import cv2
import numpy as np

img = cv2.imread("images/flower.jpg",cv2.IMREAD_GRAYSCALE)
scale_percent = 60 # percent of original size
width = int(img.shape[1] * scale_percent / 100)
height = int(img.shape[0] * scale_percent / 100)
dim = (width,height)
  
# resize image
resized = cv2.resize(img,dim,interpolation = cv2.INTER_AREA)

# threshold to binary
thresh = cv2.threshold(resized,210,255,cv2.THRESH_BINARY_INV)[1]  # the 2nd parameter should be changed.

# apply morphology
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(4,4))
morph = cv2.morphologyEx(thresh,cv2.MORPH_ERODE,kernel,1)

letter = morph.copy()
cntrs = cv2.findContours(morph,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)  
# cntrs = cv2.findContours(morph,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)  

cntrs = cntrs[0] if len(cntrs) == 2 else cntrs[1]
# cntrs = cntrs[0]

for c in cntrs:
    area = cv2.contourArea(c)
    print(area)
    if area < 100:
        cv2.drawContours(letter,[c],(0,0),-1)


# do canny edge detection
edges = cv2.Canny(letter,200,200) # the result for edges is good.
length = cv2.arcLength(cntrs[0],False)  # not closed curves
print('length = ',length)  # both length and area need calibration

area = cv2.contourArea(cntrs[0])
print('area = ',area)

# Outputs
print(np.squeeze(cntrs[0]),'\n')                    # Contour
print('Contour points:',cntrs[0].shape[0],'\n')
print('arcLength:',cv2.arcLength(cntrs[0],True))  # closed curves

# write results
# cv2.imwrite("K_thresh.png",thresh)    

# show results
# cv2.imshow("K_thresh",thresh)
# cv2.imshow("K_morph",morph)
cv2.imshow("K_letter",letter)
cv2.imshow("K_edges",edges)
cv2.waitKey(0)
cv2.destroyAllWindows()

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。