You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

OCR中地图色调反转方法及蓝色色调统计与RGB提取需求

实现方案

Python 方法

依赖安装

pip install pillow numpy

代码示例

from PIL import Image
import numpy as np

# 加载图片(替换为你的图片路径)
img = Image.open("EXGX5.jpg")
img_rgb = img.convert("RGB")
pixels = np.array(img_rgb)

# 提取所有唯一RGB颜色
unique_colors = np.unique(pixels.reshape(-1, 3), axis=0)

# 筛选蓝色系颜色(可根据实际图片调整阈值)
blue_colors = [color for color in unique_colors if color[0] < 150 and color[1] < 180 and color[2] > 100]

# 基于亮度排序(亮度越低颜色越深),生成深浅标记
blue_with_brightness = [(color, 0.299*color[0] + 0.587*color[1] + 0.114*color[2]) for color in blue_colors]
# 按亮度升序排列(最深到最浅),反转后标记最浅为1
sorted_blues = sorted(blue_with_brightness, key=lambda x: x[1])
color_labels = {tuple(color): i+1 for i, (color, _) in enumerate(reversed(sorted_blues))}

# 输出结果
print("蓝色色调标记与RGB值:")
for rgb, label in color_labels.items():
    print(f"标记 {label}: RGB{rgb}")

R 方法

依赖安装

install.packages(c("png", "dplyr"))

代码示例

library(png)
library(dplyr)

# 读取图片(JPG格式请改用jpeg包的readJPEG)
img <- readPNG("EXGX5.jpg")
# 转换为RGB像素数据框
pixels <- data.frame(
  R = as.integer(as.vector(img[,,1]) * 255),
  G = as.integer(as.vector(img[,,2]) * 255),
  B = as.integer(as.vector(img[,,3]) * 255)
)

# 去重并筛选蓝色系颜色
blue_colors <- pixels %>% 
  distinct(R, G, B) %>% 
  filter(R < 150, G < 180, B > 100)

# 计算亮度并生成深浅标记(最浅为1)
blue_colors <- blue_colors %>% 
  mutate(brightness = 0.299*R + 0.587*G + 0.114*B) %>% 
  arrange(desc(brightness)) %>% 
  mutate(label = row_number())

# 输出结果
cat("蓝色色调标记与RGB值:\n")
print(blue_colors %>% select(label, R, G, B))

注意事项

  • 蓝色系的筛选阈值需要根据实际图片调整,避免混入非地图的蓝色(比如边框、文字背景等)。
  • 如果图片存在抗锯齿导致的过渡色,可以增加颜色聚类步骤(如用sklearn.cluster.KMeans)来合并相近色调。

内容的提问来源于stack exchange,提问作者Frank Swanton

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.26 15:32:26