I do not know how to make this condition on the hue work

1 visualizzazione (ultimi 30 giorni)
I do not understand how to make this condition work to segment my image
Hello,
What I want to do is setting to zero all pixels whose hue value is distant of more than 0.1 from the value of the pixel I have selected using ginput(), however the results I have with my code are far from that. Many pixels whose value are still more than 0.1 away from the desired value remain with non-zero value. Depending on the selected pixel with ginput(), even the selected pixel can be set to zero with this code.
clear ; close all ;
imagea = imread(imagetoanalyze.jpg') ;
imaged = double(imagea) ;
imghsv=rgb2hsv(imaged);
Hue=(imghsv(:,:,1));
figure(1) ; image(imagea) ; drawnow ; axis('image') ;
a=round(ginput(1));
huev(1,1)=(Hue(a(1,1),a(1,2)));
for row=1:size(imagea,1)
for col=1:size(imagea,2)
if Hue(row,col)>huev+0.1;
imghsv(row,col,:)=(0);
if Hue(row,col)<huev-0.1;
imghsv(row,col,:)=(0);
end
end
end
end
image2=uint8(hsv2rgb(imghsv));figure(1) ; image(image2) ;
drawnow ; axis('image') ;
I assume the code that doesn't work is in this code section:
for row=1:size(imagea,1)
for col=1:size(imagea,2)
if Hue(row,col)>huev+0.1;
imghsv(row,col,:)=(0);
if Hue(row,col)<huev-0.1;
imghsv(row,col,:)=(0);
As suggested on another place, I asked I tried to replace the code section above with:
for row=1:size(imagea,1)
for col=1:size(imagea,2)
if abs(Hue(row,col) - huev) < 0.1;
imghsv(row,col,:)=0;
end
end
end
But this still doesn't work as I want it to work and I'm unable to solve this.
Thanking you in advance for your help,

Risposte (2)

Voss
Voss il 10 Lug 2022
Modificato: Voss il 10 Lug 2022
What if you try this:
for row=1:size(imagea,1)
for col=1:size(imagea,2)
% if abs(Hue(row,col) - huev) < 0.1;
if abs(Hue(row,col) - huev) > 0.1 % distance *more* than 0.1 -> set to 0
imghsv(row,col,:)=0;
end
end
end
Or this, which is what I think you meant with the two if statements (but one was inside the other):
for row=1:size(imagea,1)
for col=1:size(imagea,2)
% two separate, independent if statements
if Hue(row,col)>huev+0.1;
imghsv(row,col,:)=(0);
end
if Hue(row,col)<huev-0.1;
imghsv(row,col,:)=(0);
end
end
end
Or this:
for row=1:size(imagea,1)
for col=1:size(imagea,2)
% one if statement with two conditions
if Hue(row,col)>huev+0.1 || Hue(row,col)<huev-0.1
imghsv(row,col,:)=(0);
end
end
end

DGM
DGM il 11 Lug 2022
Modificato: DGM il 11 Lug 2022
You don't need any loops. You could just do:
% no loops necessary
mask = Hue>huev+0.1 | Hue<huev-0.1;
imghsv(repmat(mask,[1 1 3])) = 0;
... or you could avoid a lot of waste and rounding error by applying the mask to the working RGB image instead of the temporary HSV copy used for mask generation.
Besides that, you're misusing double() and uint8(), so you're going to be creating a bunch of improperly-scaled images and your output could easily become unusable if you're not careful with how you handle them while they're improperly-scaled. In this case, neither conversion is actually needed. If it were necessary to change class, the images should be properly scaled to match their class. Casting alone does not scale the data. That's what im2double(), im2uint8() are for.
imgrgb = imread('peppers.png');
imghsv = rgb2hsv(imgrgb);
Hue = imghsv(:,:,1);
figure(1); image(imgrgb); drawnow; axis('image');
% get a H sample
a = round(ginput(1));
huesample = Hue(a(1,1),a(1,2));
% no loops necessary
mask = Hue>huesample+0.1 | Hue<huesample-0.1;
outputimg = imgrgb;
outputimg(repmat(mask,[1 1 3])) = 0;
figure(1); image(outputimg); drawnow; axis('image');

Categorie

Scopri di più su Graphics Performance in Help Center e File Exchange

Prodotti


Release

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by