python利用opencv去除图片logo,使用Python OpenCV删除图像的黑色标题部分

本文介绍了一种使用Python的OpenCV库去除图像中黑点的方法,通过形态学操作实现。首先,将图像转为灰度并应用Otsu二值化,接着进行形态学的Hit-or-Miss操作来检测和去除点状图案。然后,通过位运算修复因去点操作可能损坏的文本像素,并膨胀恢复文本质量。最终,将背景颜色设置为白色,文本保持黑色。
摘要由CSDN通过智能技术生成

I need to remove the blackened section in multiple parts of image using Python CV.

I tried with denoising which doesn't give satisfactory results.

Eg. I need to remove the blackened part in Table Header (below image) and convert the header background to white with contents as black.

284eb1bdf32dd1cedc21a26437986de8.png

Can anyone help me with choosing the correct library or solution to overcome this?

解决方案

Here's a modified version of @eldesgraciado's approach to filter the dotted pattern using a morphological hit or miss operation on the target pixels in Python. The difference is that instead of subtracting the mask with the binary image which decreases text quality, we dilate the binary image then bitwise-and to retain the text quality.

Obtain binary image. Load image, grayscale, Otsu's threshold

Perform morphological hit or miss operation. We create a dot pattern kernel with cv2.getStructuringElement then use cv2.filter2D to convolve the image

Remove dots. We cv2.bitwise-xor the mask with the binary image

Fix damaged text pixels. We cv2.dilate then cv2.bitwise_and the finalized mask with the input image and color background pixels white

Binary image

25ebb90c3a4a832c897828ffc2fe1d49.png

Dot mask

e38d502f29f346ed4360eba365556d32.png

Remove dots

af9d476cf073b56a03e155765f31673f.png

Dilate to fix damaged text pixels from the thresholding process

31d146ce29676aa809f6a297b954ea30.png

Result

c665cba7a65069772742e1c81c9310d0.png

Code

import cv2

import numpy as np

# Load image, grayscale, Otsu's threshold

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

gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]

# Perform morphological hit or miss operation

kernel = np.array([[-1,-1,-1], [-1,1,-1], [-1,-1,-1]])

dot_mask = cv2.filter2D(thresh, -1, kernel)

# Bitwise-xor mask with binary image to remove dots

result = cv2.bitwise_xor(thresh, dot_mask)

# Dilate to fix damaged text pixels

# since the text quality has decreased from thresholding

# then bitwise-and with input image

kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (2,2))

dilate = cv2.dilate(result, kernel, iterations=1)

result = cv2.bitwise_and(image, image, mask=dilate)

result[dilate==0] = [255,255,255]

cv2.imshow('dot_mask', dot_mask)

cv2.imshow('thresh', thresh)

cv2.imshow('result', result)

cv2.imshow('dilate', dilate)

cv2.waitKey()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值