Python reads and displays raw images + numpy basic usage records

This task is to get a few raw pictures, no other header files or information, you need to use python to open and display the raw pictures

(As a beginner, I have been working on jpg and png all day, and suddenly came a raw, which means it is very big)

1. Get raw image shape information (height, width, number of channels)

Open the display through a third party, find its information, record it, and use it later

Let’s forget about all kinds of raw to jpg images searched on the Internet. I tried one or two, but they all failed.

Two methods are recommended here, one is software: ps, using ps can directly open and read raw pictures, and when opened, it will automatically display the height, width, number of channels and digits (but for now, it feels that ps randomly matches the width and length) , the displayed picture is not accurate), as shown in the figure below, it can also be converted to other formats if necessary, such as jpg, png, etc.

 Another way is an online site: Photopea | Online Photo Editor

Super easy to use! ! ! And you can choose a variety of matching forms, you can also preview the picture, and then you can choose the correct matching form, and the corresponding information such as height and width is corresponding to your picture

 (It is also very easy to use at ordinary times, and can be used for various image processing operations. If the demand is not large, you don’t need ps)

2.python image reading

   With the information of the number of high and wide channels, we can analyze the numpy image, and finally read the image through opencv

import numpy as np
import cv2

# 注意到这个函数只能显示uint16类型的数据,如果是uint8的数据请先转成uint16。否则图片显示会出现问题。**
# image为array类型,多少维度都无所谓,直接操作全部元素
img = np.fromfile("D:/VScode/pyproject/PR/view/showRaw/1.raw", dtype=np.uint16)
print(img)  
print("数组元素总数:",img.size)      #打印数组尺寸,即数组元素总数  


# /均值、标准差归一化/
image = (img - np.average(img)) / np.std(img)
# ///
print(image)

imgData = image.reshape(288, 384, 1)
# 展示图像
cv2.imshow('img',imgData)
cv2.waitKey()
cv2.destroyAllWindows()

Among them, I used some image normalization methods. I have roughly learned that there are the following three commonly used methods. You can try them according to your own pictures. The better effect of normalizing my pictures this time is the mean value. , standard deviation normalization

# /“x-min/max-min”//
image = (img - np.min(img)) / (np.max(img) - np.min(img))
# ///


# ///simoid归一化/
image = img
for i in range(size):
    image[i] = 1.0 / (1 + np.exp(-float(img[i])))
# ///

# /均值、标准差归一化/
image = (img - np.average(img)) / np.std(img)
# ///

 3. Some basic usage of numpy image processing

①np.fromfile

The raw image can be read and converted into a vector array, which is convenient for us to process later

imgData2 = np.fromfile('D:/VScode/pyproject/PR/view/showRaw/2.raw', dtype='uint16')

②np.average: Take the average value. The difference between np.mean and this is that the average can be weighted average

③np.std: remove the standard deviation

④np.random.randint(0,10,(4,3)): Randomly generate a 4x3 matrix in 0-10

⑤ np.hstack: vector splicing, the raw image numpy is read out in vector form, which needs to be used

⑥np.concatenate((a, b), axis=1): The ab matrix is ​​concatenated in the form of columns, adding 1 more column 

rotation = np.array([[1, 2, 3],
                     [4, 5, 6],
                     [7, 8, 9]])
 
trans = np.array([[7],
                  [8],
                  [0]])
z = np.concatenate((rotation, trans), axis=1)
[[1 2 3 7]
 [4 5 6 8]
 [7 8 9 0]]

4. Some instructions for image processing

①image.shape: display image height and width channels

②image.size: image data length

③image[y:y+h,x:x+w]: interception of the image, xy is the starting point of the upper left corner of the interception

 At present, the above are some records of individuals in the process of learning neural networks. They are for reference only. If there are any mistakes, please correct them in the comment area, thank you! ! !

Guess you like

Origin blog.csdn.net/hhb3329/article/details/126141962