1
我寫了一個方法來在openCV中爲我正在爲競賽而建的機器人做一些視覺處理。幀和過程對象是mat對象和實例字段,並通過我知道的一個事實工作的另一種方法從usb相機流中獲取。當我用另一種方法顯示框架對象時,如果我已經運行了處理方法,框架就不會顯示,但如果我沒有運行,框架就會顯示。框架墊包含未處理的圖像,通過該圖像繪製標記和輪廓,並且在將標記和輪廓繪製到框架上之前,處理墊用於存儲處理的圖像。 [編輯]:我發現它是由cvtColor方法造成的。這是打印錯誤: (-215)(scn == 3 || scn == 4)& &(depth == CV_8U || depth == CV_32F)。OpenCV計算機視覺處理代碼不起作用
//blurs the image to remove false positives
Imgproc.GaussianBlur(frame, processed, new Size(17, 17), 3);
//we are going to use HSV, not BGR for better filtration
Imgproc.cvtColor(processed, processed, Imgproc.COLOR_BGR2HSV);
//create scalars to hold high and low thresholds if using BGR
/*Scalar lowRange = new Scalar(RobotMap.lowBlueValue, RobotMap.lowGreenValue, RobotMap.lowRedValue);
Scalar highRange = new Scalar(RobotMap.highBlueValue, RobotMap.highGreenValue, RobotMap.highRedValue);*/
//create scalars if using HSV
Scalar lowRange = new Scalar(RobotMap.lowHue, RobotMap.lowSat, RobotMap.lowVal);
Scalar highRange = new Scalar(RobotMap.highHue, RobotMap.highSat, RobotMap.highVal);
//removes everything not in our filter range
Core.inRange(processed, lowRange, highRange, processed);
//mat used to for some of the contour finding
//TODO determine if necessary
Mat hierarchy = new Mat();
//create an arraylist to hold the unfiltered contours
ArrayList<MatOfPoint> contours = new ArrayList<MatOfPoint>();
//find the contours in our image
findContours(processed, contours, hierarchy, RETR_LIST, CHAIN_APPROX_NONE);
//list of filtered contours
ArrayList<MatOfPoint> filteredContours = new ArrayList<MatOfPoint>();
//list of filtered contours as rect objects
ArrayList<Rect> rects = new ArrayList<Rect>();
//put our contours into rectangle objects if they pass our conditions
for (MatOfPoint contour : contours) {
//bounding rect objects are rectangles whose bounderies encompass all of the contour
Rect boundingRect = boundingRect(contour);
//check to see if we are a tallish rectangle with a largish area
if (boundingRect.height > boundingRect.width && boundingRect.area() > RobotMap.minimumArea) {
filteredContours.add(contour);
rects.add(boundingRect);
}
}
//draw our contours
drawContours(frame, filteredContours, -1, new Scalar(0, 0xFF, 0), FILLED);
//figure out how many targets there are
numTargets = filteredContours.size();
//draw marker at center of all rects
if(rects.size() > 0)
Imgproc.drawMarker(frame, center(rects), new Scalar(0xFF, 0, 0xFF));
//draw markers to show info on each rect
for (Rect rect : rects) {
Imgproc.drawMarker(frame, center(rect), new Scalar(0, 0, 0xFF));
Imgproc.drawMarker(frame, rect.br(), new Scalar(0xFF, 0, 0));
Imgproc.drawMarker(frame, rect.tl(), new Scalar(0xFF, 0, 0));
}
if(numTargets > 0)
center = center(rects).x;
謝謝。我發現了這個錯誤。事實證明,在自定義攝像頭服務器的啓動期間,我正在抓取框架,框架還沒有被抓住。如果框架爲空,我會在開始處添加一點檢查。由於這是我的第一篇文章,我如何將其標記爲已解決? – Nomaxx117