Java Code Examples for org.opencv.imgproc.Imgproc#cvtColor()

The following examples show how to use org.opencv.imgproc.Imgproc#cvtColor() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MainActivity.java    From MOAAP with MIT License 8 votes vote down vote up
void Contours() {
    Mat grayMat = new Mat();
    Mat cannyEdges = new Mat();
    Mat hierarchy = new Mat();

    List<MatOfPoint> contourList = new ArrayList<MatOfPoint>(); //A list to store all the contours

    //Converting the image to grayscale
    Imgproc.cvtColor(originalMat, grayMat, Imgproc.COLOR_BGR2GRAY);

    Imgproc.Canny(originalMat, cannyEdges, 10, 100);

    //finding contours
    Imgproc.findContours(cannyEdges, contourList, hierarchy, Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_SIMPLE);

    //Drawing contours on a new image
    Mat contours = new Mat();
    contours.create(cannyEdges.rows(), cannyEdges.cols(), CvType.CV_8UC3);
    Random r = new Random();
    for (int i = 0; i < contourList.size(); i++) {
        Imgproc.drawContours(contours, contourList, i, new Scalar(r.nextInt(255), r.nextInt(255), r.nextInt(255)), -1);
    }
    //Converting Mat back to Bitmap
    Utils.matToBitmap(contours, currentBitmap);
    imageView.setImageBitmap(currentBitmap);
}
 
Example 2
Source File: MainActivity.java    From MOAAP with MIT License 7 votes vote down vote up
public void DifferenceOfGaussian() {
    Mat grayMat = new Mat();
    Mat blur1 = new Mat();
    Mat blur2 = new Mat();

    //Converting the image to grayscale
    Imgproc.cvtColor(originalMat, grayMat, Imgproc.COLOR_BGR2GRAY);

    Imgproc.GaussianBlur(grayMat, blur1, new Size(15, 15), 5);
    Imgproc.GaussianBlur(grayMat, blur2, new Size(21, 21), 5);

    //Subtracting the two blurred images
    Mat DoG = new Mat();
    Core.absdiff(blur1, blur2, DoG);

    //Inverse Binary Thresholding
    Core.multiply(DoG, new Scalar(100), DoG);
    Imgproc.threshold(DoG, DoG, 50, 255, Imgproc.THRESH_BINARY_INV);

    //Converting Mat back to Bitmap
    Utils.matToBitmap(DoG, currentBitmap);
    imageView.setImageBitmap(currentBitmap);
}
 
Example 3
Source File: MainActivity.java    From open-quartz with Apache License 2.0 6 votes vote down vote up
public Mat onCameraFrame(CvCameraViewFrame inputFrame) {
    mRgba = inputFrame.rgba();
    mGray = inputFrame.gray();

    switch (MainActivity.viewMode) {
        case MainActivity.VIEW_MODE_RGBA:
            return mRgba;

        case MainActivity.VIEW_MODE_HIST:
            return mRgba;

        case MainActivity.VIEW_MODE_CANNY:
            Imgproc.Canny(mGray, mIntermediateMat, 80, 100);
            Imgproc.cvtColor(mIntermediateMat, mGray, Imgproc.COLOR_GRAY2BGRA, 4);
            return mGray;

        case MainActivity.VIEW_MODE_SOBEL:
            Imgproc.Sobel(mGray, mGray, CvType.CV_8U, 1, 1);
            //			Core.convertScaleAbs(mIntermediateMat, mIntermediateMat, 10, 0);
            Imgproc.cvtColor(mGray, mGray, Imgproc.COLOR_GRAY2BGRA, 4);
            return mGray;

        case MainActivity.VIEW_MODE_PIXELIZE:
            Imgproc.resize(mGray, mIntermediateMat, mSize0, 0.1, 0.1,
                Imgproc.INTER_NEAREST);
            Imgproc.resize(mIntermediateMat, mRgba, mRgba.size(), 0.0, 0.0,
                Imgproc.INTER_NEAREST);
            return mRgba;

        case MainActivity.VIEW_MODE_GRAY:
            return mGray;

        case MainActivity.VIEW_MODE_FEATURES:
            FindFeatures(mGray.getNativeObjAddr(), mRgba.getNativeObjAddr());
            return mRgba;

        default:
            return mRgba;
    }
}
 
