"JAVA" image recognition--HSV skin tone extraction

Source: Internet
Author: User
Tags float max

Oschina See all kinds of language of the girl figure of the program section, take to run a run, are crawler mechanism, and address is generally fixed, format fixed, to catch the figure, which shows not enough intelligence, so the author of the code to change into a download image of the crawler. Then the problem came, a lot of pictures, not what I want, this thought of the image recognition, the main branches have, look for similar figures, face recognition, Kam Yellow and so on.

Today to talk about skin color extraction, probably exposed, I want to choose what branch, not much to say, not much to say >_<!

Skin Tone Extraction

Started using a Java code written by a great God on csdn (to detect a yellow image), using the YUV color space. The effect is still very good.

/**  * flesh  *   *  @param  c  *  @return    */ public static boolean isflesh (final color c)  {  if  ((c.getred ()  > 230)  &&  (C.getgreen ()  > 170)  &&  ( C.getblue ()  > 190))  {   return false;  }  ldialyzer &NBSP;YUV&NBSP;=&NBSP;LDIALYZER.GETYUV (c.getred (),  c.getgreen (),  c.getblue ());   return   ((c.getred ()  > 40)  &&  (C.getgreen ()  > 40)  &&   (yuv.y + 16 > 145)     &&  (yuv.v + 128  < 173)  &&  (yuv.v + 128 > 133)      &&  (yuv.u + 128 < 127)  &&  (yuv.u + 128  > 77));  }

But this code, the upper part of the RGB range directly pass off part, this is determined to be a bit decisive, carefully observe the RGB color space, you will find that there is still a part of the yellow is excluded. Then consider using the HSV color space.

HSV Hexagonal Pyramid

The h parameter represents the color information, where the spectral color is located. This parameter is indicated by an angle amount, the red, green, and blue are separated by 120 degrees respectively. The complementary colors differ by 180 degrees respectively.

Purity S is a proportional value ranging from 0 to 1, which represents the ratio between the purity of the selected color and the maximum purity of the color. When S=0, only grayscale.

V indicates how bright the color is, ranging from 0 to 1. One thing to note: There is no direct link between it and light intensity.

RGB Conversion to HSV algorithm

Max=max (R,G,B)

Min=min (R,G,B)

If R = max, H = (g-b)/(Max-min)

If G = max, H = 2 + (b-r)/(Max-min)

If B = max, H = 4 + (R-G)/(Max-min)

H = h * 60

If h < 0, H = H + 360

V=max (R,G,B)

S= (max-min)/max

Algorithm of HSV conversion to RGB

If s = 0

R=g=b=v

Else

H/= 60;

i = INTEGER (H)

f = h-i

A = V * (1-s)

b = V * (1-s * f)

c = V * (1-s * (1-f))

Switch (i)

Case 0:r = V; G = C; B = A;

Case 1:r = b; G = v; B = A;

Case 2:r = A; G = v; B = C;

Case 3:r = A; G = b; B = v;

Case 4:r = c; G = A; B = v;

Case 5:r = v; G = A; b = b;

by algorithm, write Java implementation

public static HSV RGB2HSV (RGB RGB) {Float r = (float) rgb.getr ()/255;  float g = (float) RGB.GETG ()/255;  Float B = (float) rgb.getb ()/255;  float max = max (R, G, b);  float min = min (R, G, b);  float h = 0;  if (r==max) H = (g-b)/(Max-min);  if (g==max) H = (b-r)/(Max-min);  if (B==max) h= 4+ (r-g)/(Max-min);  H *=60;  if (h<0) H +=360;  HSV HSV = new HSV (h, (max-min)/max,max); return HSV; }

For skin color recognition saturation (S) and luminance (V) It doesn't matter, so you just get a range of values for the Hue (H).

From the Internet to find the value of H range, probably in the 25~50, in order to determine the value of the next step, did the following experiments.

First buckle some beautiful figure, as long as the meat, try to choose a difference.

Java Implementation Statistics

