主要代码参考https://blog.csdn.net/wzh191920/article/details/79589506

GitHub:https://github.com/yinghualuowu

答辩通过了,补完~

该部分代码还包括缩小边界

def img_color(card_imgs):
colors = []
for card_index, card_img in enumerate(card_imgs): green = yello = blue = black = white = 0
card_img_hsv = cv2.cvtColor(card_img, cv2.COLOR_BGR2HSV)
# 有转换失败的可能,原因来自于上面矫正矩形出错
if card_img_hsv is None:
continue
row_num, col_num = card_img_hsv.shape[:2]
card_img_count = row_num * col_num for i in range(row_num):
for j in range(col_num):
H = card_img_hsv.item(i, j, 0)
S = card_img_hsv.item(i, j, 1)
V = card_img_hsv.item(i, j, 2)
if 11 < H <= 34 and S > 34:
yello += 1
elif 35 < H <= 99 and S > 34:
green += 1
elif 99 < H <= 124 and S > 34:
blue += 1 if 0 < H < 180 and 0 < S < 255 and 0 < V < 46:
black += 1
elif 0 < H < 180 and 0 < S < 43 and 221 < V < 225:
white += 1
color = "no" limit1 = limit2 = 0
if yello * 2 >= card_img_count:
color = "yello"
limit1 = 11
limit2 = 34 # 有的图片有色偏偏绿
elif green * 2 >= card_img_count:
color = "green"
limit1 = 35
limit2 = 99
elif blue * 2 >= card_img_count:
color = "blue"
limit1 = 100
limit2 = 124 # 有的图片有色偏偏紫
elif black + white >= card_img_count * 0.7:
color = "bw"
colors.append(color)
card_imgs[card_index] = card_img if limit1 == 0:
continue
xl, xr, yh, yl = accurate_place(card_img_hsv, limit1, limit2, color)
if yl == yh and xl == xr:
continue
need_accurate = False
if yl >= yh:
yl = 0
yh = row_num
need_accurate = True
if xl >= xr:
xl = 0
xr = col_num
need_accurate = True if color =="green":
card_imgs[card_index] = card_img
else:
card_imgs[card_index] = card_img[yl:yh, xl:xr] if color != "green" or yl < (yh - yl) // 4 else card_img[
yl - (
yh - yl) // 4:yh,
xl:xr] if need_accurate:
card_img = card_imgs[card_index]
card_img_hsv = cv2.cvtColor(card_img, cv2.COLOR_BGR2HSV)
xl, xr, yh, yl = accurate_place(card_img_hsv, limit1, limit2, color)
if yl == yh and xl == xr:
continue
if yl >= yh:
yl = 0
yh = row_num
if xl >= xr:
xl = 0
xr = col_num
if color =="green":
card_imgs[card_index] = card_img
else:
card_imgs[card_index] = card_img[yl:yh, xl:xr] if color != "green" or yl < (yh - yl) // 4 else card_img[
yl - (
yh - yl) // 4:yh,
xl:xr] return colors,card_imgs

accrate_place部分

def accurate_place(card_img_hsv, limit1, limit2, color):
row_num, col_num = card_img_hsv.shape[:2]
xl = col_num
xr = 0
yh = 0
yl = row_num
row_num_limit = 21
col_num_limit = col_num * 0.8 if color != "green" else col_num * 0.5 # 绿色有渐变
for i in range(row_num):
count = 0
for j in range(col_num):
H = card_img_hsv.item(i, j, 0)
S = card_img_hsv.item(i, j, 1)
V = card_img_hsv.item(i, j, 2)
if limit1 < H <= limit2 and 34 < S and 46 < V:
count += 1
if count > col_num_limit:
if yl > i:
yl = i
if yh < i:
yh = i
for j in range(col_num):
count = 0
for i in range(row_num):
H = card_img_hsv.item(i, j, 0)
S = card_img_hsv.item(i, j, 1)
V = card_img_hsv.item(i, j, 2)
if limit1 < H <= limit2 and 34 < S and 46 < V:
count += 1
if count > row_num - row_num_limit:
if xl > j:
xl = j
if xr < j:
xr = j
return xl, xr, yh, yl

最新文章

  1. 萌新笔记——C++里创建 Trie字典树(中文词典)(三)(联想)
  2. Python爬虫学习(9):Selenium的使用
  3. Cannot set a credential for principal 'sa'. (Microsoft SQL Server,错误: 15535)
  4. ”未在本地计算机上注册“microsoft.et.OLEDB.4.0”提供程序。“解决方案大集合
  5. JAVA求圆的面积
  6. poj2486Apple Tree[树形背包!!!]
  7. 解决php的“It is not safe to rely on the system’s timezone settings”问题
  8. C语言字符串比较(转)
  9. java信号量PV操作 解决生产者-消费者问题
  10. 李洪强漫谈iOS开发[C语言-035]-选择结构-与小结
  11. 源码安装rsyslog
  12. dvtm: 平铺式终端管理器 — LinuxTOY
  13. 提高code效率
  14. AC自动机总结及板子(不带指针)
  15. 配合JdbcUtils最终版重写QueryRunner
  16. yii2高级模板使用一个域名管理前后台
  17. 转:vw适配中使用伪类选择器遇到的问题
  18. selenium+python-autoit文件上传
  19. gitlab 迁移
  20. php多进程、IPC和事件驱动

热门文章

  1. Java的JAR包, EAR包 ,WAR包 都是干什么的,有什么区别
  2. 定时node-schedule 模块的使用
  3. 关于A类,B类,C类IP地址的网段和主机数的计算方法
  4. unity3d 5.6参考手册
  5. opencv新版本的数据结构
  6. Ubuntu,kubuntu与xubuntu的差别 Ubuntu各版本主要差异
  7. 数字图像处理实验(7):PROJECT 04-03 , Lowpass Filtering 标签: 图像处理MATLAB 2017-05-25 09:30 109人
  8. Luogu 1450 [HAOI2008]硬币购物
  9. input与字符串格式化
  10. 导出Excel多个表多个sheet