Example 4
Source File: ProcessHelper.java    From OpenCV-android with Apache License 2.0 6 votes vote down vote up
/**
 * 边缘检测
 *
 * @param origin   原始bitmap
 * @param callback 回调
 */
public void canny(Bitmap origin, ProcessCallback callback) {
    if (origin == null) {
        return;
    }
    try {
        Bitmap bitmap = Bitmap.createBitmap(origin.getWidth(), origin.getHeight(), Bitmap.Config.RGB_565);
        Utils.bitmapToMat(origin, rgbMat);
        Imgproc.cvtColor(rgbMat, grayMat, Imgproc.COLOR_RGB2GRAY);
        Mat edges = new Mat();
        // 阈值极限
        Imgproc.Canny(grayMat, edges, 50, 300);
        Utils.matToBitmap(edges, bitmap);
        callback.onSuccess(bitmap);
    } catch (Exception e) {
        callback.onFailed(e.getMessage());
    }
}
 
Example 5
Source File: AutoCalibrationManager.java    From ShootOFF with GNU General Public License v3.0 6 votes vote down vote up
public Mat preProcessFrame(final Mat mat) {
	if (mat.channels() == 1) return mat.clone();

	final Mat newMat = new Mat(mat.rows(), mat.cols(), CvType.CV_8UC1);

	Imgproc.cvtColor(mat, newMat, Imgproc.COLOR_BGR2GRAY);

	if (logger.isTraceEnabled()) {
		String filename = String.format("grayscale.png");
		final File file = new File(filename);
		filename = file.toString();
		Highgui.imwrite(filename, newMat);
	}

	return newMat;
}
 
Example 6
Source File: MainActivity.java    From MOAAP with MIT License 6 votes vote down vote up
static Mat drawMatches(Mat img1, MatOfKeyPoint key1, Mat img2, MatOfKeyPoint key2, MatOfDMatch matches, boolean imageOnly){
    //https://github.com/mustafaakin/image-matcher/tree/master/src/in/mustafaak/imagematcher
    Mat out = new Mat();
    Mat im1 = new Mat();
    Mat im2 = new Mat();
    Imgproc.cvtColor(img1, im1, Imgproc.COLOR_GRAY2RGB);
    Imgproc.cvtColor(img2, im2, Imgproc.COLOR_GRAY2RGB);
    if ( imageOnly){
        MatOfDMatch emptyMatch = new MatOfDMatch();
        MatOfKeyPoint emptyKey1 = new MatOfKeyPoint();
        MatOfKeyPoint emptyKey2 = new MatOfKeyPoint();
        Features2d.drawMatches(im1, emptyKey1, im2, emptyKey2, emptyMatch, out);
    } else {
        Features2d.drawMatches(im1, key1, im2, key2, matches, out);
    }
    //Bitmap bmp = Bitmap.createBitmap(out.cols(), out.rows(), Bitmap.Config.ARGB_8888);
    Imgproc.cvtColor(out, out, Imgproc.COLOR_BGR2RGB);
    Imgproc.putText(out, "Frame", new Point(img1.width() / 2,30), Core.FONT_HERSHEY_PLAIN, 2, new Scalar(0,255,255),3);
    Imgproc.putText(out, "Match", new Point(img1.width() + img2.width() / 2,30), Core.FONT_HERSHEY_PLAIN, 2, new Scalar(255,0,0),3);
    return out;
}
 
Example 7
Source File: FaceSwap.java    From Machine-Learning-Projects-for-Mobile-Applications with MIT License 5 votes vote down vote up
/**
 * Swaps the faces of two photos where the faces have landmarks pts1 and pts2.
 *
 * @param bmp1 photo 1.
 * @param bmp2 photo 2.
 * @param pts1 landmarks for a face in bmp1.
 * @param pts2 landmarks for a face in bmp2.
 * @return a bitmap where a face in bmp1 has been pasted onto a face in bmp2.
 */