Public static int[] vessel = new int[360]; public static int[]  vesselindex = new int[360];  public static void main (String[]  args)  throws ioexception {  file file = new file ("d:\\ culture material" );    file[] listfiles = file.listfiles ();  arraylist

Result: (slightly off count=0)

h=15,count:31071

h=18,count:26936

h=16,count:24615

h=13,count:24031

h=17,count:21968

h=12,count:21211

h=30,count:19438

h=38,count:16740

h=14,count:16470

h=33,count:16404

h=32,count:16217

h=28,count:15231

h=35,count:14929

h=20,count:14714

h=31,count:14353

h=36,count:13654

h=29,count:13515

h=21,count:13311

h=34,count:13133

h=19,count:12595

h=26,count:11921

h=10,count:11062

h=37,count:10669

h=11,count:10422

h=27,count:9726

h=22,count:9010

h=25,count:8629

h=24,count:8548

h=40,count:8375

h=23,count:8240

h=39,count:7295

h=41,count:4262

h=43,count:3365

h=0,count:3229

h=9,count:2628

h=60,count:1983

h=42,count:1469

h=8,count:1453

h=7,count:927

h=44,count:862

h=45,count:742

h=180,count:515

h=51,count:354

h=48,count:263

h=240,count:221

h=330,count:210

h=6,count:198

h=47,count:168

h=50,count:147

h=56,count:137

h=5,count:134

h=63,count:125

h=52,count:116

H=46,count:90

h=69,count:69

h=220,count:59

h=76,count:57

H=70,count:50

H=77,count:44

h=4,count:41

H=64,count:36

H=184,count:32

H=75,count:32

H=72,count:30

H=49,count:29

H=354,count:27

H=353,count:26

H=280,count:25

H=2,count:25

H=150,count:24

H=120,count:23

H=68,count:23

H=352,count:19

H=350,count:17

H=3,count:16

H=55,count:15

H=54,count:14

H=90,count:13

H=65,count:12

H=79,count:11

H=357,count:11

H=210,count:10

H=351,count:10

H=251,count:10

H=74,count:9

H=356,count:9

H=53,count:9

H=190,count:8

H=67,count:8

H=300,count:8

H=73,count:8

H=348,count:8

H=57,count:8

H=185,count:7

H=345,count:7

H=83,count:7

H=78,count:7

H=66,count:7

H=355,count:6

H=188,count:6

H=228,count:6

H=100,count:5

H=340,count:5

H=336,count:4

H=85,count:4

H=84,count:4

H=171,count:3

H=186,count:3

H=173,count:3

H=140,count:3

H=195,count:3

H=349,count:3

H=105,count:3

H=108,count:2

H=174,count:2

H=96,count:2

H=182,count:2

H=183,count:2

H=82,count:2

H=95,count:2

H=165,count:2

H=170,count:2

H=189,count:2

H=106,count:2

H=358,count:2

H=260,count:1

H=264,count:1

H=94,count:1

H=144,count:1

H=88,count:1

H=1,count:1

H=166,count:1

H=342,count:1

H=187,count:1

H=168,count:1

H=110,count:1

H=114,count:1

H=192,count:1

H=172,count:1

H=92,count:1

H=128,count:1

H=175,count:1

H=176,count:1

H=249,count:1

H=135,count:1


Analyze the data, the range of H is probably between 9~43

Verify the above analysis

public static void Main (string[] args) throws IOException {

BufferedImage DST = new BufferedImage (100, 360 * 5,

BUFFEREDIMAGE.TYPE_INT_RGB);

for (int i = 0; i <; i++) {

for (int j = 0; J < * 5; j + +) {

for (int j = 0; J < * 5; j + +) {

Dst.setrgb (i, J, Colorutils.rgb2binary (Colorutils.hsv2rgb (New HSV (J/5, 1, 1)));

}

}

Imageio.write (DST, "JPG", New File ("d:\\hsv1.jpg"));

}

Result (skip the not drawn section)


H range [0,50), very show the above data, can be cut off 10%~30% up and down. This is when s,v are equal to 1 o'clock images, try to modify the values of S and V, range in [0,1], can match to light and other problems, resulting in a lighter or darker image. In the case of skin color matching, do not consider S and V, so that accuracy increases.

Judging Fresh Meat

public static Boolean IsFlesh2 (Color c) {
RGB RGB = new RGB (c.getred (), C.getgreen (), C.getblue ());
HSV HSV = COLORUTILS.RGB2HSV (RGB);
if (Hsv.geth () >9&&hsv.geth () <43) {
return true;
}
return false;
}

"JAVA" image recognition--HSV skin tone extraction

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.