如何消除渐变背景噪音? [英] How to remove gradient background noise?

查看:111
本文介绍了如何消除渐变背景噪音?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从所拥有的图像中消除渐变背景噪声.我用cv2尝试了很多方法,但都没有成功.

I'm trying to remove gradient background noise from the images I have. I've tried many ways with cv2 without success.

首先将图像转换为灰度图像,使其失去一定的梯度,这可能有助于查找轮廓.

Converting the image to grayscale at first to make it lose some gradients that may help to find the contours.

有人知道应对这种背景的方法吗?我什至尝试从各个角落取样并应用某种内核过滤器.

Does anybody know of a way to deal with this kind of background? I even tried taking a sample from the corners and applying some kind of kernel filter.

推荐答案

删除渐变的一种方法是使用 .

One way to remove gradients is to use cv2.medianBlur() to smooth out the image by taking the median of all pixels under a kernel. Then to extract the letters, you can perform cv2.adaptiveThreshold().

模糊消除了大多数梯度噪声.您可以更改内核大小以删除更多内容,但同时也会删除字母的详细信息

The blur removes most of the gradient noise. You can change the kernel size to remove more but it will also remove the details of the letters

自适应阈值图像以提取字符.从您的原始图像来看,似乎在字母cxz上添加了梯度杂色,以使其融合到背景中.

Adaptive threshold the image to extract characters. From your original image, it seems like gradient noise was added onto the the letters c, x, and z to make it blend into the background.

接下来,我们可以执行cv2.Canny()来检测边缘并获得

Next we can perform cv2.Canny() to detect edges and obtain this

然后我们可以使用cv2.morphologyEx()进行形态学打开,以清除小噪声并增强细节

Then we can do morphological opening using cv2.morphologyEx() to clean up the small noise and enhance details

现在,我们使用cv2.dilate()进行扩张以获取单个轮廓

Now we dilate using cv2.dilate() to obtain a single contour

在这里,我们使用cv2.findContours()查找轮廓.我们遍历每个轮廓并使用cv2.contourArea()进行过滤,并使用最小和最大面积来获得边界框.根据您的图像,您可能需要调整最小/最大区域滤镜.这是结果

From here, we find contours using cv2.findContours(). We iterate through each contour and filter using cv2.contourArea() with a minimum and maximum area to obtain bounding boxes. Depending on your image, you may have to adjust the min/max area filter. Here's the result

import cv2
import numpy as np

image = cv2.imread('1.png')

blur = cv2.medianBlur(image, 7)
gray = cv2.cvtColor(blur, cv2.COLOR_BGR2GRAY)
thresh = cv2.adaptiveThreshold(gray,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY,11,3)

canny = cv2.Canny(thresh, 120, 255, 1)
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5,5))
opening = cv2.morphologyEx(canny, cv2.MORPH_CLOSE, kernel)
dilate = cv2.dilate(opening, kernel, iterations=2)

cnts = cv2.findContours(dilate, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]

min_area = 500
max_area = 7000
for c in cnts:
    area = cv2.contourArea(c)
    if area > min_area and area < max_area:
        x,y,w,h = cv2.boundingRect(c)
        cv2.rectangle(image, (x, y), (x + w, y + h), (36,255,12), 2)

cv2.imshow('blur', blur)
cv2.imshow('thresh', thresh)
cv2.imshow('canny', canny)
cv2.imshow('opening', opening)
cv2.imshow('dilate', dilate)
cv2.imshow('image', image)
cv2.waitKey(0)

这篇关于如何消除渐变背景噪音?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