程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python reads and displays raw images + numpy basic usage records

編輯:Python

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

(As a beginner, I worked on jpg and png all day, and suddenly came a raw, which means I am very big)

1. Obtain 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

Forget 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, you can directly open and read raw pictures. When opened, it will automatically display the height, width, number of channels and digits (but for now, it feels like ps is random.Match the width and length, the displayed picture is not accurate), as shown below, can also be converted to other formats if necessary, such as jpg, png, etc.

Another option is an online website: 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, the corresponding height and width and other information is corresponding to your picture

(usually very useful, you can perform various image processing operations, if the demand is not large, you don't need ps)

2.python image reading

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

import numpy as npimport cv2# Note that this function can only display data of type uint16. If it is data of uint8, please convert it to uint16 first.Otherwise, there will be problems with the picture display.**# image is of array type, it doesn't matter how many dimensions, directly operate all elementsimg = np.fromfile("D:/VScode/pyproject/PR/view/showRaw/1.raw", dtype=np.uint16)print(img)print("Total number of array elements: ",img.size) #Print the size of the array, that is, the total number of array elements# /mean, standard deviation normalization/image = (img - np.average(img)) / np.std(img)# ///print(image)imgData = image.reshape(288, 384, 1)# show imagecv2.imshow('img', imgData)cv2.waitKey()cv2.destroyAllWindows()

I have used some image normalization methods. I have a general understanding of the following three commonly used methods. You can try them according to your own pictures. My own image normalization effect is better this time.is the mean and standard deviation normalization

#/"x-min/max-min"//image = (img - np.min(img)) / (np.max(img) - np.min(img))# ///# ///simoid normalization/image = imgfor i in range(size):image[i] = 1.0 / (1 + np.exp(-float(img[i])))# ///# /mean, standard deviation normalization/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, the difference between np.mean and this is that average can be weighted average

③np.std: to standard deviation

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

⑤ np.hstack: vector stitching, raw image numpy read out in vector form, need to use

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

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 the number of image height and width channels

②image.size: length of image data

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

What I have described so far are some records of individuals in the process of learning neural networks. They are for reference only. If there is any error, please correct me in the comment area, thank you!!!


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved