Random square sampling for single channel images
Mostrar comentarios más antiguos
For a single-channel image with random square sampling, Not a fixed sub-window,how to generate random squares usingMATLAB, an example is as follows,Thank you

Respuestas (2)
Subhadeep Koley
el 28 de Dic. de 2020
Hi, here is one working example.
Hope this helps!
% Read the original image
img = imread('cameraman.tif');
[row, col, ~] = size(img);
imgSize = [row, col];
% Specify dimension of the random square
squareSize = [50, 50];
% Calculate crop rectangle
maximumPosValue = (imgSize - squareSize + 1);
initialRandomPos = [randi(maximumPosValue(1)), randi(maximumPosValue(2))];
cropRect = [initialRandomPos(2), initialRandomPos(1), squareSize(2)-1, squareSize(1)-1];
% Crop the image
imgCropped = imcrop(img, cropRect);
% Visualize results
out = imtile({img, imgCropped}, 'BackgroundColor', 'w');
figure
imshow(out)
title('Original image | Random cropped image');
2 comentarios
len Bruce
el 28 de Dic. de 2020
Subhadeep Koley
el 28 de Dic. de 2020
% Read the original image
img = imread('peppers.png');
[row, col, ~] = size(img);
imgSize = [row, col];
% Specify dimension of the random square
val = randi(min(imgSize));
squareSize = [val, val];
% Calculate crop rectangle
maximumPosValue = (imgSize - squareSize + 1);
initialRandomPos = [randi(maximumPosValue(1)), randi(maximumPosValue(2))];
cropRect = [initialRandomPos(2), initialRandomPos(1), squareSize(2)-1, squareSize(1)-1];
% Crop the image
imgCropped = imcrop(img, cropRect);
% Visualize results
out = imtile({img, imgCropped}, 'BackgroundColor', 'w');
figure
imshow(out)
title('Original image | Random cropped image');
len Bruce
el 28 de Dic. de 2020
0 votos
Categorías
Más información sobre Import, Export, and Conversion en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!