private Bitmap swap(Bitmap bmp1, Bitmap bmp2, ArrayList<PointF> pts1, ArrayList<PointF> pts2) {
    // For storing x and y coordinates of landmarks.
    // Needs to be stored like this when sending them to native code.
    int[] X1 = new int[pts1.size()];
    int[] Y1 = new int[pts1.size()];
    int[] X2 = new int[pts2.size()];
    int[] Y2 = new int[pts2.size()];

    for (int i = 0; i < pts1.size(); ++i) {
        int x1 = pts1.get(i).X();
        int y1 = pts1.get(i).Y();
        X1[i] = x1;
        Y1[i] = y1;

        int x2 = pts2.get(i).X();
        int y2 = pts2.get(i).Y();
        X2[i] = x2;
        Y2[i] = y2;
    }

    // Get OpenCV data structures
    Mat img1 = new Mat();
    bitmapToMat(bmp1, img1);
    Mat img2 = new Mat();
    bitmapToMat(bmp2, img2);

    // Convert to three channel image format
    Imgproc.cvtColor(img1, img1, Imgproc.COLOR_BGRA2BGR);
    Imgproc.cvtColor(img2, img2, Imgproc.COLOR_BGRA2BGR);

    Mat swapped = new Mat();
    // Call native function to get swapped image
    portraitSwapNative(img1.getNativeObjAddr(), img2.getNativeObjAddr(), X1, Y1, X2, Y2, swapped.getNativeObjAddr());
    // Convert back to standard image format
    Bitmap bmp = Bitmap.createBitmap(bmp1.getWidth(), bmp1.getHeight(), Bitmap.Config.ARGB_8888);
    matToBitmap(swapped, bmp);

    return bmp;
}
 
Example 8
Source File: ColorBlobDetector.java    From FaceT with Mozilla Public License 2.0 5 votes vote down vote up
public void setHsvColor(Scalar hsvColor) {
        double minH = (hsvColor.val[0] >= mColorRadius.val[0]) ? hsvColor.val[0]-mColorRadius.val[0] : 0;
        double maxH = (hsvColor.val[0]+mColorRadius.val[0] <= 255) ? hsvColor.val[0]+mColorRadius.val[0] : 255;

        mLowerBound.val[0] = minH;
        mUpperBound.val[0] = maxH;

        Log.d(" mLowerBound.val[0]", mLowerBound.val[0]+"");
        Log.d(" mUpperBound.val[0]", mUpperBound.val[0]+"");

        mLowerBound.val[1] = hsvColor.val[1] - mColorRadius.val[1];
        mUpperBound.val[1] = hsvColor.val[1] + mColorRadius.val[1];

        mLowerBound.val[2] = hsvColor.val[2] - mColorRadius.val[2];
        mUpperBound.val[2] = hsvColor.val[2] + mColorRadius.val[2];

//        mLowerBound.val[3] = 30;
//        mUpperBound.val[3] = 220;
        mLowerBound.val[3] = 0;
        mUpperBound.val[3] = 255;

        Mat spectrumHsv = new Mat(1, (int)(maxH-minH), CvType.CV_8UC3);

        for (int j = 0; j < maxH-minH; j++) {
            byte[] tmp = {(byte)(minH+j), (byte)255, (byte)255};
            spectrumHsv.put(0, j, tmp);
        }

        Imgproc.cvtColor(spectrumHsv, mSpectrum, Imgproc.COLOR_HSV2RGB_FULL, 4);
    }
 
