You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

84 lines
2.5 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package com.yuxue.easypr.core;
import org.bytedeco.javacpp.BytePointer;
import org.bytedeco.javacpp.opencv_core;
import org.bytedeco.javacpp.opencv_imgproc;
import org.bytedeco.javacpp.opencv_core.Mat;
import org.bytedeco.javacpp.opencv_core.MatVector;
import com.yuxue.enumtype.PlateColor;
public class Carcolorj {
public static String colorj( Mat src) {
String colorString="";
// 转到HSV空间进行处理颜色搜索主要使用的是H分量进行蓝色与黄色的匹配工作
Mat src_hsv = new Mat();
opencv_imgproc.cvtColor(src, src_hsv, opencv_imgproc.CV_BGR2HSV);
MatVector hsvSplit = new MatVector();
opencv_core.split(src_hsv, hsvSplit);
opencv_imgproc.equalizeHist(hsvSplit.get(2), hsvSplit.get(2));
opencv_core.merge(hsvSplit, src_hsv);
int channels = src_hsv.channels();
int nRows = src_hsv.rows();
// 图像数据列需要考虑通道数的影响;
int nCols = src_hsv.cols() * channels;
int cnt=src_hsv.cols()*src_hsv.rows();
// 连续存储的数据,按一行处理
if (src_hsv.isContinuous()) {
nCols *= nRows;
nRows = 1;
}
int green,blue,black,white,yellow;
green=black=blue=white=yellow=0;
for (int i = 0; i < src_hsv.rows(); i++) {
BytePointer p = src_hsv.ptr(i);
for (int j = 0; j < src_hsv.cols(); j=j+3) {
int H = p.get(j) & 0xFF;
int S = p.get(j + 1) & 0xFF;
int V = p.get(j + 2) & 0xFF;
if (11 < H&&H <= 34 && S > 34)
yellow += 1;
else if (H>35&&H<=99&&S>34) {
green+=1;
}
else if (H>99&&H<=124&&S>34) {
blue+=1;
}
if (H>0&&H<180 && S>0&&S<225 && V>0&&V<46)
black += 1;
else if (H>0&&H<180 && S>0&&S<43 && V>221&&V<225) {
white += 1;
}
}
}
if(yellow*2>=cnt)
colorString="黄色车辆";
else if (blue*2>=cnt) {
colorString="蓝色车辆";
}
else if (green*2>=cnt) {
colorString="绿色车辆";
}
else if (white*2>=cnt) {
colorString="白色车辆";
}
else {
colorString="黑色车辆";
}
return colorString;
}
}