本文介绍了如何在MATLAB中读取RAW图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我想在MATLAB中打开并阅读 .raw 图像。我的文件可以和 imrotate 为我们进行3D转置。我会旋转图像使其顺时针旋转90度,但这会使图像镜像反射到列上。然后我使用 flipdim ,第二个参数为 2 ,因为我希望镜像反映各列以获取原始图像返回。 因此,您实际需要做的是: 行= 576; COL = 768; fin = fopen('m-001-1.raw','r'); I = fread(fin,col * row * 3,'uint8 => uint8'); %//作为单字节流读入 I =重塑(I,[col row 3]); %//重塑以使其成为3D矩阵 - 请注意,这是列主要的 Ifinal = flipdim(imrotate(I,-90),2); %//聪明的转置 imshow(Ifinal); fclose(fin); %//关闭文件 我得到的是这张图片: 或者,您当然可以使用Peter的代码,但您可以使用它,这样您就可以一次重建一个颜色平面的图像。换句话说,你可以这样做: row = 576; COL = 768; fin = fopen('m-001-1.raw','r'); I1 = fread(fin,[col row],'uint8 => uint8'); %//红色通道 I2 = fread(fin,[col row],'uint8 => uint8'); %//绿色通道 I3 = fread(fin,[col row],'uint8 => uint8'); %//蓝色通道 I1 = I1。'; I2 = I2。'; I3 = I3。'; %//分别转置每个通道 Ifinal = cat(3,I1,I2,I3); %//创建3D矩阵 imshow(Ifinal); fclose(fin); 您将获得与上面完全相同的图像。 I want to open and read a .raw image in MATLAB. My file can be downloaded here. I have tried the following three code snippets, but neither gives the expected results.Code Snippet #1 row=576; col=768; fin=fopen('m-001-1.raw','r'); I=fread(fin,row*col,'uint8=>uint8'); Z=reshape(I,row,col); Z=Z'; k=imshow(Z);It shows this picture: Code Snippet #2 f=fopen('m-001-1.raw'); a=fread(f); input_img = reshape(a,768, 576, 3); input_img = imrotate(input_img, -90); imwrite(input_img, 'm-001-1.jpg'); This saves a blank (just white) image in .jpg format. Code Snippet #3 id = fopen('m-001-1.raw', 'r'); x = fread(id, [576,768], 'short');When I use imshow(x), this picture shows: How do I read this picture correctly? 解决方案 Peter's code (thanks btw!) works assuming that your image is grayscale. For colour images, what you need to do is read in all of the bytes as a single 1D array as we can't use fread to read the data is in as a 3D matrix (at least not to my knowledge...). After, we then reshape it so that it becomes a 3D matrix. After we reshape the matrix, notice that this is the transposed result. As such, we will have to either transpose each channel independently, or you can cleverly combine flipdim and imrotate to do the 3D transpose for us. I would rotate the image so that it's 90 degrees clockwise, but this would make the image mirror reflected across the columns. I then use flipdim with the second parameter as 2 as I want to mirror reflect across the columns to get the original image back.As such, what you would actually have to do is this:row=576; col=768;fin=fopen('m-001-1.raw','r');I=fread(fin, col*row*3,'uint8=>uint8'); %// Read in as a single byte streamI = reshape(I, [col row 3]); %// Reshape so that it's a 3D matrix - Note that this is column majorIfinal = flipdim(imrotate(I, -90),2); % // The clever transposeimshow(Ifinal);fclose(fin); %// Close the fileWhat I get is this image:Alternatively, you can certainly use Peter's code, but you would use it so that you would reconstruct the image one colour plane at a time. In other words, you can do this:row=576; col=768;fin=fopen('m-001-1.raw','r');I1=fread(fin, [col row],'uint8=>uint8'); %// Red channelI2=fread(fin, [col row],'uint8=>uint8'); %// Green channelI3=fread(fin, [col row],'uint8=>uint8'); %// Blue channelI1 = I1.'; I2 = I2.'; I3 = I3.'; %// Transpose each channel separatelyIfinal = cat(3, I1, I2, I3); %// Create 3D matriximshow(Ifinal);fclose(fin);You'll get exactly the same image as above. 这篇关于如何在MATLAB中读取RAW图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-26 22:21