我需要一些代码帮助。我想写一些类似于: LINK
我从前置手机摄像头拍了两张小的.png照片。我对calc直方图有疑问。有代码:
Mat img=new Mat();
img=org.opencv.highgui.Highgui.imread(imgLocation);
List<Mat> imagesList=new ArrayList<Mat>();
imagesList.add(img);
int channelArray[]={0,1,2};
MatOfInt channels=new MatOfInt(channelArray);
Mat hist=new Mat();
MatOfInt histSize=new MatOfInt(256);
MatOfFloat ranges=new MatOfFloat(0.0f,255.0f);
org.opencv.imgproc.Imgproc.calcHist(imagesList, channels,new Mat(), hist, histSize, ranges);
return hist;
我收到了一个错误:
12-10 02:37:53.157: E/cv::error()(6203): OpenCV Error: Assertion failed (csz == 0 || csz == dims) in void cv::calcHist(cv::InputArrayOfArrays, const std::vector<int>&, cv::InputArray, cv::OutputArray, const std::vector<int>&, const std::vector<float>&, bool), file /home/reports/ci/slave_desktop/50-SDK/opencv/modules/imgproc/src/histogram.cpp, line 1427
答案 0 :(得分:0)
我是用那段代码制作的:
public void compare()
{
String filenameLeft = null, filenameCurrent = null, filenameLeftHist = null, filenameCurrentHist = null;
File fileLeft = null, fileCurrent = null, fileLeftHist = null, fileCurrentHist = null;
File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES); // sciezka do sdcard
fileLeft = new File(path, "left.png");fileCurrent = new File(path, "current.png"); fileLeftHist = new File(path, "leftHist.png"); fileCurrentHist = new File(path, "currentHist.png");
filenameLeft = fileLeft.toString(); filenameCurrent = fileCurrent.toString(); filenameLeftHist = fileLeftHist.toString(); filenameCurrentHist = fileCurrentHist.toString();
Mat H1 = histogram(filenameLeft,filenameLeftHist);
Mat H2 = histogram(filenameCurrent,filenameCurrentHist);
double text = Imgproc.compareHist(H1, H2, Imgproc.CV_COMP_BHATTACHARYYA);
if(text<0.2)Log.d(TAG,"Skrecam W LEWO, bo: "+text);
else Log.d(TAG, "Nie skrecam w LEWO, bo: "+text);
}
public Mat histogram(String filenameIn,String filenameOut) // zwraca histogram obrazu
{
Mat img = Highgui.imread(filenameIn);
Mat src = new Mat(img.height(), img.width(), CvType.CV_8UC2);
Imgproc.cvtColor(img, src, Imgproc.COLOR_RGB2GRAY);
Vector<Mat> bgr_planes = new Vector<Mat>();
Core.split(src, bgr_planes);
MatOfInt histSize = new MatOfInt(256);
final MatOfFloat histRange = new MatOfFloat(0f, 256f);
boolean accumulate = false;
Mat b_hist = new Mat();
Imgproc.calcHist(bgr_planes, new MatOfInt(0),new Mat(), b_hist, histSize, histRange, accumulate);
Highgui.imwrite(filenameOut, b_hist);
return b_hist;
}
我在onCameraFrame()方法中使用compare()。它运作良好,但我有疑问。
有什么选择可以让它更准确吗?它很大程度上依赖于光线,有时相同的布局口不能很好地工作。我在这里比较2个直方图。
答案 1 :(得分:0)
您需要为每个频道指定histsize。
所以而不是
MatOfInt histSize=new MatOfInt(256);
尝试
MatOfInt histSize=new MatOfInt(256,256,256);
范围也是如此。指定每个通道的下限和上限。