필터 지우기
필터 지우기

Creating a bounding box that is not a perfect rectangle

조회 수: 4 (최근 30일)
ISABELLE GUNDRUM
ISABELLE GUNDRUM 2023년 1월 14일
답변: Image Analyst 2023년 1월 14일
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
imshow(A)
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
else
load sqa.mat
end
Thanks!
  댓글 수: 4
Matt J
Matt J 2023년 1월 14일
Yes, certainly. Exactly how though depends on the analysis to be done.
Walter Roberson
Walter Roberson 2023년 1월 14일
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.

댓글을 달려면 로그인하십시오.

채택된 답변

DGM
DGM 2023년 1월 14일
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
imshow(inpict)
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.

추가 답변 (1개)

Image Analyst
Image Analyst 2023년 1월 14일

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by