Ask Your Question
3

Detect red pimples on face using openCV [closed]

asked 2015-10-03 08:44:39 -0600

galharth gravatar image

updated 2020-10-28 02:34:16 -0600

How can I detect and count how many red pimples there is on people faces using openCV(C++). For example this face has approximately 23 pimples:

image description

update

The algorithm should only detect red/pink pimples, for example the white spots in this picture are not pimples: image description

Also how can I implement face detection to prevent selecting the area of the pimples?

edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by galharth
close date 2017-05-05 07:53:59.884508

Comments

i am thinking about how to implement face detection + pimple detection. ( i don't know about profile face detection ) . if the program use a webcam and detect face and pimples real time then implemention is easy imho. could you explain what is your aimed usage of the program

sturkmen gravatar imagesturkmen ( 2015-10-04 14:39:12 -0600 )edit

The aimed usage of the program is actually an iPhone app, the user will upload image of his face and the program will estimate the severity of his acne.

galharth gravatar imagegalharth ( 2015-10-05 07:01:42 -0600 )edit
1

@galharth i am trying to do some improvements. (it was a good training to me ) keep on following this topic.

sturkmen gravatar imagesturkmen ( 2015-10-05 08:53:54 -0600 )edit

Hi, did you have any progress :) ?

galharth gravatar imagegalharth ( 2015-10-09 06:31:22 -0600 )edit

@galharth i posted edited code having some improvements.

sturkmen gravatar imagesturkmen ( 2015-10-09 07:37:43 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
6

answered 2015-10-03 12:29:56 -0600

updated 2015-10-09 13:59:00 -0600

as a challange to myself i tried to write the code below. i hope it helps .

some hints about it :

  • you must create a mask with left mouse click and move to specify the area to detect.
  • some variables manually entered like treshold parameters. it must changed for detecting another dimensions
  • it checks if detected areas are red by HSV values if(color[0] < 10 & color[1] > 70 & color[2] > 50)

sample result image :

image description

#include "opencv2/imgproc.hpp"
#include "opencv2/highgui.hpp"

using namespace cv;
using namespace std;

Mat src,cloneimg;
bool mousedown;
vector<vector<Point> > contours;
vector<Point> pts;

bool findPimples(Mat img)
{
    Mat bw,bgr[3];
    split( img,bgr );
    bw = bgr[1];
    int pimplescount = 0;

    adaptiveThreshold(bw,bw,255,ADAPTIVE_THRESH_MEAN_C,THRESH_BINARY,15,5);
    dilate(bw,bw, Mat(), Point(-1,-1),1);

    contours.clear();
    findContours( bw, contours, RETR_LIST, CHAIN_APPROX_SIMPLE );

    for( size_t i = 0; i< contours.size(); i++ )
    {

        if( contourArea(contours[i]) > 20 & contourArea(contours[i]) < 150 )
        {
            Rect minRect = boundingRect( Mat(contours[i]) );
            Mat imgroi(img,minRect);

            cvtColor(imgroi,imgroi,COLOR_BGR2HSV);
            Scalar color =mean(imgroi);
            cvtColor(imgroi,imgroi,COLOR_HSV2BGR);

            if(color[0] < 10 & color[1] > 70 & color[2] > 50)
            {
                Point2f center, vtx[4];
                float radius = 0;
                minEnclosingCircle(Mat(contours[i]), center, radius);

                if(radius < 20)
                {
                    rectangle(img,minRect,Scalar(0,255,0));
                    pimplescount++;
                }
            }
        }
    }
    putText(img, format("%d",pimplescount), Point(50, 30),FONT_HERSHEY_SIMPLEX, 0.8, Scalar(255,255,0), 2);

    imshow( "pimples dedector", img );
}


void onMouse( int event, int x, int y, int flags, void* userdata )
{
    Mat img = *((Mat *)userdata);

    if( event == EVENT_LBUTTONDOWN )
    {
        mousedown = true;
        contours.clear();
        pts.clear();
    }

    if( event == EVENT_LBUTTONUP )
    {
        mousedown = false;
        if(pts.size() > 2 )
        {
            Mat mask(img.size(),CV_8UC1);
            mask = 0;
            contours.push_back(pts);
            drawContours(mask,contours,0,Scalar(255),-1);
            Mat masked(img.size(),CV_8UC3);
            masked = Scalar(255,255,255);
            img.copyTo(masked,mask);
            cloneimg = src.clone();
            findPimples(masked);
        }
    }

    if(mousedown)
    {
        if(pts.size() > 2 )
            line(img,Point(x,y),pts[pts.size()-1],Scalar(0,255,0));

        pts.push_back(Point(x,y));

        imshow( "pimples dedector", img );
    }
}


int main( int argc, const char** argv )
{
    src = imread(argv[1]);
    if(src.empty())
    {
        return -1;
    }

    namedWindow("pimples dedector", WINDOW_AUTOSIZE);
    cloneimg = src.clone();
    setMouseCallback( "pimples dedector", onMouse, &cloneimg );
    imshow( "pimples dedector", src );

    waitKey(0);
    return 0;
}
edit flag offensive delete link more

Comments

Thank you very much! can you please describe more how can I customize this algorithm?

galharth gravatar imagegalharth ( 2015-10-04 10:55:44 -0600 )edit

@galharth you are welcome. give me more detail about images you want to detect pimples. or provide some more sample images

sturkmen gravatar imagesturkmen ( 2015-10-04 11:10:31 -0600 )edit

i wonder if selecting an area to detect is practicable? it is possible to implement a face detection + pimple dedection i think...

sturkmen gravatar imagesturkmen ( 2015-10-04 11:14:26 -0600 )edit

I updated the question. Thanks again for helping :)

galharth gravatar imagegalharth ( 2015-10-04 13:22:55 -0600 )edit
2

@both, once you selected the area, it would be quite easy to train a SVM on pixel based levels for pimple and non pimple pixels. That way you avoid setting personnal thresholds for your algorithm.

StevenPuttemans gravatar imageStevenPuttemans ( 2015-10-05 06:09:25 -0600 )edit
2

@StevenPuttemans thanks for your advice.you are right. in my implementation i only set threshols by experimental trials to show a starting point.

sturkmen gravatar imagesturkmen ( 2015-10-05 06:19:38 -0600 )edit

How can i use above code in swift ios?

kk143g gravatar imagekk143g ( 2018-07-11 05:58:49 -0600 )edit

I have successfully detected pimples through above code in Swift, how to change colors of detected pimples to skin color?

kk143g gravatar imagekk143g ( 2018-07-17 08:04:36 -0600 )edit

You will have to redefine the color thresholds set at this line if(color[0] < 10 & color[1] > 70 & color[2] > 50). Good luck!

StevenPuttemans gravatar imageStevenPuttemans ( 2018-07-23 04:06:52 -0600 )edit

@sturkmen Do you have a python version of this code? I am going to use this in python. Thank you

Eugene Agustin gravatar imageEugene Agustin ( 2018-11-05 05:38:44 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2015-10-03 08:44:39 -0600

Seen: 11,138 times

Last updated: Oct 09 '15