How to use loops to scan through pixels and black out the pixels outside a shape with a (white) perimeter (image)
I am attempting to use loops to check each pixel for whether or not its within a perimeter of white. Essentially I want to keep whats inside the white perimeter while blacking out everything else. I would like to do this using a loops rather than a mask if possible so I can apply this to other similar images (Different skin colors with same white perimeter). Below I have an image listed for example. In my attempt to loop through I found I could not make the loop skip over the area between the white lines and everything but the white parts would be blacked out. Below is the code I used to try and do this:
newI =(imread(nameF));
figure;
imshow(newI);
newB = newI(:,:,1);
for r = 1:size(newB,1)
for c = 1:size(newB,2)
pixel = newB(r,c);
current = c;
if pixel == 255
for cc = current:size(newB,2)
pixel2=(newB(r,cc));
if pixel2 == 255
skip = cc;
break
end
end
else
newI(r, c, 1) = 0;
newI(r, c, 2) = 0;
newI(r, c, 3) = 0;
end
c = skip;
end
end
figure
imshow(newI)I am attempting to use loops to check each pixel for whether or not its within a perimeter of white. Essentially I want to keep whats inside the white perimeter while blacking out everything else. I would like to do this using a loops rather than a mask if possible so I can apply this to other similar images (Different skin colors with same white perimeter). Below I have an image listed for example. In my attempt to loop through I found I could not make the loop skip over the area between the white lines and everything but the white parts would be blacked out. Below is the code I used to try and do this:
newI =(imread(nameF));
figure;
imshow(newI);
newB = newI(:,:,1);
for r = 1:size(newB,1)
for c = 1:size(newB,2)
pixel = newB(r,c);
current = c;
if pixel == 255
for cc = current:size(newB,2)
pixel2=(newB(r,cc));
if pixel2 == 255
skip = cc;
break
end
end
else
newI(r, c, 1) = 0;
newI(r, c, 2) = 0;
newI(r, c, 3) = 0;
end
c = skip;
end
end
figure
imshow(newI) I am attempting to use loops to check each pixel for whether or not its within a perimeter of white. Essentially I want to keep whats inside the white perimeter while blacking out everything else. I would like to do this using a loops rather than a mask if possible so I can apply this to other similar images (Different skin colors with same white perimeter). Below I have an image listed for example. In my attempt to loop through I found I could not make the loop skip over the area between the white lines and everything but the white parts would be blacked out. Below is the code I used to try and do this:
newI =(imread(nameF));
figure;
imshow(newI);
newB = newI(:,:,1);
for r = 1:size(newB,1)
for c = 1:size(newB,2)
pixel = newB(r,c);
current = c;
if pixel == 255
for cc = current:size(newB,2)
pixel2=(newB(r,cc));
if pixel2 == 255
skip = cc;
break
end
end
else
newI(r, c, 1) = 0;
newI(r, c, 2) = 0;
newI(r, c, 3) = 0;
end
c = skip;
end
end
figure
imshow(newI) image processing MATLAB Answers — New Questions