python图片中的蓝色或者红色的文字识别

python识别图片中的蓝色或者红色的文字

一 、 红色字体识别代码

import cv2
import numpy as np
from PIL import Image
 
import ddddocr
 
# 读取图像---要识别的图像
img = cv2.imread('G:/tupian/01.png')
 
# 将图像转换为HSV颜色空间
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
 
# 定义红色在HSV颜色空间中的取值范围,以及二值化阈值
lower_red = np.array([0, 50, 50])
upper_red = np.array([10, 255, 255])
mask1 = cv2.inRange(hsv, lower_red, upper_red)
 
lower_red = np.array([170, 50, 50])
upper_red = np.array([180, 255, 255])
mask2 = cv2.inRange(hsv, lower_red, upper_red)
 
# 结合两个面具
mask = cv2.bitwise_or(mask1, mask2)
 
# 将二值化结果应用于原始图像,并将红色像素设置为黑色,将非红色像素设置为白色或透明
result = np.zeros_like(img)
result[mask == 255] = [0, 0, 0]  # 将红色像素设置为黑色
result[mask != 255] = [255, 255, 255]  # 将非红色像素设置为白色或透明
 
# 保存结果图像
cv2.imwrite('G:/tupian/03.png', result)
 
# # 显示结果图像
# cv2.imshow('Result', result)
# cv2.waitKey(0)
# cv2.destroyAllWindows()
 
# 将黑白图片通过ddddocr识别出来
ocr = ddddocr.DdddOcr()
with open('G:/tupian/03.png', 'rb') as f:
    img_bytes = f.read()
res = ocr.classification(img_bytes)
print(res)
 

二 、 蓝色字体识别

# 读取图像
import cv2
import numpy as np
import ddddocr


# 读取图像
img = cv2.imread('D:/Verification/1.png')

# 将图像转换为HSV颜色空间
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)

# 定义蓝色在HSV颜色空间中的取值范围,以及二值化阈值
lower_blue = np.array([100, 50, 50])
upper_blue = np.array([130, 255, 255])
mask = cv2.inRange(hsv, lower_blue, upper_blue)

# 将二值化结果应用于原始图像,并将蓝色像素设置为黑色,将非蓝色像素设置为白色或透明
result = np.zeros_like(img)
result[mask == 255] = [0, 0, 0]  # 将蓝色像素设置为黑色
result[mask != 255] = [255, 255, 255]  # 将非蓝色像素设置为白色或透明

# 保存结果图像
cv2.imwrite('D:/Verification/result.jpg', result)

# # 显示结果图像
# cv2.imshow('Result', result)
# cv2.waitKey(0)
# cv2.destroyAllWindows()

# 将黑白图片通过ddddocr识别出来
ocr = ddddocr.DdddOcr()
with open('G:/tupian/03.png', 'rb') as f:
    img_bytes = f.read()
res = ocr.classification(img_bytes)
print(res)

在这里插入图片描述

以下是一个示例代码,可以实现从200张图片中随机选出20张图片识别图片中的虚拟人物,并统计其中红色系、蓝色系上衣的人物数量: ```python import random import cv2 import numpy as np # 定义颜色阈值 lower_red = np.array([0, 0, 100]) upper_red = np.array([100, 100, 255]) lower_blue = np.array([100, 0, 0]) upper_blue = np.array([255, 100, 100]) # 从200张图片中随机选取20张 image_paths = ["path/to/image{}.jpg".format(i) for i in range(1, 201)] random.shuffle(image_paths) image_paths = image_paths[:20] # 统计虚拟人物和红色系、蓝色系上衣的数量 total_count = 0 red_count = 0 blue_count = 0 for image_path in image_paths: # 加载图片并转换为HSV颜色空间 image = cv2.imread(image_path) hsv_image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV) # 对图片进行二值化处理,提取出人物轮廓 lower_skin = np.array([0, 20, 70], dtype=np.uint8) upper_skin = np.array([20, 255, 255], dtype=np.uint8) mask = cv2.inRange(hsv_image, lower_skin, upper_skin) contours, hierarchy = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) # 统计虚拟人物和红色系、蓝色系上衣的数量 for contour in contours: area = cv2.contourArea(contour) if area < 1000: # 忽略面积较小的轮廓 continue x, y, w, h = cv2.boundingRect(contour) roi = image[y:y+h, x:x+w] hsv_roi = cv2.cvtColor(roi, cv2.COLOR_BGR2HSV) if (hsv_roi[:, :, 0] > 170).sum() > area * 0.1: # 判断是否为虚拟人物 total_count += 1 if (hsv_roi[:, :, 0] >= lower_red[0]).sum() > area * 0.05: # 判断是否为红色系上衣 red_count += 1 elif (hsv_roi[:, :, 0] <= upper_blue[0]).sum() > area * 0.05: # 判断是否为蓝色系上衣 blue_count += 1 # 在终端输出相应的数量 print("总共有{}个虚拟人物,其中红色系上衣的人物数量为{},蓝色系上衣的人物数量为{}。".format(total_count, red_count, blue_count)) ``` 需要注意的是,这只是一个示例代码,实际情况下可能需要根据具体的图片识别要求来进行调整。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大数据采集分析 及自动化RPA

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值