【opencv】选择区域进行滤波处理

手动选择感兴趣的区域,获取坐标

image_path = 'result2.jpg'
img_result1 = cv2.imread(image_path)
# 弹出一个框 让你选择ROI | x,y是左上角的坐标
x1,y1,w1,h1 = cv2.selectROI(img_result1)
print(x1, y1, w1, h1)
cv2.destroyAllWindows()

对感兴趣的区域进行滤波处理

双边滤波处理

# 双边滤波
def do_shuangbian_img(x,y,w,h,img,d,sigmaColor,sigmaSpace):
    roi = img[y:y+h,x:x+w]
    # roi = cv2.blur(roi,(3,3))
    roi = cv2.bilateralFilter(roi,d,sigmaColor,sigmaSpace)
    img[y:y+h,x:x+w] = roi

    cv2.imshow('result',roi)
    cv2.imshow('result1',img)
    cv2.imwrite('result2.jpg', img)

    cv2.waitKey(0)
    cv2.destroyAllWindows()
do_shuangbian_img(x1,y1,w1,h1,img_result1,5,20,20)

中值滤波,高斯滤波,均值滤波处理

def do_medianBlur_img(x,y,w,h,img,ksize):
    roi = img[y:y+h,x:x+w]
    # 均值滤波
    # roi = cv2.blur(roi,(3,3))
    # 高斯滤波
    # roi = cv2.GaussianBlur(roi,(3,3),0)
		# 中值滤波
    roi = cv2.medianBlur(roi,ksize)
    img[y:y+h,x:x+w] = roi

    cv2.imshow('result',roi)
    cv2.imshow('result1',img)
    cv2.imwrite('result2.jpg', img)

    cv2.waitKey(0)
    cv2.destroyAllWindows()
do_medianBlur_img(x1,y1,w1,h1,img_result1,5)

你可能感兴趣的:(opencv,opencv,计算机视觉,人工智能)