OpenCVcv2.imread传入灰度图像,shape还是3通道

opencv中的imread函数用法:

cv2.imread(filename, flags=None)

其中第一个参数是载入图片名字,第二个参数是int类型的flags,为载入标识,它指定一个加载图像的颜色类型。可以看到它自带缺省值1。
其中:
flags =1返回一个3通道的彩色图像
flags =0返回灰度图像。
flags <0返回包含Alpha通道的加载的图像。

用函数的时候没有设置第二个参数,就会默认值为1。
尽管读入的是灰度图,还是会自动转化成三通道图像。
可以将第二个参数置为0得到灰度图像

import cv2 as cv2

image = cv2.imread("run.png", 0)
cv2.imwrite("C:\\Users\\Administrator\\Desktop\\My_Study\\CV\\run_huiidu.jpg", image)
image_huidu = cv2.imread("run_huiidu.jpg")
print(image_huidu)
cv2.imshow("run1", image_huidu)
cv2.imshow("run_huidu", image_huidu)
cv2.waitKey()  # 1000ms为1s,在等待时间结束前图片仍可以通过按下任意键提前结束
cv2.destroyAllWindows()

image_color = cv2.imread("run.png")
print("获取色彩图像的属性")
print("shape =", image_color.shape)
print("size =", image_color.size)
print("dtype =", image_color.dtype)
image_Gray = cv2.imread("run_huiidu.jpg",0)
print("获取灰度图像的属性")
print("shape =", image_Gray.shape)
print("size =", image_Gray.size)
print("dtype =", image_Gray.dtype)

[[[255 255 255]
  [255 255 255]
  [255 255 255]
  ...
  [255 255 255]
  [255 255 255]
  [255 255 255]]

 [[255 255 255]
  [255 255 255]
  [255 255 255]
  ...
  [255 255 255]
  [255 255 255]
  [255 255 255]]

 [[255 255 255]
  [255 255 255]
  [255 255 255]
  ...
  [255 255 255]
  [255 255 255]
  [255 255 255]]

 ...

 [[253 253 253]
  [253 253 253]
  [255 255 255]
  ...
  [234 234 234]
  [249 249 249]
  [249 249 249]]

 [[252 252 252]
  [253 253 253]
  [255 255 255]
  ...
  [249 249 249]
  [254 254 254]
  [252 252 252]]

 [[252 252 252]
  [253 253 253]
  [255 255 255]
  ...
  [250 250 250]
  [251 251 251]
  [250 250 250]]]
获取色彩图像的属性
shape = (300, 300, 3)
size = 270000
dtype = uint8
获取灰度图像的属性
shape = (300, 300)
size = 90000
dtype = uint8

进程已结束,退出代码为 0

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Good_Yes_Top_

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

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

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

打赏作者

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

抵扣说明:

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

余额充值