Random square sampling for single channel images
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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
0 Kommentare
Antworten (2)
Subhadeep Koley
am 28 Dez. 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 Kommentare
Subhadeep Koley
am 28 Dez. 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');
Siehe auch
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!