检测颜色并从图像中删除该颜色 [英] Detect color and remove that color from image
问题描述
我的图像背景为浅紫色,深蓝色为字符。我的目标是从图像中识别文本。因此,我尝试从背景中去除浅紫色,以使我的图像没有噪点,但是我找不到该图像的确切颜色代码,因为它在各个地方都有些不同,因此无法屏蔽图片。这是我的代码
I have image with kind of light purple image in background and character in dark blue. My goal is to identify text from the image. So I'm trying to remove light purple color from background so that my image will be free of noise, but I can't find the exact color code for that image as it is somewhat different everywhere, so I'm not able to mask image. Here's my code
import numpy as np
from PIL import Image
im = Image.open('capture.png')
im = im.convert('RGBA')
data = np.array(im)
rgb = data[:,:,:3]
color = [27, 49, 89] # Original value to be mask
black = [0,0,0, 255]
white = [255,255,255,255]
mask = np.all(rgb == color, axis = -1)
data[mask] = black
data[np.logical_not(mask)] = white
new_im = Image.fromarray(data)
new_im.save('new_file.png')
所以我想,如果我可以去除所有特定颜色范围内的颜色,例如[R:0-20,G:0-20,B:80-100],那也许会有用。有人可以告诉我我该怎么做。
So I thought if I can remove color in all particular color range like [R:0-20, G:0-20, B:80-100] maybe that'll will work. Can someone tell me how can i do that.
解决该问题的任何其他建议也将受到赞赏。
Any other suggestion to solve this problem will also be appreciated.
推荐答案
由于文本和背景似乎有明显的阴影,因此应在此处使用颜色阈值。想法是将图像转换为HSV格式,然后使用上下阈值生成二进制分段掩码,然后按位生成文本并提取文本。这是一个使用Python OpenCV的实现
Since there seems to be a distinguishable shade from the text and the background, color thresholding should work here. The idea is to convert the image to HSV format then use a lower and upper threshold to generate a binary segmented mask then bitwise-and to extract the text. Here's an implementation using Python OpenCV
使用此下限和上限,我们获得此掩码
Using this lower and upper threshold, we obtain this mask
lower = np.array([0, 120, 0])
upper = np.array([179, 255, 255])
然后按位排列-并带有原始图像
Then we bitwise-and with the original image
最后我们阈值获得二进制图像,其中前景文本为黑色,背景为白色
Finally we threshold to get a binary image with the foreground text in black and the background in white
import numpy as np
import cv2
# Color threshold
image = cv2.imread('1.png')
original = image.copy()
hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
lower = np.array([0, 120, 0])
upper = np.array([179, 255, 255])
mask = cv2.inRange(hsv, lower, upper)
result = cv2.bitwise_and(original,original,mask=mask)
result[mask==0] = (255,255,255)
# Make text black and foreground white
result = cv2.cvtColor(result, cv2.COLOR_BGR2GRAY)
result = cv2.threshold(result, 0, 255, cv2.THRESH_OTSU + cv2.THRESH_BINARY)[1]
cv2.imshow('mask', mask)
cv2.imshow('result', result)
cv2.waitKey()
您可以使用此HSV颜色阈值脚本来确定上下阈值
You can use this HSV color threshold script to determine the lower and upper thresholds
import cv2
import sys
import numpy as np
def nothing(x):
pass
# Load in image
image = cv2.imread('1.png')
# Create a window
cv2.namedWindow('image')
# create trackbars for color change
cv2.createTrackbar('HMin','image',0,179,nothing) # Hue is from 0-179 for Opencv
cv2.createTrackbar('SMin','image',0,255,nothing)
cv2.createTrackbar('VMin','image',0,255,nothing)
cv2.createTrackbar('HMax','image',0,179,nothing)
cv2.createTrackbar('SMax','image',0,255,nothing)
cv2.createTrackbar('VMax','image',0,255,nothing)
# Set default value for MAX HSV trackbars.
cv2.setTrackbarPos('HMax', 'image', 179)
cv2.setTrackbarPos('SMax', 'image', 255)
cv2.setTrackbarPos('VMax', 'image', 255)
# Initialize to check if HSV min/max value changes
hMin = sMin = vMin = hMax = sMax = vMax = 0
phMin = psMin = pvMin = phMax = psMax = pvMax = 0
output = image
wait_time = 33
while(1):
# get current positions of all trackbars
hMin = cv2.getTrackbarPos('HMin','image')
sMin = cv2.getTrackbarPos('SMin','image')
vMin = cv2.getTrackbarPos('VMin','image')
hMax = cv2.getTrackbarPos('HMax','image')
sMax = cv2.getTrackbarPos('SMax','image')
vMax = cv2.getTrackbarPos('VMax','image')
# Set minimum and max HSV values to display
lower = np.array([hMin, sMin, vMin])
upper = np.array([hMax, sMax, vMax])
# Create HSV Image and threshold into a range.
hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
mask = cv2.inRange(hsv, lower, upper)
output = cv2.bitwise_and(image,image, mask= mask)
# Print if there is a change in HSV value
if( (phMin != hMin) | (psMin != sMin) | (pvMin != vMin) | (phMax != hMax) | (psMax != sMax) | (pvMax != vMax) ):
print("(hMin = %d , sMin = %d, vMin = %d), (hMax = %d , sMax = %d, vMax = %d)" % (hMin , sMin , vMin, hMax, sMax , vMax))
phMin = hMin
psMin = sMin
pvMin = vMin
phMax = hMax
psMax = sMax
pvMax = vMax
# Display output image
cv2.imshow('image',output)
# Wait longer to prevent freeze for videos.
if cv2.waitKey(wait_time) & 0xFF == ord('q'):
break
cv2.destroyAllWindows()
这篇关于检测颜色并从图像中删除该颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!