Example 9
Source File: MainActivity.java    From MOAAP with MIT License 5 votes vote down vote up
void HoughLines() {

        Mat grayMat = new Mat();
        Mat cannyEdges = new Mat();
        Mat lines = new Mat();

        //Converting the image to grayscale
        Imgproc.cvtColor(originalMat, grayMat, Imgproc.COLOR_BGR2GRAY);

        Imgproc.Canny(grayMat, cannyEdges, 10, 100);

        Imgproc.HoughLinesP(cannyEdges, lines, 1, Math.PI / 180, 50, 20, 20);

        Mat houghLines = new Mat();
        houghLines.create(cannyEdges.rows(), cannyEdges.cols(), CvType.CV_8UC1);

        //Drawing lines on the image
        for (int i = 0; i < lines.cols(); i++) {
            double[] points = lines.get(0, i);
            double x1, y1, x2, y2;

            x1 = points[0];
            y1 = points[1];
            x2 = points[2];
            y2 = points[3];

            Point pt1 = new Point(x1, y1);
            Point pt2 = new Point(x2, y2);

            //Drawing lines on an image
            Imgproc.line(houghLines, pt1, pt2, new Scalar(255, 0, 0), 1);
        }

        //Converting Mat back to Bitmap
        Utils.matToBitmap(houghLines, currentBitmap);
        imageView.setImageBitmap(currentBitmap);

    }
 
Example 10
Source File: GrayscaleFilter.java    From DogeCV with GNU General Public License v3.0 5 votes vote down vote up
public void process(Mat input, Mat mask) {
    // Convert the input to grayscale
    Imgproc.cvtColor(input,input,Imgproc.COLOR_RGB2GRAY);

    // Blur it
    Imgproc.GaussianBlur(input,input,new Size(5,5),0);
    // Run in range check
    Core.inRange(input, lower, upper, mask);
    input.release();
}
 
Example 11
Source File: JavaShotDetector.java    From ShootOFF with GNU General Public License v3.0 5 votes vote down vote up
private void addShot(Frame workingFrame, PixelCluster pc) {
	final Optional<ShotColor> color = pc.getColor(workingFrame.getOriginalMat(), colorDistanceFromRed);

	if (!color.isPresent()) {
		if (logger.isDebugEnabled()) logger.debug("Processing Shot: Shot Rejected By Lack Of Color Density");
		return;
	}

	final double x = pc.centerPixelX;
	final double y = pc.centerPixelY;

	if (super.addShot(color.get(), x, y, workingFrame.getTimestamp(), true)
			&& Configuration.getConfig().isDebugShotsRecordToFiles()) {
		final Mat debugFrame = new Mat();
		Imgproc.cvtColor(workingFrame.getOriginalMat(), debugFrame, Imgproc.COLOR_HSV2BGR);

		String filename = String.format("shot-%d-%d-%d_orig.png",
				cameraManager.cameraTimeToShotTime(workingFrame.getTimestamp()), (int) pc.centerPixelX,
				(int) pc.centerPixelY);
		final File file = new File(filename);
		filename = file.toString();
		Highgui.imwrite(filename, debugFrame);

		for (final Pixel p : pc) {
			if (javafx.scene.paint.Color.GREEN.equals(color.get())) {
				final double[] greenColor = { 0, 255, 0 };
				debugFrame.put(p.y, p.x, greenColor);
			} else {
				final double[] redColor = { 0, 0, 255 };
				debugFrame.put(p.y, p.x, redColor);
			}
		}

		final File outputfile = new File(
				String.format("shot-%d-%d-%d.png", cameraManager.cameraTimeToShotTime(workingFrame.getTimestamp()),
						(int) pc.centerPixelX, (int) pc.centerPixelY));
		filename = outputfile.toString();
		Highgui.imwrite(filename, debugFrame);
	}
}
 
