List of usage examples for org.opencv.core Core extractChannel
public static void extractChannel(Mat src, Mat dst, int coi)
From source file:com.example.colordetector.CamMainActivity.java
License:Apache License
public Mat onCameraFrame(CvCameraViewFrame inputFrame) { // The frame currently captured by the camera, converted in the color RGBA rgbaFrame = inputFrame.rgba();// ww w . j av a2 s .c o m // Convert the frame in the HSV color space, to be able to identify the color with the thresholds Imgproc.cvtColor(rgbaFrame, rgbFrame, Imgproc.COLOR_RGBA2RGB); // Cant't convert directly rgba->hsv Imgproc.cvtColor(rgbFrame, hsvFrame, Imgproc.COLOR_RGB2HSV); // Create a mask with ONLY zones of the chosen color on the frame currently captured Core.inRange(hsvFrame, thresMin, thresMax, inRangeMask); filteredFrame.setTo(new Scalar(0, 0, 0)); rgbFrame.copyTo(filteredFrame, inRangeMask); // if the method of shooting image is set to manual, exit and return the filtered image... if (!methodAuto) { return filteredFrame; } //...else it was setted the automatic method, so continue with the method // Check the H channel of the image to see if the searched color is present on the frame Core.extractChannel(filteredFrame, hChannel, 0); /* There are two method to verify the color presence; below a little explanation */ /* checkRange: if almost one pixel of the searched color is found, continue with the countdown * Pro -> fast. * Versus -> less accurate, possible presence of false positive depending the quality of the camera * if(!Core.checkRange(hChannel, true, 0, 1)){ */ /* Percentage: count the pixel of the searched color, and if there are almost the * 0.1% of total pixel of the frame with the searched color, continue with the countdown * Pro: more accurate, lower risk of false positive * Versus: slower than checkRange * N.B.: the threshold percentage is imposted with a low value, otherwise small object will not be seen */ int perc = Core.countNonZero(hChannel); // Percentage if (perc > (frameDim * 0.001)) { // if the shooting method is setted to 'immediate', the photo is returned now; // otherwise continue with the countdown if (!countDown) { takePicture(); return rgbaFrame; } // 'point' is where the countdown will be visualized; in that case at // a quarter of height and width than left up angle Point point = new Point(rgbaFrame.cols() >> 2, rgbaFrame.rows() >> 2); // Update the osd countdown every 75*8 ms (if color searched is present) // Use the division in 75 ms cause a higher value would give the user the feeling of screen/app 'blocked'. if (timeToElapse % 8 == 0) { if (osdSecond.compareTo("") == 0) osdSecond = ((Integer) (timeToElapse >> 3)).toString(); else osdSecond = osdSecond.concat(".." + (((Integer) (timeToElapse >> 3)).toString())); Core.putText(rgbaFrame, osdSecond, point, 1, 3, Scalar.all(255)); } timeToElapse -= 1; // the user has framed an object for more than 3 seconds; shoot the photo if (timeToElapse <= 0) { timeToElapse = 24; takePicture(); } // the user has framed an object for less than 3 seconds; wait else { try { synchronized (this) { wait(75); } } catch (InterruptedException e) { e.printStackTrace(); } } } // the user has NOT framed a color searched object; reset osd else { timeToElapse = 24; osdSecond = ""; } return rgbaFrame; }
From source file:de.hftl_projekt.ict.MainActivity.java
/** * method to reduce the color (quantize) the given matrix (image) * @param image input matrix// w ww.j a v a2 s .c o m * @return modified input matrix */ public Mat reduceColors(Mat image) { if (channels.size() == 0) { for (int i = 0; i < image.channels(); i++) { Mat channel = new Mat(); // fill array with a matrix for each channel channels.add(channel); } } int i = 0; // process each channel individually for (Mat c : channels) { Core.extractChannel(image, c, i); // binary quantization (set threshold so each color (R, G, B) can have the value (0 or 255) ) // and using the Otsu algorithm to optimize the quantization Imgproc.threshold(c, c, 0, 255, Imgproc.THRESH_BINARY_INV + Imgproc.THRESH_OTSU); i++; } Core.merge(channels, image); // put the channel back together return image; }