Effacer les filtres
Effacer les filtres

Creating a bounding box that is not a perfect rectangle

3 vues (au cours des 30 derniers jours)
Currently, my code allows me to select a ROI in an image and then it creates a bounding box. This bounding box, however, is a rectangle while I would like the cropped image to remain as the ROI shape and not a perfect rectangle.
clear varaiables, close all
if ~exist("image_analysis.mlx")
% 1. Load the file and scale it
before = "blue_close"; white = "white_close"; after = "group1_close";
A = imresize(imread(before,'jpg'),1.00);
B = imresize(imread(white,'jpg'),1.00);
C = imresize(imread(after,'jpg'),1.00);
% % 2. manually specify the ROI using the original image
h = drawpolygon('FaceAlpha',0);
h.Color = 'yellow';
% find a bounding box of ROI to further cut the image smaller
% bbox = [xmin ymin xmax ymax]
hmin = min(h.Position); hmax = max(h.Position);
bbox = [hmin hmax-hmin];
% 3. Crop ROI for both image a and image b using the bounding box
Cb = imcrop(B,bbox);
Ca = imcrop(A,bbox);
Cc = imcrop(C,bbox);
clear A B C
save sqa.mat
load sqa.mat
  4 commentaires
Matt J
Matt J le 14 Jan 2023
Yes, certainly. Exactly how though depends on the analysis to be done.
Walter Roberson
Walter Roberson le 14 Jan 2023
For example if you were to create a mask from the ROI, and you were to double() that so that the values become numeric 0 and numeric 1 instead of logical 0 (false) and logical 1 (true), then you could pass that numeric array to regionprops as the "label" array, and pass in a grayscale image as the next parameter. You can then ask about the properties and it will only calculate the properties for the marked locations.
That said... if you were calculating a property such as Solidity which has to do with the area occupied relative the area of the bounding box, then the bounding box is still going to be calculated as a rectangle.

Connectez-vous pour commenter.

Réponse acceptée

DGM le 14 Jan 2023
As Matt says, it depends what you intend to do with the image region. If all you intend to do is some sort of color analysis, then:
% a test image
inpict = imread('peppers.png');
% create ROI object
% i'm explicitly setting the point list since i'm doing this in the forum
ROI = drawpolygon(gca,'position',[295 184;256 207;281 217;298 222;304 193]);
% create mask from the ROI, expand as necessary
mask = repmat(createMask(ROI),[1 1 size(inpict,3)]);
% get samples from selected locations
% this is a Mx3 array of RGB values
sampledpixels = reshape(inpict(mask),[],3)
sampledpixels = 962×3
255 183 0 255 186 0 255 185 0 255 186 0 255 185 0 255 181 0 255 184 0 255 182 0 255 183 0 255 181 0
Of course, this won't be of much help if you're trying to do anything spatial.

Plus de réponses (1)

Image Analyst
Image Analyst le 14 Jan 2023




Community Treasure Hunt

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

Start Hunting!

Translated by