How to store images in an array ?

25 views (last 30 days)
Jack Wolf
Jack Wolf on 25 Oct 2021
Answered: yanqi liu on 26 Oct 2021
I've already read similar topics here in the comunity, but it's not clear for me yet.
Image sizes are 480x720x3 uint8
I want to replicate the folowing code made in Python/OpenCV. For that I need to be able to store my images in an array. But no matter what I do, I dont seem to get it right.
  • PYTHON
def get_background(file_path):
cap = cv2.VideoCapture(file_path)
% we will randomly select 50 frames for the calculating the median
frame_indices = cap.get(cv2.CAP_PROP_FRAME_COUNT) * np.random.uniform(size=50)
% we will store the frames in array
frames = []
for idx in frame_indices:
% set the frame id to read that particular frame
cap.set(cv2.CAP_PROP_POS_FRAMES, idx)
ret, frame = cap.read()
frames.append(frame)
% calculate the median
median_frame = np.median(frames, axis=0).astype(np.uint8)
return median_frame
This code returns the background of a video, by selecting random 50 frames and calculating the median of those frames.
Is it possible to store images like this ?
  • MATLAB
randomframes = randi([1 numFrames],1,30); % Generate 30 random numbers from the total number of frames.
for n= 1:(30) % run through randomframes vector.
frames(n) = read(vid, randomframes(n)); % store the image from a random frame in "frames(1....n)"
end

Accepted Answer

yanqi liu
yanqi liu on 26 Oct 2021
sir,please check the follow code to get some information
clc; clear all; close all;
vid = VideoReader('xylophone.mp4');
numFrames=vid.NumberOfFrames;
% Generate 30 random numbers from the total number of frames
randomframes = randi([1 numFrames],1,30);
frames = [];
for n= 1:(30)
% run through randomframes vector.
% store the image from a random frame in "frames(1....n)"
frames(:,:,:,n) = mat2gray(read(vid, randomframes(n)));
end
figure; montage(frames, 'Size', [5 6], 'BackgroundColor', 'w', 'BorderSize', [3 3])
median_frame = median(frames,4);
figure; imshow(mat2gray(median_frame));

More Answers (1)

Image Analyst
Image Analyst on 25 Oct 2021
You can store the images in a 3-D array. Something like (untested):
redImages = zeros(rows, columns, 50, 'uint8');
greenImages = zeros(rows, columns, 50, 'uint8');
blueImages = zeros(rows, columns, 50, 'uint8');
for k = 1 : 50
rgbImage = getsnapshot(); % However you get it...
[thisR, thisG, thisB] = imsplit(rgbImage);
redImages(:, :, k) = thisR;
greenImages(:, :, k) = thisG;
blueImages(:, :, k) = thisB;
end
% Get medians for each z-column.
medRed = zeros(rows, columns, 'uint8');
medGreen = zeros(rows, columns, 'uint8');
medBlue = zeros(rows, columns, 'uint8');
for col = 1 : columns
for row = 1 : rows
medRed(row, col) = median(redImages(row, col, :));
medGreen(row, col) = median(greenImages(row, col, :));
medBlue(row, col) = median(blueImages(row, col, :));
end
end

Categories

Find more on Image Processing and Computer Vision in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!