How toimage regional analysis

2 views (last 30 days)
bozheng
bozheng on 9 Mar 2024
Edited: DGM on 9 Mar 2024
Sorry to bother you all , i have a picture but i dont konw the picture(size 869 *755) is RGB or CMYK ,the scond quesion is i will want to conduct regional analysis (can viwer can tell me how to cut two parts ----yellow and red) and bellow is i coding about get red region
img = imread('test sample.jpg');
img = rgb2hsv(img);
hue_range = [0.9, 0.1];
mask = inrange(img(:,:,1), hue_range(1), hue_range(2));
cropped_img = imcrop(img, mask);
cropped_img = hsv2rgb(cropped_img);
imshow(cropped_img);
but will say error on "mask=inrange......"
I hope someone can help me, I will sincerely thank you
  1 Comment
DGM
DGM on 9 Mar 2024
Edited: DGM on 9 Mar 2024
You didn't attach any image, so I can't tell you anything about the colorspace used by the file.
inrange() is not a function in base MATLAB or any official toolbox, so nobody knows what it is. If you don't know what it is, then I'd have to ask where you got the code. Otherwise I don't know why you'd write that line.
Similarly, that's not how imcrop() works.

Sign in to comment.

Answers (1)

DGM
DGM on 9 Mar 2024
Edited: DGM on 9 Mar 2024
If all you're trying to do is select red regions with a mask:
rgbimg = imread('peppers.png');
hsvimg = rgb2hsv(rgbimg);
hue_range = [0.9, 0.1];
mask = hsvimg(:,:,1)>=hue_range(1) | hsvimg(:,:,1)<=hue_range(2);
% this doesn't make any sense
%cropped_img = imcrop(rgbimg, mask);
imshow(mask,'border','tight');
As to what you're trying to do with imcrop(), I don't know. That's not how imcrop() is used, so it's hard for me to guess. I have two guesses.
If you want to get only the scattered pixels associated with the mask, it won't be a rectangular image anymore, so it doesn't make sense to display it.
selectedpixels = rgbimg(repmat(mask,[1 1 3]));
selectedpixels = reshape(selectedpixels,[],3); % a 3-column RGB color table
Alternatively, you could crop the image to the bounding box of the mask, but for all I know, the bounding box is the same size as the image, so I don't know if that makes sense either.
% crop both the image and mask to the bounding box of the mask
[croppedmask rows cols] = crop2box(mask);
croppedrgb = rgbimg(rows,cols,:);
imshow(croppedrgb,'border','tight');

Categories

Find more on Image Processing Toolbox in Help Center and File Exchange

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!