简体   繁体   English

使用OpenCV Python查找字母并将其着色为红色

[英]Find Alphabet and Color it to Red with OpenCV Python

I have a large image with some alphabets in it and cut out of one alphabet ("A"). 我有一个大图像,里面有一些字母和一个字母(“A”)。 I need to find each A in the larger image and color it to red. 我需要在较大的图像中找到每个A并将其颜色变为红色。

Large Image: 大图: 大图

Alphabet A: 字母A: 字母A.

To solve the problem, I have used the following codes- 为了解决这个问题,我使用了以下代码 -

import cv2, numpy as np

# read the image and convert into binary
a = cv2.imread('search.png', 0) 
ret,binary_image = cv2.threshold(a,230,255,cv2.THRESH_BINARY_INV)

# create the Structuring element
letter_a = cv2.imread('A.png', 0)
ret,se = cv2.threshold(letter_a,230,255,cv2.THRESH_BINARY_INV)

#erosion and dilation for finding A
erosion = cv2.erode(binary_image , se) 
new_se = cv2.flip(se,0)
dilation = cv2.dilate(erosion, new_se) 
cv2.imwrite('dilation.jpg', dilation )

In this point, I get the following image 在这一点上,我得到以下图像 扩张图像

As you can see, I am clearly identifying all the A. However, I need to color those A to red and most importantly, write on the first large image with black letter and white background. 正如你所看到的,我清楚地识别出所有的A.但是,我需要将A染成红色,最重要的是,用黑色字母和白色背景写在第一个大图像上。 Is there any way to do that? 有没有办法做到这一点? Maybe using numpy array write on the first image? 也许在第一张图片上使用numpy数组写?

You can solve this as follows. 您可以按如下方式解决此问题。
First off, to color the letters red in the main image, it is best to load it in color. 首先,要在主图像中为红色字母着色,最好将其加载为彩色。 A grayscale copy is created to perform the threshold. 创建灰度副本以执行阈值。
Then a black image with the dimensions of the main image is create and the color of this image is set to red. 然后创建具有主图像尺寸的黑色图像,并将该图像的颜色设置为红色。 The image with the A's is used as a mask to get an image of red A's. 具有A的图像用作掩模以获得红色A的图像。 These red A's are then added to the main image.* 然后将这些红色A添加到主图像中。*

Result: 结果:

在此输入图像描述

Code: 码:

import cv2, numpy as np

# load the image in color
a = cv2.imread('search.png') 
# create grayscale
a_gray = cv2.cvtColor(a,cv2.COLOR_BGR2GRAY)
ret,binary_image = cv2.threshold(a_gray,230,255,cv2.THRESH_BINARY_INV)

# create the Structuring element
letter_a = cv2.imread('A.png', 0)
ret,se = cv2.threshold(letter_a,230,255,cv2.THRESH_BINARY_INV)

#erosion and dilation for finding A
erosion = cv2.erode(binary_image , se) 
new_se = cv2.flip(se,0)
dilation = cv2.dilate(erosion, new_se) 

# create a red background image
red = np.zeros((a.shape[:3]),dtype=a.dtype)
red[:] = (0,0,255)
# apply the mask with A's to get red A's
red_a = cv2.bitwise_and(red,red,mask=dilation)

# Add the A's to the main image
result = cv2.add(a,red_a)

cv2.imshow('Result', result )
cv2.waitKey(0)
cv2.destroyAllWindows()

*If the letters are not black an extra step is needed, read this tutorial . *如果字母不是黑色,则需要额外的步骤,请阅读本教程 But for your image this is not necessary. 但对于你的形象,这不是必要的。

I used the following codes to solve the problem- 我使用以下代码来解决问题 -

import cv2, numpy as np
# read the image and convert into binary
color_image = cv2.imread(r'search.png', 1) 
gray_image = cv2.imread(r'search.png', 0) 
ret,binary_image = cv2.threshold(gray_image,230,255,cv2.THRESH_BINARY_INV)

# create the Structuring element
letter_a = cv2.imread('A.png', 0)
ret,se = cv2.threshold(letter_a,230,255,cv2.THRESH_BINARY_INV)

#erosion and dilation for finding A
erosion = cv2.erode(binary_image, se) 
new_se = cv2.flip(se,0)
dilation = cv2.dilate(erosion, new_se) 

for i in zip(*np.where(dilation == 255)):
    color_image[i[0], i[1], 0] = 0
    color_image[i[0], i[1], 1] = 0
    color_image[i[0], i[1], 2] = 255

# show and save image
cv2.imwrite('all_a.jpg', color_image)
cv2.imshow('All A',color_image)
cv2.waitKey(0)
cv2.destroyAllWindows()

结果

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM