Question about hair removal matlab code.
Hello all! I am doing a program about hair removal. And I found a code online but part of this code I couln’t understand. The final part(the loop part) was really confused me.
img_b is a binary image and after dilation we could get the img_b binary image, which the black part is skin and the white part is hair. Thus, the ‘for’ part in the final loop is to decide if this pixel is skin or hair, if the pixel is in the skin part then it keeps the same pixel value as the original image; if the pixel is in the hair part, then do ‘else’ and calculate it for removing hair.
So here comes the question, I do not understand the calculation part in ‘else’ and the meaning of parameter r.When I change the value of r into a bigger one, the running speed gets slower. So I wonder could anyone give me a guide about this part please?when I change the value of r into a bigger one, the running speed gets slower. Thank you!
Images are listed below: the first image is which we need to deal with and the second image is the processed image.
Updated on Mar. 7th 2019: Sorry guys forgot to put the otsu function. Already put it in case anyone need it.
if true
clear;
clc;
r=7;
se1_para = 3;
se2_para = 2;
img_old = imread(‘D:MATLAB1binpicsam3-2.jpg’);
figure(1),imshow(img_old);
[x,y,z] = size(img_old);
img = rgb2gray(img_old);
se1 = strel(‘disk’,se1_para);
img_c = imclose(img,se1)
figure(2), imshow(img_c,[]);
img_fur = double(img_c) – double(img);
figure(3),imshow(img_fur,[]);
[X Y]=meshgrid(1:x);
tt=(X-280).^2+(Y-280).^2<280^2;
thresh = otsu(img_fur(tt),sum(tt(:)));
img_b = (img_fur>thresh);
figure(4),imshow(img_b);
se2 = strel(‘disk’,se2_para);
img_b =imdilate(img_b,se2);
figure(5),imshow(img_b);
img_new = uint8(zeros(x,y,z));
for i = 1:x
for j = 1:y
if img_b(i,j) == false
img_new(i,j,:) = img_old(i,j,:);
else
ttt = img_old(max(1,i-r):min(i+r,x),max(j-r,1):min(j+r,y),:);
no_efficient_pix = cat(3,img_b(max(1,i-r):min(i+r,x),max(j-r,1):min(j+r,y)),not(tt(max(1,i-r):min(i+r,x),max(j-r,1):min(j+r,y))));
no_efficient_pix = any(no_efficient_pix,3);
ttt = ttt.*repmat(uint8(not(no_efficient_pix)),[1,1,3]);
efficient_pix_num = (2*r+1)^2-sum(no_efficient_pix(:));
img_new(i,j,:) = uint8(sum(sum(ttt))./efficient_pix_num);
end
end
end
figure(6),imshow(img_new,[]);
end
function thresh = otsu(data,pix_num)
img_var = zeros(256,1);
for i=1:256
w0 = sum(sum(data<=i-1))./pix_num;
w1 = 1-w0;
u0 = sum(sum(data.*double(data<=i-1)))./(w0*pix_num);
u1 = sum(sum(data.*double(data>i-1)))./(w1*pix_num);
img_var(i) = w0.*w1.*((u0-u1).^2);
end
[~,I] = max(img_var);
thresh = I-1;
endHello all! I am doing a program about hair removal. And I found a code online but part of this code I couln’t understand. The final part(the loop part) was really confused me.
img_b is a binary image and after dilation we could get the img_b binary image, which the black part is skin and the white part is hair. Thus, the ‘for’ part in the final loop is to decide if this pixel is skin or hair, if the pixel is in the skin part then it keeps the same pixel value as the original image; if the pixel is in the hair part, then do ‘else’ and calculate it for removing hair.
So here comes the question, I do not understand the calculation part in ‘else’ and the meaning of parameter r.When I change the value of r into a bigger one, the running speed gets slower. So I wonder could anyone give me a guide about this part please?when I change the value of r into a bigger one, the running speed gets slower. Thank you!
Images are listed below: the first image is which we need to deal with and the second image is the processed image.
Updated on Mar. 7th 2019: Sorry guys forgot to put the otsu function. Already put it in case anyone need it.
if true
clear;
clc;
r=7;
se1_para = 3;
se2_para = 2;
img_old = imread(‘D:MATLAB1binpicsam3-2.jpg’);
figure(1),imshow(img_old);
[x,y,z] = size(img_old);
img = rgb2gray(img_old);
se1 = strel(‘disk’,se1_para);
img_c = imclose(img,se1)
figure(2), imshow(img_c,[]);
img_fur = double(img_c) – double(img);
figure(3),imshow(img_fur,[]);
[X Y]=meshgrid(1:x);
tt=(X-280).^2+(Y-280).^2<280^2;
thresh = otsu(img_fur(tt),sum(tt(:)));
img_b = (img_fur>thresh);
figure(4),imshow(img_b);
se2 = strel(‘disk’,se2_para);
img_b =imdilate(img_b,se2);
figure(5),imshow(img_b);
img_new = uint8(zeros(x,y,z));
for i = 1:x
for j = 1:y
if img_b(i,j) == false
img_new(i,j,:) = img_old(i,j,:);
else
ttt = img_old(max(1,i-r):min(i+r,x),max(j-r,1):min(j+r,y),:);
no_efficient_pix = cat(3,img_b(max(1,i-r):min(i+r,x),max(j-r,1):min(j+r,y)),not(tt(max(1,i-r):min(i+r,x),max(j-r,1):min(j+r,y))));
no_efficient_pix = any(no_efficient_pix,3);
ttt = ttt.*repmat(uint8(not(no_efficient_pix)),[1,1,3]);
efficient_pix_num = (2*r+1)^2-sum(no_efficient_pix(:));
img_new(i,j,:) = uint8(sum(sum(ttt))./efficient_pix_num);
end
end
end
figure(6),imshow(img_new,[]);
end
function thresh = otsu(data,pix_num)
img_var = zeros(256,1);
for i=1:256
w0 = sum(sum(data<=i-1))./pix_num;
w1 = 1-w0;
u0 = sum(sum(data.*double(data<=i-1)))./(w0*pix_num);
u1 = sum(sum(data.*double(data>i-1)))./(w1*pix_num);
img_var(i) = w0.*w1.*((u0-u1).^2);
end
[~,I] = max(img_var);
thresh = I-1;
end Hello all! I am doing a program about hair removal. And I found a code online but part of this code I couln’t understand. The final part(the loop part) was really confused me.
img_b is a binary image and after dilation we could get the img_b binary image, which the black part is skin and the white part is hair. Thus, the ‘for’ part in the final loop is to decide if this pixel is skin or hair, if the pixel is in the skin part then it keeps the same pixel value as the original image; if the pixel is in the hair part, then do ‘else’ and calculate it for removing hair.
So here comes the question, I do not understand the calculation part in ‘else’ and the meaning of parameter r.When I change the value of r into a bigger one, the running speed gets slower. So I wonder could anyone give me a guide about this part please?when I change the value of r into a bigger one, the running speed gets slower. Thank you!
Images are listed below: the first image is which we need to deal with and the second image is the processed image.
Updated on Mar. 7th 2019: Sorry guys forgot to put the otsu function. Already put it in case anyone need it.
if true
clear;
clc;
r=7;
se1_para = 3;
se2_para = 2;
img_old = imread(‘D:MATLAB1binpicsam3-2.jpg’);
figure(1),imshow(img_old);
[x,y,z] = size(img_old);
img = rgb2gray(img_old);
se1 = strel(‘disk’,se1_para);
img_c = imclose(img,se1)
figure(2), imshow(img_c,[]);
img_fur = double(img_c) – double(img);
figure(3),imshow(img_fur,[]);
[X Y]=meshgrid(1:x);
tt=(X-280).^2+(Y-280).^2<280^2;
thresh = otsu(img_fur(tt),sum(tt(:)));
img_b = (img_fur>thresh);
figure(4),imshow(img_b);
se2 = strel(‘disk’,se2_para);
img_b =imdilate(img_b,se2);
figure(5),imshow(img_b);
img_new = uint8(zeros(x,y,z));
for i = 1:x
for j = 1:y
if img_b(i,j) == false
img_new(i,j,:) = img_old(i,j,:);
else
ttt = img_old(max(1,i-r):min(i+r,x),max(j-r,1):min(j+r,y),:);
no_efficient_pix = cat(3,img_b(max(1,i-r):min(i+r,x),max(j-r,1):min(j+r,y)),not(tt(max(1,i-r):min(i+r,x),max(j-r,1):min(j+r,y))));
no_efficient_pix = any(no_efficient_pix,3);
ttt = ttt.*repmat(uint8(not(no_efficient_pix)),[1,1,3]);
efficient_pix_num = (2*r+1)^2-sum(no_efficient_pix(:));
img_new(i,j,:) = uint8(sum(sum(ttt))./efficient_pix_num);
end
end
end
figure(6),imshow(img_new,[]);
end
function thresh = otsu(data,pix_num)
img_var = zeros(256,1);
for i=1:256
w0 = sum(sum(data<=i-1))./pix_num;
w1 = 1-w0;
u0 = sum(sum(data.*double(data<=i-1)))./(w0*pix_num);
u1 = sum(sum(data.*double(data>i-1)))./(w1*pix_num);
img_var(i) = w0.*w1.*((u0-u1).^2);
end
[~,I] = max(img_var);
thresh = I-1;
end hair removal MATLAB Answers — New Questions