将cv::Mat转换为YUV420
最编程
2024-08-15 15:27:49
...
void WriteYuv() { cv::VideoCapture vc; bool flag = vc.open("S1000008.avi"); if (!flag) { printf("avi file open error \n"); system("pause"); exit(-1); } int frmCount = vc.get(CV_CAP_PROP_FRAME_COUNT); frmCount -= 5; printf("frmCount: %d \n", frmCount); int w = vc.get(CV_CAP_PROP_FRAME_WIDTH); int h = vc.get(CV_CAP_PROP_FRAME_HEIGHT); int bufLen = w*h*3/2; unsigned char* pYuvBuf = new unsigned char[bufLen]; FILE* pFileOut = fopen("result.yuv", "w+"); if (!pFileOut) { printf("pFileOut open error \n"); system("pause"); exit(-1); } printf("pFileOut open ok \n"); for (int i=0; i<frmCount; i++) { printf("%d/%d \n", i+1, frmCount); cv::Mat srcImg; vc>>srcImg; cv::imshow("img", srcImg); cv::waitKey(1); cv::Mat yuvImg; cv::cvtColor(srcImg, yuvImg, CV_BGR2YUV_I420); memcpy(pYuvBuf, yuvImg.data, bufLen*sizeof(unsigned char)); fwrite(pYuvBuf, bufLen*sizeof(unsigned char), 1, pFileOut); } fclose(pFileOut); delete[] pYuvBuf; }
推荐阅读
-
将YUV420图像转换为BGR图像的方法使用OpenCvSharp实现
-
将 Android UVC Camera 获取的 YUV 转换为 Mat 的方法
-
将cv::Mat转换为YUV420
-
将标题重写为:“将YUV数据格式从NV12转换为I420并进行90度旋转”
-
将yuv420格式转换为bmp格式
-
14-傅里叶变换的代码实现-一、numpy实现傅里叶变换和逆傅里叶变换 1.numpy实现傅里叶变换numpy.fft.fft2实现傅里叶变换,返回一个复数数组(complex ndarray),也就是频谱图像numpy.fft.fftshift将零频率分量移到频谱中心(将左上角的低频区域,移到中心位置) 20*np.log(np.abs(fshift))设置频谱的范围。可以理解为,之前通过傅里叶变换得到复数的数组,是不能通过图像的方法展示出来的,需要转换为灰度图像(映射到[0,255]区间)需要注意的是1> 傅里叶得到低频、高频信息,针对低频、高频处理能够实现不同的目的2> 傅里叶过程是可逆的,图像经过傅里叶变换、逆傅里叶变换后,能够恢复到原始图像3> 在频域对图像进行处理,在频域的处理会反映在逆变换图像上 # 将绘制的图显示在窗口 %matplotlib qt5 import cv2 import numpy as np import matplotlib.pyplot as plt img = cv2.imread(r"image\lena.bmp",cv2.IMREAD_GRAYSCALE) # 傅里叶变换 f = np.fft.fft2(img) # 移动中心位置 fshift = np.fft.fftshift(f) # 调整值范围 result = 20*np.log(np.abs(fshift)) plt.subplot(1,2,1) plt.imshow(img,cmap=plt.cm.gray) plt.title("original") plt.axis("off") plt.subplot(1,2,2) plt.imshow(result,cmap=plt.cm.gray) plt.title("result") plt.axis("off") plt.show 傅里叶变换的频谱图像: 2.numpy实现逆傅里叶变换numpy.fft.ifft2实现逆傅里叶变换,返回一个复数数组(complex ndarray)numpy.fft.ifftshiftfftshift函数的逆函数,将中心位置的低频,重新移到左上角iimg = np.abs(逆傅里叶变化结果)设置值的范围,映射到[0,255]区间 # 将绘制的图显示在窗口 %matplotlib qt5 import cv2 import numpy as np import matplotlib.pyplot as plt img = cv2.imread(r"image\boat.bmp",cv2.IMREAD_GRAYSCALE) # 傅里叶变换 f = np.fft.fft2(img) fshift = np.fft.fftshift(f) # 逆傅里叶变换 ishift = np.fft.ifftshift(fshift) iimg = np.fft.ifft2(ishift) iimg = np.abs(iimg) plt.subplot(1,2,1) plt.imshow(img,cmap=plt.cm.gray) plt.title("original") plt.axis("off") plt.subplot(1,2,2) plt.imshow(iimg,cmap=plt.cm.gray) plt.title("iimg") plt.axis("off") plt.show 将一副图像,进行傅里叶变换和逆傅里叶变换后,进行对比(一样的) 实例:通过numpy实现高通滤波,保留图像的边缘信息 获取图像的形状rows,cols = img.shape获取图像的中心点crow,ccol = int(rows/2),int(cols/2)将频谱图像的中心区域(低频区域)设置为0(黑色)fshift[crow-30:crow+30,ccol-30:ccol+30] = 0 # 将绘制的图显示在窗口 %matplotlib qt5 import cv2 import numpy as np import matplotlib.pyplot as plt img = cv2.imread(r"image\boat.bmp",cv2.IMREAD_GRAYSCALE) # 傅里叶变换 f = np.fft.fft2(img) fshift = np.fft.fftshift(f) # 高通滤波 rows,cols = img.shape crow,ccol = int(rows/2),int(cols/2) fshift[crow-30:crow+30,ccol-30:ccol+30] = 0 # 逆傅里叶变换 ishift = np.fft.ifftshift(fshift) iimg = np.fft.ifft2(ishift) iimg = np.abs(iimg) plt.subplot(1,2,1) plt.imshow(img,cmap=plt.cm.gray) plt.title("original") plt.axis("off") plt.subplot(1,2,2) plt.imshow(iimg,cmap=plt.cm.gray) plt.title("iimg") plt.axis("off") plt.show 使用numpy实现高通滤波的实验结果: 二、opencv实现傅里叶变换和逆傅里叶变换 1.opencv实现傅里叶变换 返回结果 = cv2.dft(原始图像,转换标识)1> 返回结果:是双通道的,第一个通道是结果的实数部分,第二个通道是结果的虚数部分2> 原始图像:输入图像要首先转换成np.float32(img)格式3> 转换标识:flags = cv2.DFT_COMPLEX_OUTPUT,输出一个复数阵列numpy.fft.fftshift将零频率分量移到频谱中心(将左上角的低频区域,移到中心位置)调整频谱的范围,将上面频谱图像的复数数组,转换为可以显示的灰度图像(映射到[0,255]区间)返回值 = 20*np.log(cv2.magnitude(参数1,参数2))1> 参数1:浮点型X坐标值,也就是实部2> 参数2:浮点型Y坐标值,也就是虚部 # 将绘制的图显示在窗口 %matplotlib qt5 import cv2 import numpy as np import matplotlib.pyplot as plt img = cv2.imread(r"image\lena.bmp",cv2.IMREAD_GRAYSCALE) # 傅里叶变换 dft = cv2.dft(np.float32(img),flags = cv2.DFT_COMPLEX_OUTPUT) # 移动中心位置 dftShift = np.fft.fftshift(dft) # 调整频谱的范围 result = 20*np.log(cv2.magnitude(dftShift[:,:,0],dftShift[:,:,1])) plt.subplot(1,2,1) plt.imshow(img,cmap=plt.cm.gray) plt.title("original") plt.axis("off") plt.subplot(1,2,2) plt.imshow(result,cmap=plt.cm.gray) plt.title("result") plt.axis("off") plt.show 傅里叶变换的频谱图像: 2.opencv实现逆傅里叶变换返回结果 = cv2.idft(原始数据)1> 返回结果:取决于原始数据的类型和大小2> 原始数据:实数或者复数均可numpy.fft.ifftshiftfftshift函数的逆函数,将中心位置的低频,重新移到左上角调整频谱的范围,映射到[0,255]区间返回值 = cv2.magnitude(参数1,参数2)1> 参数1:浮点型X坐标值,也就是实部2> 参数2:浮点型Y坐标值,也就是虚部 # 将绘制的图显示在窗口 %matplotlib qt5 import cv2 import numpy as np import matplotlib.pyplot as plt img = cv2.imread(r"image\lena.bmp",cv2.IMREAD_GRAYSCALE) # 傅里叶变换 dft = cv2.dft(np.float32(img),flags = cv2.DFT_COMPLEX_OUTPUT) dftShift = np.fft.fftshift(dft) # 逆傅里叶变换 ishift = np.fft.ifftshift(dftShift) iimg = cv2.idft(ishift) iimg = cv2.magnitude(iimg[:,:,0],iimg[:,:,1]) plt.subplot(1,2,1) plt.imshow(img,cmap=plt.cm.gray) plt.title("original") plt.axis("off") plt.subplot(1,2,2) plt.imshow(iimg,cmap=plt.cm.gray) plt.title("inverse") plt.axis("off") plt.show 将一副图像,进行傅里叶变换和逆傅里叶变换后,进行对比(一样的) 实例:通过opencv实现低通滤波,模糊一副图像
-
如何通过 pybind11 将语义分割 cv::mat 结果返回给 python 调用
-
如何将 .mat 文件转换为 .nii 文件 - .mat 文件转换为 .nii 文件
-
学习 linux gcc/g++ 常用命令行编译指令 - 项目目录 ~learn_cmake2/ +src/ |-cpp1.cpp |-cpp2.cpp |-main.cpp +include/ |-cpp1.h |-main.cpp +build/ +CMakeList cpp1.h #include<iostream>; 使用命名空间 std. void print1. cpp2.h #include<iostream>. 使用命名空间 std.NET void print2. cpp1.cpp #include "cpp1.h" void print1 { cout<< "this is cpp1"<< endl; } cpp2.cpp #include "cpp2.h" void print2 { cout<< "this is cpp2"<< endl; } main.cpp #include<iostream>; #include "opencv2/highgui/highgui/highgui #include "opencv2/highgui/highgui.hpp" #include "cpp1.hpp #include "cpp1.h" #include "cpp2.h" 使用 namespace std. 使用 namespace std; 使用 namespace cv. cv; int main Mat a=imread("cones.png",0) Mat a=imread("cones.png",0); print1; int main { print1; print2; int main { Mat a=imread("cones.png",0) print1; print2. imshow("cones",a); cvWaitKey(0); } 使用指令编译: 我们的想法是将除 main 函数外的所有 cpp 文件编译成目标文件,然后将它们链接到 maincpp。 总结:无论采用哪种编译方式,我们都需要告诉编译器我们要编译哪些 cpp,以及这些 cpp 的头文件(路径)在哪里。这些 cpp 使用的第三方库是什么(库文件名),库在哪里(库路径),库到我们的接口在哪里(库头文件路径,我们通过包含头文件编写 cpp,用接口调用相应的库实现) 事实上,我们现在可以编写一个 shell 来管理这些编译指令
-
Python OpenCV cv2.transpose 示例解析 - OpenCV 是一个编程函数库,主要用于实时计算机视觉。cv2.transpose 方法用于转置二维数组。cv::transpose 函数将图像逆时针旋转 90 度。 使用方法:cv2.cv.transpose( src[, dst] ) 参数:src:要转置矩阵的图像。dst:与 src 图像大小和深度相同的输出图像。这是一个可选参数。 返回值:返回一幅图像。 用于以下所有示例图像: 示例