Example 12
Source File: SXOpenCV.java    From SikuliX1 with MIT License 5 votes vote down vote up
public static List<Match> doFindChanges(Image original, Image changed) {
  List<Match> changes = new ArrayList<>();
  if (changed.isValid()) {
    int PIXEL_DIFF_THRESHOLD = 3;
    int IMAGE_DIFF_THRESHOLD = 5;
    Mat previousGray = SXOpenCV.newMat();
    Mat nextGray = SXOpenCV.newMat();
    Mat mDiffAbs = SXOpenCV.newMat();
    Mat mDiffTresh = SXOpenCV.newMat();

    Imgproc.cvtColor(original.getContent(), previousGray, toGray);
    Imgproc.cvtColor(changed.getContent(), nextGray, toGray);
    Core.absdiff(previousGray, nextGray, mDiffAbs);
    Imgproc.threshold(mDiffAbs, mDiffTresh, PIXEL_DIFF_THRESHOLD, 0.0, Imgproc.THRESH_TOZERO);

    if (Core.countNonZero(mDiffTresh) > IMAGE_DIFF_THRESHOLD) {
      Imgproc.threshold(mDiffAbs, mDiffAbs, PIXEL_DIFF_THRESHOLD, 255, Imgproc.THRESH_BINARY);
      Imgproc.dilate(mDiffAbs, mDiffAbs, SXOpenCV.newMat());
      Mat se = Imgproc.getStructuringElement(Imgproc.MORPH_ELLIPSE, new Size(5, 5));
      Imgproc.morphologyEx(mDiffAbs, mDiffAbs, Imgproc.MORPH_CLOSE, se);

      List<MatOfPoint> contours = new ArrayList<MatOfPoint>();
      Mat mHierarchy = SXOpenCV.newMat();
      Imgproc.findContours(mDiffAbs, contours, mHierarchy, Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_SIMPLE);
      changes = contoursToRectangle(contours);
    }
  }
  return changes;
}
 
Example 13
Source File: HSVRangeFilter.java    From DogeCV with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Process a image and return a mask
 * @param input - Input image to process
 * @param mask - Output mask
 */
@Override
public void process(Mat input, Mat mask) {
    // Convert the input to HSV
    Imgproc.cvtColor(input,input,Imgproc.COLOR_RGB2HSV_FULL);

    // Blur it
    Imgproc.GaussianBlur(input,input,new Size(5,5),0);
    // Run in range check
    Core.inRange(input,lower,upper,mask);
    input.release();
}
 
Example 14
Source File: ImgprocessUtils.java    From classchecks with Apache License 2.0 5 votes vote down vote up
/**
 * 将源图像转换为灰度图
 * @param srcImg
 * @return
 */
public static Mat covertImage2Gray(Mat srcImg) {
	Mat gray = new Mat(); // 存储灰度图
	if (srcImg.channels() == 3) {
		Imgproc.cvtColor(srcImg, gray, Imgproc.COLOR_BGR2GRAY);
	} else if (srcImg.channels() == 4) {
		Imgproc.cvtColor(srcImg, gray, Imgproc.COLOR_BGRA2GRAY);
	} else {
		gray = srcImg;
	}
	return gray;
}
 
Example 15
Source File: ColorBlobDetector.java    From OpenCV-AndroidSamples with MIT License 5 votes vote down vote up
public void process(Mat rgbaImage) {
    Imgproc.pyrDown(rgbaImage, mPyrDownMat);
    Imgproc.pyrDown(mPyrDownMat, mPyrDownMat);

    Imgproc.cvtColor(mPyrDownMat, mHsvMat, Imgproc.COLOR_RGB2HSV_FULL);

    Core.inRange(mHsvMat, mLowerBound, mUpperBound, mMask);
    Imgproc.dilate(mMask, mDilatedMask, new Mat());

    List<MatOfPoint> contours = new ArrayList<MatOfPoint>();

    Imgproc.findContours(mDilatedMask, contours, mHierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);

    // Find max contour area
    double maxArea = 0;
    Iterator<MatOfPoint> each = contours.iterator();
    while (each.hasNext()) {
        MatOfPoint wrapper = each.next();
        double area = Imgproc.contourArea(wrapper);
        if (area > maxArea)
            maxArea = area;
    }

    // Filter contours by area and resize to fit the original image size
    mContours.clear();
    each = contours.iterator();
    while (each.hasNext()) {
        MatOfPoint contour = each.next();
        if (Imgproc.contourArea(contour) > mMinContourArea*maxArea) {
            Core.multiply(contour, new Scalar(4,4), contour);
            mContours.add(contour);
        }
    }
}
 
