如何使用opencv丢弃图像的边缘?

2024-04-19 10:48:21 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在预处理一些图像,以便从我感兴趣的区域移除背景。然而,由于相机的聚焦,我的长凳上的图像有圆形的边缘。如何丢弃这些圆边,只从图像中删除我感兴趣的对象?下面的代码我可以删除图像的背景,但它不能正常工作,因为周围的边缘。在

import numpy as np
import cv2

#Read the image and perform threshold and get its height and weight
img = cv2.imread('IMD408.bmp')
h, w = img.shape[:2]

# Transform to gray colorspace and blur the image.
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray,(5,5),0)

# Make a fake rectangle arround the image that will seperate the main contour.
cv2.rectangle(blur, (0,0), (w,h), (255,255,255), 10)

# Perform Otsu threshold.
_,thresh = cv2.threshold(blur,0,255,cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU)

# Create a mask for bitwise operation
mask = np.zeros((h, w), np.uint8)

# Search for contours and iterate over contours. Make threshold for size to
# eliminate others.
contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_NONE)

for i in contours:
    cnt = cv2.contourArea(i)
    if 1000000 >cnt > 100000:
        cv2.drawContours(mask, [i],-1, 255, -1)


# Perform the bitwise operation.
res = cv2.bitwise_and(img, img, mask=mask)

# Display the result.
cv2.imwrite('IMD408.png', res)
cv2.imshow('img', res)
cv2.waitKey(0)
cv2.destroyAllWindows()

输入图像: enter image description here

退出: enter image description here

错误: enter image description here


Tags: andthe图像imageimgforthresholdnp
1条回答
网友
1楼 · 发布于 2024-04-19 10:48:21

既然你提到了所有的图像都有相同的色调,那么这对它们来说应该很好。步骤是做一些白色平衡,这将增加一点对比度。 enter image description here

去拿灰阶。 enter image description here

阈值灰度图像。小于127的值设置为255(白色)。这将为您提供一个二进制图像,它将成为原始图像的遮罩。在

enter image description here 戴上面罩

enter image description here

如果你想得到更好的结果,你可能需要反复使用阈值,这里是link。但这应该能让你开始。我使用的OpenCV版本不同于您可能需要对代码稍作调整。在

import cv2

def equaliseWhiteBalance(image):
    ''' Return equilised WB of an image '''
    wb = cv2.xphoto.createSimpleWB()                        #Create WB Object
    imgWB = wb.balanceWhite(img)                            #Balance White on image
    r,g,b = cv2.split(imgWB)                                #Get individual r,g,b channels
    r_equ  = cv2.equalizeHist(r)                            #Equalise RED channel
    g_equ  = cv2.equalizeHist(g)                            #Equalise GREEN channel
    b_equ  = cv2.equalizeHist(b)                            #Equalise BLUE channel
    img_equ_WB = cv2.merge([r_equ,g_equ,b_equ])             #Merge equalised channels
    return imgWB

#Read the image
img = cv2.imread('IMD408.bmp')
result = img.copy()

#Get whiteBalance of image
imgWB = equaliseWhiteBalance(img)

cv2.imshow('img', imgWB)
cv2.waitKey(0)

# Get gray image
gray = cv2.cvtColor(imgWB,cv2.COLOR_RGB2GRAY)
cv2.imshow('img', gray)
cv2.waitKey(0)

# Perform threshold
_, thresh = cv2.threshold(gray,127,255,cv2.THRESH_BINARY)
cv2.imshow('img', thresh)
cv2.waitKey(0)

# Apply mask
result[thresh!=0] = (255,255,255)

cv2.imshow('img', result)
cv2.waitKey(0)

如果每个图像的暗角渐晕都有不同的大小,那么我建议在二值(掩模)图像上寻找轮廓的质心。距离图像任何一个角落“短”距离的质心将是暗渐晕,因此它们的值可以从黑色更改为白色。在

相关问题 更多 >