**
代码直接可以用,只需要改一下自己的路径即可
彩色图片显示:
# -*- coding:utf-8 -*-
import cv2
import numpy
import numpy as np
import pylab
import PIL.Image as Image
import matplotlib.pyplot as plt
imgfile = input("请输入图片名:")
txtfile = input("请输入存储文本文件名:")
'''
cv2.imread()用于读取图片文件,这里要注意的是三通道顺序为BGR,后边需要转化为RGB顺序输出
imread函数有两个参数,第一个参数是图片路径,第二个参数表示读取图片的形式,有三种:
cv2.IMREAD_COLOR:加载彩色图片,这个是默认参数,可以直接写1。
cv2.IMREAD_GRAYSCALE:以灰度模式加载图片,可以直接写0。
cv2.IMREAD_UNCHANGED:包括alpha,可以直接写-1
'''
img = cv2.imread("G:\python\image_array\images" + "\\" + imgfile + ".png", cv2.IMREAD_COLOR)
print("图像的形状,返回一个图像的(行数,列数,通道数):", img.shape)
print("图像的像素数目:", img.size)
print("图像的数据类型:", img.dtype)
print("img:",img[0][0][1]) # 查看一下读取是否正常
# ----------------------------------------------------------------------------
'''
In windows the COLOR->GRAYSCALE: Y = 0.299R+0.587G+0.114B 测试是否三个通道的值是相同的。
某些图像三通道值相同,可以直接读灰度图来代替读单一通道。
"""
sum = 0
ans = 0
for i in range(328):1
for j in range(640):
if not(img[i][j][0] == img[i][j][1] and img[i][j][1] == img[i][j][2]):
sum += 1
else:
ans += 1
print(ans)
print(sum)
'''
# -----------------------------------------------------------------------------
fname = open("G:\python\image_array\images" + "\\" + txtfile, 'w')
Xlenth = img.shape[1] # 图片列数
Ylenth = img.shape[0] # 图片行数
k = 3 # 每一个像素点RGB三通道的值
for i in range(Ylenth):
for j in range(Xlenth):
for h in range(k):
fname.write(str(img[i][j][h]) + ' ')
fname.close()
# ---------------------------------------------------------------------------
'''
将txt文件中的数据读取进blist
并显示为"test"图片框进行测试。
'''
# -----------------------------------------------------------------------------
blist = []
split_char = ' ' # 空格的含义,具体可查看strip与spilt的用法
with open("G:\python\image_array\images" + "\\" + txtfile, 'r') as bf:
blist = [b.strip().split(split_char) for b in bf]
# 下边是花费了一天才找到的问题,读取txt文件的blist最后是一个二维的,blist[0][图像最大值-1]
# array_len = 图像的像素数目*RGB三通道(3)
array_len = Ylenth * Xlenth * 3
print("blist:", (blist[0][array_len - 1]))
# 从txt文件读入进来的值类型是char需要转换为int
for i in range(1):
for j in range(array_len - 1):
blist[i][j] = int(blist[i][j])
# 提取出来所需要的的整形数据,然后将其分成三维数组new_blist,这样有便于通道分离
new_blist = np.zeros((img.shape)).astype("uint8")
i = 0
for j in range(Ylenth):
for k in range(Xlenth):
for l in range(3):
new_blist[j][k][l] = blist[0][i]
i = i + 1
if (i >= array_len):
break
# 验证测试,与写入img的txt数据进行对比
print("new_blist:", (new_blist[0][0][1]))
# 因为ov的存储图像的方式是BGR顺序,所以要将其转变成RGB输出的话才能得到正确的图像
B = new_blist[:, :, 0]
G = new_blist[:, :, 1]
R = new_blist[:, :, 2]
src_new = np.zeros((img.shape)).astype("uint8")
src_new[:, :, 0] = R
src_new[:, :, 1] = G
src_new[:, :, 2] = B
# 这句话可有可无,去掉的话把下边的tlist变成src_new即可,因为目的创建多维数组
tlist = numpy.array(src_new)
# 绘制图像
plt.figure()
plt.imshow(tlist)
plt.axis('on') # 不显示坐标轴
pylab.show()
# ------------------------------------------------------------------------------
# 原图片显示在img框
cv2.imshow('image', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
灰度图片显示:
# -*- coding:utf-8 -*-
import cv2
import numpy
import pylab
import PIL.Image as Image
import matplotlib.pyplot as plt
from cv2 import COLOR_RGB2GRAY
imgfile = input("请输入图片名:")
txtfile = input("请输入存储文本文件名:")
'''
cv2.imread()用于读取图片文件
imread函数有两个参数,第一个参数是图片路径,第二个参数表示读取图片的形式,有三种:
cv2.IMREAD_COLOR:加载彩色图片,这个是默认参数,可以直接写1。
cv2.IMREAD_GRAYSCALE:以灰度模式加载图片,可以直接写0。
cv2.IMREAD_UNCHANGED:包括alpha,可以直接写-1
'''
img = cv2.imread("G:\python\image_array\images" + "\\" + imgfile + ".png",cv2.IMREAD_GRAYSCALE)
# img = cv2.cvtColor(img, COLOR_RGB2GRAY)
print("图像的形状,返回一个图像的(行数,列数,通道数):", img.shape)
print("图像的像素数目:", img.size)
print("图像的数据类型:", img.dtype)
fname = open("G:\python\image_array\images" + "\\" + txtfile, 'w')
Xlenth = img.shape[1] # 图片列数
Ylenth = img.shape[0] # 图片行数
for i in range(Ylenth):
for j in range(Xlenth):
fname.write(str(img[i][j]) + ' ')
fname.write('\n')
fname.close()
# ---------------------------------------------------------------------------
blist = []
split_char = ' '
with open("G:\python\image_array\images" + "\\" + txtfile, 'r') as bf:
blist = [b.strip().split(split_char) for b in bf]
# 从txt文件读入进来的值类型是char需要转换为int
for i in range(Ylenth):
for j in range(Xlenth):
blist[i][j] = int(blist[i][j])
tlist = numpy.array(blist)
plt.figure()
plt.title("gray")
plt.imshow(tlist, cmap='gray')
plt.axis('on') # 不显示坐标轴
pylab.show()
# ------------------------------------------------------------------------------
# """
# 将图片显示在'image'图片框
# """
#
cv2.imshow('image', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
关于numpy的解释:https://blog.csdn.net/fu6543210/article/details/83240024
关于plt.imshow():https://blog.csdn.net/weixin_44690866/article/details/111877574
思路源头:https://www.cnblogs.com/vocaloid01/p/9514142.html
自己菜比一个,希望大佬们有什么建议啥的评论区说一下,刚开始学习。