Example 16
Source File: ImageProcessor.java    From react-native-documentscanner-android with MIT License 4 votes vote down vote up
private void enhanceDocument( Mat src ) {
    if(!this.noGrayscale){
        Imgproc.cvtColor(src,src, Imgproc.COLOR_RGBA2GRAY);
    }
    src.convertTo(src,CvType.CV_8UC1, colorGain , colorBias);
}
 
Example 17
Source File: JavaCameraView.java    From real_time_circle_detection_android with MIT License 4 votes vote down vote up
@Override
public Mat rgba() {
    Imgproc.cvtColor(mYuvFrameData, mRgba, Imgproc.COLOR_YUV2RGBA_NV21, 4);
    return mRgba;
}
 
Example 18
Source File: LeviColorFilter.java    From DogeCV with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Process a image and return a mask
 * @param input - Input image to process
 * @param mask - Output mask
 */
@Override
public void process(Mat input, Mat mask) {
    channels = new ArrayList<>();

    switch(color){
        case RED:
            if(threshold == -1){
                threshold = 164;
            }

            Imgproc.cvtColor(input, input, Imgproc.COLOR_RGB2Lab);
            Imgproc.GaussianBlur(input,input,new Size(3,3),0);
            Core.split(input, channels);
            Imgproc.threshold(channels.get(1), mask, threshold, 255, Imgproc.THRESH_BINARY);
            break;
        case BLUE:
            if(threshold == -1){
                threshold = 145;
            }

            Imgproc.cvtColor(input, input, Imgproc.COLOR_RGB2YUV);
            Imgproc.GaussianBlur(input,input,new Size(3,3),0);
            Core.split(input, channels);
            Imgproc.threshold(channels.get(1), mask, threshold, 255, Imgproc.THRESH_BINARY);
            break;
        case WHITE:
            if(threshold == -1) {
                threshold = 150;
            }

            Imgproc.cvtColor(input, input, Imgproc.COLOR_RGB2Lab);
            Imgproc.GaussianBlur(input,input,new Size(3,3),0);
            Core.split(input, channels);
            Core.inRange(channels.get(0), new Scalar(threshold, 150, 40), new Scalar(255, 150, 150), mask);
            break;
        case YELLOW:
            if(threshold == -1){
                threshold = 70;
            }
            
            Mat lab = new Mat(input.size(), 0);
            Imgproc.cvtColor(input, lab, Imgproc.COLOR_RGB2Lab);
            Mat temp = new Mat();
            Core.inRange(input, new Scalar(0,0,0), new Scalar(255,255,164), temp);
            Mat mask2 = new Mat(input.size(), 0);
            temp.copyTo(mask2);
            input.copyTo(input, mask2);
            mask2.release();
            temp.release();
            lab.release();
            
            Imgproc.cvtColor(input, input, Imgproc.COLOR_RGB2YUV);
            Imgproc.GaussianBlur(input,input,new Size(3,3),0);
            Core.split(input, channels);
            if(channels.size() > 0){
                Imgproc.threshold(channels.get(1), mask, threshold, 255, Imgproc.THRESH_BINARY_INV);
            }

            break;
    }

    for(int i=0;i<channels.size();i++){
        channels.get(i).release();
    }

    input.release();

}
 
Example 19
Source File: JavaCameraView.java    From MOAAP with MIT License 4 votes vote down vote up
@Override
public Mat rgba() {
    Imgproc.cvtColor(mYuvFrameData, mRgba, Imgproc.COLOR_YUV2RGBA_NV21, 4);
    return mRgba;
}
 
Example 20
Source File: Color.java    From FTCVision with MIT License 2 votes vote down vote up
/**
 * Rapidly convert an RGBA matrix to a Grayscale matrix, bypassing
 * most of the color conversion overhead.
 *
 * @param rgba RGBA matrix
 * @return Grayscale matrix
 */
public static Mat rapidConvertRGBAToGRAY(Mat rgba) {
    Mat gray = new Mat(rgba.rows(), rgba.cols(), CvType.CV_8UC1);
    Imgproc.cvtColor(rgba, gray, Imgproc.COLOR_RGBA2GRAY);
    return gray;
}