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

python+opencv study notes

編輯:Python

(The following is a bit of a mess,Mainly personal note taking,Hope some of the content is helpful to you)

目錄

1.學習背景

2.視頻幀讀取+保存jpg+MP4

3.opencv  cv.imshow()注意點

1.學習背景

In this study, the team leader gave me a few sheets without any header files and other informationraw圖(It's all done beforejpg,png,rawIt is really troublesome to open and use it),and turn it on,其中通過Photopea | Online Photo EditorThis website first went to browse the general content of the picture,Just look at the general content,Because these softwares are just intelligent to help you fit the right size,But the specific normalization and other operations still need to be done by yourself.The final result is fourraw來自不同設備,Two are just single sheetsraw圖,One is made up of two pictures assembled together,A picture is saved as a picture from multiple video frames.

(具體raw1,2,3The operation opens to see the previous articlePython讀取顯示raw圖片+numpy基本用法記錄_Brother Taoist's blog-CSDN博客)

2.視頻幀讀取+保存jpg+MP4

①Clipping of video frames+jpg保存

在raw所讀取的numpy數組中,The order of a single frame of pictures is the same as the data in the array,So we only need to get the size of a single frame of picture,And then calculate the single frame picturesize(It needs to be adjusted manually here,In the process of tuning, you will find a lot of repetitive content,When it is adjusted to the content of only a single frame, it is the corresponding size,見下圖)

 So my single frame is384*288,原圖尺寸是1728*1792,So just use it nowimg(x:x+s)We can cut out our single frame picture,下面是代碼演示

import numpy as np
import cv2
# image為array類型,It doesn't matter how many dimensions,Manipulate all elements directly
img = np.fromfile("D:/VScode/pyproject/PR/view/showRaw/4.raw", dtype=np.uint16)
print(img)
print("數組元素總數:",img.size) #Print array dimensions,That is, the total number of elements in the array
def imgv(img):
s = 0
# Count the number of images in a single frame
t = int(img.size/110592)
# Generates an empty vector that can be used to place a single-frame image array
a = np.random.randint(0,1,(t,110592))
print(a)
for i in range(t):
# Single frame capture
a[i] =img[s:s+110592]
# /“x-min/max-min”歸一化//
imagec = (a[i] - np.min(a[i])) / (np.max(a[i]) - np.min(a[i]))
# ///
imgDatac = imagec.reshape(288, 384, 1)
s = s+ 110592
print("顯示第%d張圖片"%(i+1))
imgDatac = imgDatac*255
cv2.imwrite("D:/VScode/pyproject/PR/view/rawview/p4jpg/4.{}.jpg".format(i+1),imgDatac)
result = "Every frame is saved successfully!"
return result
result = imgv(img)
print(result)

 ②Video frames are savedmp4

Video reading only needs to be usedcv.vediowriterThe graph can be saved as mp4格式,代碼如下

import glob
import os
from ssl import PROTOCOL_TLSv1_2
import cv2
# //Video frames are composited into video.mp4///
def pic2video(pic_root_path):
fps = 10 # 設置視頻幀率
fourcc = cv2.VideoWriter_fourcc(*'MP4V') # 設置視頻編碼方式
videoWriter = cv2.VideoWriter('result.mp4', fourcc, fps, (384,288)) # 創建videoWriter 實例,視頻sizeneed with picturessize相同
filePathNames = glob.glob(os.path.join(pic_root_path, '*.jpg')) # 遍歷目錄下所有jpg文件
filePathNames.sort() # 排序
n = len(filePathNames)
for i in range(n):
print('writing frame {}'.format(i))
img = cv2.imread(filePathNames[i])
videoWriter.write(img)
videoWriter.release()
path = "D:/VScode/pyproject/PR/view/rawview/p4jpg/"
pic2video(path)

3.opencv  cv.imshow()注意點

①在使用cv.imshow()過程中,when i picturearrayWhen a negative number appears in ,opencvNegative numbers are automatically erased,然後用0進行代替,I tried using negative numbers myself0代替,Then the picture does not follow0processed images for comparison,發現是一模一樣的,Also verified my idea

import numpy as np
import cv2
import torch
# image為array類型,It doesn't matter how many dimensions,Manipulate all elements directly
img = np.fromfile("D:/VScode/pyproject/PR/view/showRaw/1.raw", dtype=np.uint16)
image = (img - np.average(img)) / np.std(img)
# It can be seen that there is a negative number when it is printed out
print(image)
imgData1 = image.reshape(288, 384, 1)
# Set negative numbers to zero
imgData2= np.where(imgData1>0,imgData1,0)
print(imgData2)
imgData1 = imgData1*255
imgData2 = imgData2*255
cv2.imwrite("1.jpg",imgData1)
cv2.imwrite("2.jpg",imgData2)

 ②不同位數opencvDisplay processing is different

After doing image normalization,I normalized the data as I did before*255,然後將其顯示,In the end, the pictures were all exposed,Said that after I multiplied the number is much greater than255了,但是我printAfter that, the numbers are clearly correct,So I suspect the problem should be therecv.imshow,我使用help(cv2.imshow)Check out the official documentation,最終發現

如果圖像是8位無符號的,is displayed as is.

如果圖像是16位無符號的,then the pixels are divided by 256.即將[0,255*256]The value range of is mapped to[0,255].

如果圖像是32位或64位浮點,則像素值乘以255.The upcoming value range[0,1]映射為[0,255].

32Bit integer images are no longer processed,Due to the ambiguity of the required transformation.Convert to using custom preprocessing specific to the image context8Bit unsigned matrix

 And my image is16位的,After normalization the data becomes 64位,So it gives me data by itself*255,That's why it's exposed,So my data is not required to proceed255Grayscale processing

 The above is the record of the study notes,我也是個新手,Purely personal opinion haha,If there is any statement or understanding wrong,I also trouble you to help me criticize and correct me.


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