JAVA+OPENCV Color Recognition
Recently in the project involved in the image recognition, the use of third-party detection is OPENCV, in view of the online use of Java to do a very small amount of information, so I wrote this blog, I hope the students a little help later!
Note: Color extraction on the picture requires a higher, if your picture is more ambiguous (hundreds of KB, 2M is no problem), please do not waste time ,
This is written about the color of the extraction ——-original artwork:
here is the red extract:
1, get the Picture object mat
Mat hsv_image = new Mat (Mat.size (), Mat.type ());
1 Convert the format of the picture to HSV mode, originally RGB ***imgproc.cvtcolor (Mat, Hsv_image, IMGPROC.COLOR_BGR2HSV); * * Hue (H), saturation (S), lightness (V)
The following can be used to extract the color of the int num_rows = Hsv_image.rows ();
int num_col = Hsv_image.cols ();
for (int i = 0; i < num_rows. i++) {for (int j = 0; J < Num_col; J +) {//Get each pixel
double[] Clone = Hsv_image.get (i, J). Clone (); Double hun = clone[0]; HSV hun if (Hun >= 0 && Hun < 34) | | (Hun > 160 && Hun < 200)) {if (clone[1] > && clone[1] < () {if (clone[2) ; && Clone[2] > 90 {//Red range, all set to black, clone[
0] = 0;
CLONE[1] = 0;
CLONE[2] = 0;
Hsv_image.put (I, J, clone); } else {CLone[0] = 255;
CLONE[1] = 255;
CLONE[2] = 255;
Hsv_image.put (I, J, clone);
}
}
} }
Finally get a picture of the red number set to black, the border and so on all become white, (the above code to perform the effect is not a black box) black border is my additional effect, red color value H about 0 ~ 10 | | 165 ~ 180, please set the threshold according to the requirement
The color values extracted above can be set at your own discretion.
Basically is this, the typesetting effect is not good do not spray ha, thank you!