Ask Your Question
0

Skin Color Detection Problem [opencv4android]

asked 2013-02-20 22:26:18 -0600

baharsan gravatar image

i want to make a skin color detection, then i search a simple example to do skin color detection, but i don't know why my code is cannot run properly : this example basic skin color detection :

int main(){
Mat src = imread("qq.jpg");
if (src.empty())
    return -1;
blur( src, src, Size(3,3) );
Mat hsv;
cvtColor(src, hsv, CV_BGR2HSV);
Mat bw;
inRange(hsv, Scalar(0, 10, 60), Scalar(20, 150, 255), bw);
imshow("src", src);
imshow("dst", bw);
waitKey(0);
return 0;}

then this my android java code :

for (Rect r : hand){
            Core.rectangle(mRgba, r.tl(), r.br(), new Scalar(255, 255, 255, 255), 3);
            Mat roi = mRgba.submat(r);

            Imgproc.cvtColor(roi, roi, Imgproc.COLOR_BGR2HSV, 4);
            Imgproc.cvtColor(roi, roi, Imgproc.COLOR_RGB2RGBA, 4);

            Core.inRange(roi, new Scalar(0, 10, 60), new Scalar(20, 150, 255), roi);                            
            Mat rect_area = mRgba.rowRange(r.y, r.y+r.height).colRange(r.x, r.x+r.width);

            roi.copyTo(rect_area);  
        }

any body help me... i need this for my final project, thanks

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

answered 2013-02-21 05:43:05 -0600

berak gravatar image

updated 2013-02-21 05:44:04 -0600

inRange() takes a 3channel input, and gives a 1 channel output, right ?

i got no real idea about the java/android wrappers, but i'd say, that

Core.inRange(roi, new Scalar(0, 10, 60), new Scalar(20, 150, 255), roi);

does not do, what you expect here, you can't do it in-place. ( you probably need a fresh Mat for the output, like in the c++ example )

similar problem in the lines below: copying back a 1 channel img to a rgb one won't work, you'd need to cvtColor(src,dst,CV_GRAY2RGB); that(c++ speak), before copying it back to the original image.

edit flag offensive delete link more

Comments

I Got this New one Berak, I hacked a little,

  for (Rect r : hand){
  Core.rectangle(mRgba, r.tl(), r.br(), new Scalar(255, 255, 255, 255), 3);
  Mat roi = mRgba.submat(r);
  Mat dst = new Mat();
  Imgproc.cvtColor(roi, dst, Imgproc.COLOR_BGR2HSV_FULL, 0);
  Mat H = new Mat();
  Core.extractChannel(dst, H, 0); //this extract hsv to one channel
  Core.inRange(dst, new Scalar(0, 58, 89), new Scalar(25, 173, 229),H);//output inRange in one channel
  Imgproc.cvtColor(H, roi, Imgproc.COLOR_RGB2RGBA, 0);//H in 1 channel and roi in 3 channel, how to fix this problem ?

I still got error on this. please help me berak... :)

baharsan gravatar imagebaharsan ( 2013-02-21 16:12:45 -0600 )edit

hmm, more a c++ guy here, so bear with me if i get android things wrong, but..

Core.extractChannel(dst, H, 0); // yes, but what for ? you want a single channel made from the ranges next line, so this is kinda useless

since you're overwriting it again: Core.inRange(dst, new Scalar(0, 58, 89), new Scalar(25, 173, 229),H); // output in H

but, most important, what's the error you get ?

berak gravatar imageberak ( 2013-02-21 16:44:10 -0600 )edit

I am a noob in opencv, you can teach me altough in opencv c++. i don't know my error but it always force close when hand detected. here is all my purpose, i have hand detector using haar cascade. then roi image convert to hsv from rgb. i need to manipulate hsv image to become skin color detection.

i use this code, but still force close when hand detected :

Mat roi = mRgba.submat(r); Mat dst = new Mat(); Imgproc.cvtColor(roi, dst, Imgproc.COLOR_BGR2HSV_FULL, 0);

Mat H = new Mat();
Core.inRange(dst, new Scalar(0, 58, 89), new Scalar(25, 173, 229), H);

Imgproc.cvtColor(H, roi, Imgproc.COLOR_RGB2RGBA, 0);

Mat rect_area = mRgba.rowRange(r.y, r.y+r.height).colRange(r.x, r.x+r.width); roi.copyTo(rect_area);

baharsan gravatar imagebaharsan ( 2013-02-21 21:20:12 -0600 )edit

Hi @baharsan: Could you please tell how did you do the hand detector using haar cascade?

yourange gravatar imageyourange ( 2013-06-12 03:23:10 -0600 )edit

Question Tools

Stats

Asked: 2013-02-20 22:26:18 -0600

Seen: 6,016 times

Last updated: Feb 21 '13