Python learning-->grayscale conversion of opencv image basic operation learning

I haven't updated it for a long time, so I can't go back to the dormitory because I have to do nucleic acid today, so I will record the opencv knowledge I learned today!

The operating environment is: pycharm

Without further ado, let’s present the code:

import cv2  # opencv读取的格式是BGR
import matplotlib.pyplot as plt
import numpy as np
# 读取图片;括号里面填写好路径就行!!
img = cv2.imread("./123.jpg")

print(img)
# 图像显示在窗口上面
# cv2.imshow("image", img)
# # 参数代表关闭图片后程序关闭的时间,数字越大时间越久
# cv2.waitKey(0)
# # 窗口关闭
# cv2.destroyAllWindows()
# shape方法:shape返回的是图像的行数,列数,色彩通道数
print(img.shape)
# (1440, 1080, 3)


# 改为灰色,图片转换为灰度图
img = cv2.imread("./123.jpg", cv2.IMREAD_GRAYSCALE)
print("*" * 100)
print(img)
print(img.shape)
# (1440, 1080)
cv2.imshow("image", img)
# 参数代表等待时间
cv2.waitKey(0)
# 窗口关闭
cv2.destroyAllWindows()


# 保存改变
cv2.imwrite("123.jpg", img)


# 查看图片类型
sd = type(img)
print(sd)
# 查看图片的总像素
img.size
print(img.size)
# 查看存储类型
img.dtype
print(img.dtype)

First of all, let's load our picture in!

# 读取图片;括号里面填写好路径就行!!我这里当先目录下我导入的图片
img = cv2.imread("./123.jpg")

Follow us to try it first and open our picture to see!

Below is the implemented code!

# 图像显示在窗口上面
 cv2.imshow("image", img)
 # 参数代表关闭图片后程序关闭的时间,数字越大时间越久
 cv2.waitKey(0)
# 窗口关闭
 cv2.destroyAllWindows()

My picture after running is like this

We can see what the specific pixel data of the picture looks like!

img = cv2.imread("./123.jpg")

print(img)

The result of the output is:

[[[129 129 129]
  [129 129 129]
  [129 129 129]
  ...
  [ 76  76  76]
  [ 77  77  77]
  [ 78  78  78]]

 [[129 129 129]
  [129 129 129]
  [129 129 129]
  ...
  [ 75  75  75]
  [ 76  76  76]
  [ 77  77  77]]

 [[129 129 129]
  [129 129 129]
  [129 129 129]
  ...
  [ 74  74  74]
  [ 75  75  75]
  [ 75  75  75]]

 ...

 [[160 160 160]
  [160 160 160]
  [161 161 161]
  ...
  [ 59  59  59]
  [ 60  60  60]
  [ 60  60  60]]

 [[160 160 160]
  [160 160 160]
  [160 160 160]
  ...
  [ 60  60  60]
  [ 60  60  60]
  [ 60  60  60]]

 [[159 159 159]
  [160 160 160]
  [160 160 160]
  ...
  [ 60 60 60]
  [ 60 60 60]
  [ 61 61 61]]]
There are more than a billion points! Ha ha! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! !

This completes the first step!

Here we introduce the usage of shape by the way!

# shape method: shape returns the number of rows, columns, and color channels of the image
print(img.shape)
# (1440, 1080, 3)

In the second step, we think that this color picture should be changed to a grayscale picture, because sometimes it is necessary to change the picture to a grayscale picture when processing pictures

# 改为灰色,图片转换为灰度图
img = cv2.imread("./123.jpg", cv2.IMREAD_GRAYSCALE)

Completing this step is actually almost the same, and then, it is the same as the beginning

cv2.imshow("image", img)
 # 参数代表关闭图片后程序关闭的时间,数字越大时间越久
cv2.waitKey(0)
# 窗口关闭
cv2.destroyAllWindows()

but also to add

If we change the image 123.jpg to a grayscale image; and then save it, then our original color image will be changed to a grayscale image. become like below!

# 保存函数
cv2.imwrite("123.jpg", img)

Finally, let's popularize the functions of several methods by the way:

# 查看图片类型
sd = type(img)
print(sd)
# 查看图片的总像素
img.size
print(img.size)
# 查看存储类型
img.dtype
print(img.dtype)

That’s all I’ve shared today, if there’s anything wrong with the above or if you want to communicate with me, you can private message me! ! ! !

Guess you like

Origin blog.csdn.net/hhR888888/article/details/127659463