Creating a Colour Mask for an Image in HSV

조회 수: 41 (최근 30일)
Isabelle Davies
Isabelle Davies 2022년 7월 30일
편집: Isabelle Davies 2022년 8월 2일
Hi there.
I'm trying to create a mask for an image of these three balloons to filter the red balloon, but with HSV rather than RGB. I've managed to do it with RGB (as seen below), but evey attempt at doing it with HSV values has failed.
balloons = imread('balloons.png');
size(balloons)
class(balloons)
imshow(balloons)
mask = balloons(:,:,1) > 80 & balloons(:,:,2) < 100 & balloons(:,:,3) < 100
imshow(mask)
This is what I tried next:
balloons_HSV = rgb2hsv(balloons) ;
This didn't work, so then:
balloons_h = rgb2hsv(80)
balloons_s = rgb2hsv(100)
balloons_v = rgb2hsv(100)
mask_hsv = balloons_HSV(:,:,1) > balloons_h & balloons_HSV(:,:,2) < balloons_s & balloons_HSV(:,:,3) < balloons_v
But this didn't work either, so now I'm out of ideas. Help would be much appreciated!
**Edit: For some reason I forgot that the hue value for pure red is 0, and so when I used rgb2hsv for certain points on the red balloon, I assumed something had gone wrong, but everything was actually correct, and there wasn't anything wrong with my code, but I am still grateful someone tried to provide a solution.

채택된 답변

Image Analyst
Image Analyst 2022년 7월 30일
Did you try the Color Thresholder app on the Apps tab of the tool ribbon?
Or see my demo:
  댓글 수: 2
Isabelle Davies
Isabelle Davies 2022년 7월 30일
편집: Isabelle Davies 2022년 7월 30일
Hi, it's less about the final product and just knowing how it's done. It was an exercise in a class I'm taking and I feel like it might be helpful for my upcoming exam. Do you still think your demo would be helpful?
Image Analyst
Image Analyst 2022년 7월 30일
Yes, run my demo and see the histograms for every color channel and try to see where you'd put the thresholds to get just the red. It will probably be something like the hue is less than 0.1 or greater than 0.9 and the saturation greater than 0.25. But look at the histograms to see where it makes sense to split (threshold) the histogram at.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Graphics Performance에 대해 자세히 알아보기

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by