Question about hair removal matlab code.

5 vues (au cours des 30 derniers jours)
wei huang
wei huang le 9 Nov 2016
Commenté : SOMNATH BAKSI le 19 Jan 2025
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:\MATLAB1\bin\picsam\03-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
  6 commentaires
Image Analyst
Image Analyst le 20 Nov 2019
J, attach your image and code so we can run it and fix it.
SARAH LONER
SARAH LONER le 20 Nov 2019
this are my images sir nd i have used the above code for removing hair from skin. please help me on on this

Connectez-vous pour commenter.

Réponses (2)

Luis Enciso
Luis Enciso le 13 Juil 2021
I fixed the given code in this way, it works fine in squared cropped images:
close all
clear;
clc;
r=7;
se1_para = 3;
se2_para = 2;
img_old = imread('image.jpg');
figure(1),imshow(img_old);
% crop
[x,y,z] = size(img_old);
d = y-x;
im_crop = img_old(:,round(d/2)+1:y-(d-round(d/2)),:);
[x,y,z] = size(im_crop);
img = rgb2gray(im_crop);
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-x/2).^2+(Y-y/2).^2<(x+y)^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,:) = im_crop(i,j,:);
else
ttt = im_crop(max(1,i-r):min(i+r,x),max(j-r,1):min(j+r,y),:); % region
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,[]);
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

SOMNATH BAKSI
SOMNATH BAKSI le 19 Jan 2025
Check out the latest method , I uploaded in Mathworks fileexchange
Advanced Techniques for Hair Removal in Dermatological Image
https://in.mathworks.com/matlabcentral/fileexchange/179219-advanced-techniques-for-hair-removal-in-dermatological-image

Catégories

En savoir plus sur Introduction to Installation and Licensing dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by