Random square sampling for single channel images

조회 수: 6 (최근 30일)
len Bruce
len Bruce 2020년 12월 28일
답변: len Bruce 2020년 12월 28일
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

답변 (2개)

Subhadeep Koley
Subhadeep Koley 2020년 12월 28일
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
len Bruce
len Bruce 2020년 12월 28일
Very good, but your square sampling is a set fixed window size, what I want is to generate a square area based on the coordinates of the image formed by random numbers,the sampled square area appears randomly in the image and square size is variable,such as image(x1:x2, y1:y2)
Subhadeep Koley
Subhadeep Koley 2020년 12월 28일
@len Bruce you can randomize the sqare window size like below.
% 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
len Bruce 2020년 12월 28일
Generate the coordinates of the image square sampling based on random numbers,such as the upper left and the lower right co-ordinates of the ith random sub-window is denoted by (x1,y1)and (x2,y2)respectively. (image(x1:x2, y1:y2))

카테고리

Help CenterFile Exchange에서 Geometric Transformation and Image Registration에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by