how to sample some random patches of an image?

3 ビュー (過去 30 日間)
farnaz fanaee
farnaz fanaee 2016 年 11 月 30 日
回答済み: Ayush 2024 年 10 月 7 日
i want to sample some rrandom patches from an face image

回答 (1 件)

Ayush
Ayush 2024 年 10 月 7 日
Hi,
You can follow bellow steps to sample random patches from the image:
  1. Load image in MATLAB.
  2. Specify the size of the patches you want to extract.
  3. Generate random coordinates to extract patches.
  4. Use the coordinates to extract patches from the image.
Refer to the below pseudo code for a better understanding:
% Read the image
image = imread('face_image.jpg'); % Replace with your image file
% Convert to grayscale if the image is colored
if size(image, 3) == 3
image = rgb2gray(image);
end
% Define the patch size
patchSize = [50, 50]; % Example patch size of 50x50 pixels
% Get the size of the image
[imageHeight, imageWidth] = size(image);
% Number of patches you want to sample
numPatches = 5;
% Initialize a cell array to hold patches
patches = cell(1, numPatches);
% Randomly sample patches
for i = 1:numPatches
% Randomly select the top-left corner of the patch
x = randi([1, imageWidth - patchSize(2) + 1]);
y = randi([1, imageHeight - patchSize(1) + 1]);
% Extract the patch
patch = image(y:y+patchSize(1)-1, x:x+patchSize(2)-1);
% Store the patch
patches{i} = patch;
end
% Display the patches
figure;
for i = 1:numPatches
subplot(1, numPatches, i);
imshow(patches{i});
title(['Patch ', num2str(i)]);
end

カテゴリ

Help Center および File ExchangePolygons についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by