没有合适的资源?快使用搜索试试~ 我知道了~
用Pythonopencv提取图像中的区域
需积分: 50 13 下载量 35 浏览量
2020-11-24
13:39:04
上传
评论
收藏 6KB TXT 举报
温馨提示
试读
8页
这段代码是通过把图片转为hsv颜色空间进行颜色提取 可以改变lower_red和upper_red的第一个参数来改变要提取的颜色
资源推荐
资源详情
资源评论
import cv2
import numpy as np
red=np.uint8([[[0,0,255]]])
hsv_red=cv2.cvtColor(red,cv2.COLOR_BGR2HSV)
print hsv_red
img=cv2.imread("D:\\1.jpg")
res=cv2.resize(img,None,fx=0.3,fy=0.3,interpolation=cv2.INTER_CUBIC)
cv2.imshow('m',res)
hsv=cv2.cvtColor(res,cv2.COLOR_BGR2HSV)
lower_red=np.array([170,100,100])
upper_red=np.array([180,255,255])
mask=cv2.inRange(hsv,lower_red,upper_red)
res=cv2.bitwise_and(res,res,mask=mask)
cv2.imshow('frame',res)
cv2.imshow('mask',mask)
cv2.waitKey(0)
///////////////////2
#
# import colorsys
# from PIL import Image
#
# def get_dominant_color(image):
# # 颜色模式转换,以便输出rgb颜色值
# image = image.convert( 'RGBA' )
#
# # 生成缩略图,减少计算量,减小cpu压力
# image.thumbnail( (200, 200) )
#
import numpy as np
red=np.uint8([[[0,0,255]]])
hsv_red=cv2.cvtColor(red,cv2.COLOR_BGR2HSV)
print hsv_red
img=cv2.imread("D:\\1.jpg")
res=cv2.resize(img,None,fx=0.3,fy=0.3,interpolation=cv2.INTER_CUBIC)
cv2.imshow('m',res)
hsv=cv2.cvtColor(res,cv2.COLOR_BGR2HSV)
lower_red=np.array([170,100,100])
upper_red=np.array([180,255,255])
mask=cv2.inRange(hsv,lower_red,upper_red)
res=cv2.bitwise_and(res,res,mask=mask)
cv2.imshow('frame',res)
cv2.imshow('mask',mask)
cv2.waitKey(0)
///////////////////2
#
# import colorsys
# from PIL import Image
#
# def get_dominant_color(image):
# # 颜色模式转换,以便输出rgb颜色值
# image = image.convert( 'RGBA' )
#
# # 生成缩略图,减少计算量,减小cpu压力
# image.thumbnail( (200, 200) )
#
# max_score = 0 #原来的代码此处为None
# dominant_color = 0 # 原来的代码此处为None,但运行出错,改为0以后 运行成功,原因在于在下面的 score > max_score的比较中,max_score的初始格式不定
#
# for count, (r, g, b, a) in image.getcolors( image.size[0] * image.size[1] ):
# # 跳过纯黑色
# if a == 0:
# continue
#
# saturation = colorsys.rgb_to_hsv( r / 255.0, g / 255.0, b / 255.0 )[1]
#
# y = min( abs( r * 2104 + g * 4130 + b * 802 + 4096 + 131072 ) >> 13, 235 )
#
# y = (y - 16.0) / (235 - 16)
#
# # 忽略高亮色
# if y > 0.9:
# continue
#
# # Calculate the score, preferring highly saturated colors.
# # Add 0.1 to the saturation so we don't completely ignore grayscale
# # colors by multiplying the count by zero, but still give them a low
# # weight.
# score = (saturation + 0.1) * count
#
# if score > max_score:
# max_score = score
# dominant_color = (r, g, b)
#
# return dominant_color
#
# dominant_color = 0 # 原来的代码此处为None,但运行出错,改为0以后 运行成功,原因在于在下面的 score > max_score的比较中,max_score的初始格式不定
#
# for count, (r, g, b, a) in image.getcolors( image.size[0] * image.size[1] ):
# # 跳过纯黑色
# if a == 0:
# continue
#
# saturation = colorsys.rgb_to_hsv( r / 255.0, g / 255.0, b / 255.0 )[1]
#
# y = min( abs( r * 2104 + g * 4130 + b * 802 + 4096 + 131072 ) >> 13, 235 )
#
# y = (y - 16.0) / (235 - 16)
#
# # 忽略高亮色
# if y > 0.9:
# continue
#
# # Calculate the score, preferring highly saturated colors.
# # Add 0.1 to the saturation so we don't completely ignore grayscale
# # colors by multiplying the count by zero, but still give them a low
# # weight.
# score = (saturation + 0.1) * count
#
# if score > max_score:
# max_score = score
# dominant_color = (r, g, b)
#
# return dominant_color
#
剩余7页未读,继续阅读
资源评论
闻立武
- 粉丝: 10
- 资源: 21
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功