3

The following code correctly loads an mp4 file and stores it in a 3D matrix.

r = 1;
fileName = testDummyMP4;

readerobj = VideoReader(fileName, 'tag', 'myreader1');
F = get(readerobj, 'numberOfFrames');

tampon = single(read(readerobj,1));
tampon = imresize(tampon(:,:,1),r);
I = zeros(size(tampon,1),size(tampon,2),F,'uint8');

for k = 1:F
    disp(['Open: ' num2str(round(100*k/F)) '%'])
    vidFrames = single(read(readerobj,k));
    I(:,:,k) = imresize(vidFrames(:,:,2),r);
end;

imagesc((I(:,:,1)));

This is the output enter image description here

I'm trying to reverse engineer this code so that it produces the same sort of result for but for a .raw 8bit rbg file. Following the answers from this question I tried the following:

You can download the 'M1302000245_1436389857.982603.raw' rbg file here Or Google Drive version here

Ix = 256;
Iy = 256;
SF = 30; % Sample frequency
RecordingTime = 30; 
Iz = SF*RecordingTime
testDummy = 'M1302000245_1436389857.982603.raw'

fin = fopen(testDummy, 'r');
I = fread(fin, Ix*Iy*3*Iz, 'uint8');
fclose(fin);
I = reshape(I, [Ix Iy 3 Iz]); % The rbg should be 256x256x3x900

% I've tried each of the following manipulations before calling imagesc to no avail
% I = flipdim(imrotate(I, -90),2);
% I=impixel(I)
% I=I'

imagesc((I(:,:,1))); % view first slice

This gives:

enter image description here

What am I doing wrong?

Additional info: Recordings are taken using raspberry pi cameras with the following python code

class BrainCamera:
    def __init__(self):
        self.video_format = "rgb"
        #self.video_quality = 5

        # Set up the settings of the camera so that
        # Exposure and gains are constant.
        self.camera = picamera.PiCamera()
        self.camera.resolution = (256,256)
        self.camera.framerate = 30
        sleep(2.0)
        self.camera.shutter_speed = self.camera.exposure_speed
        self.camera.exposure_mode = 'off'
        g = self.camera.awb_gains
        self.camera.awb_mode = 'off'
        self.camera.awb_gains = g
        self.camera.shutter_speed = 30000
        self.camera.awb_gains = (1,1)

    def start_recording(self, video_name_path):
        self.camera.start_recording(video_name_path, format=self.video_format)
        self.camera.start_preview()

    def stop_recording(self):
        self.camera.stop_recording()
        self.camera.stop_preview()

    # Destructor
    def __del__(self):
        print ("Closed Camera")
        self.camera.close()
Community
  • 1
  • 1
Frikster
  • 2,755
  • 5
  • 37
  • 71
  • 1
    the file in the link is not accessible as we need to register etc. – bla Jul 11 '15 at 08:36
  • I can download it without registering, but it takes 30 minutes because the hoster is awfully slow. – Daniel Jul 11 '15 at 14:24
  • I thought it would take 30 minutes because your code is reading 21 MB, but it's at 40 MB and still loading. It seems that the file either has a larger resolution or a longer duration than your code expects. – Daniel Jul 11 '15 at 14:36
  • My apologies, google drive link added. Let me know if it works. – Frikster Jul 13 '15 at 17:07
  • I don't suppose there's anything akin to " get(readerobj, 'numberOfFrames')" so that I can just have how many frames there are in the rgb file? – Frikster Jul 13 '15 at 17:12
  • Added additional code showing how the recordings are taken. Let me know if you need more than that – Frikster Jul 13 '15 at 17:21

2 Answers2

1

I don't get the output you have, but I get some reasonable image:

%your vode from above, ending with I=fread(...)
I=uint8(I)
I2=reshape(I, 3, Ix ,Iy, []);
I2=permute(I2,[3,2,1,4]);
imagesc(I2(:,:,:,1));
implay(I2);
Daniel
  • 36,610
  • 3
  • 36
  • 69
1

With Daniel's help I got the following working. Additionally a co-worker found out how to get the number of frames of a .raw rbg video. This we needed since it turns out assuming noFrame = SF*RecordingTime was a bad idea.

testDummy = 'M1302000245_1436389857.982603.raw'
testDummyInfo = dir(testDummy);
noFrames = testDummyInfo.bytes/(256*256*3); % In my question Iz = noFrames
fin = fopen(testDummy, 'r');
I = fread(fin, testDummyInfo.bytes, 'uint8');
fclose(fin);
I=uint8(I);
I2=reshape(I, 3, Ix ,Iy, noFrames);
I2=permute(I2,[3,2,1,4]);
imagesc(I2(:,:,2,1)); % Throw out unnecessarry channels r and b. Only want GFP signal

to produce:

enter image description here

Frikster
  • 2,755
  • 5
  • 37
  • 71