Java Code Examples for org.opencv.core.Mat#setTo()

The following examples show how to use org.opencv.core.Mat#setTo() . 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: SimpleSampleTests.java    From super-cloudops with Apache License 2.0 7 votes vote down vote up
@Test
public void test1() {
	System.out.println("Welcome to OpenCV " + Core.VERSION);

	Mat m = new Mat(5, 10, CvType.CV_8UC1, new Scalar(0));
	System.out.println("OpenCV Mat: " + m);
	Mat mr1 = m.row(1);
	mr1.setTo(new Scalar(1));

	Mat mc5 = m.col(5);
	mc5.setTo(new Scalar(5));
	System.out.println("OpenCV Mat data:\n" + m.dump());
}
 
Example 2
Source File: ImageProcessor.java    From Document-Scanner with GNU General Public License v3.0 7 votes vote down vote up
private void enhanceDocument( Mat src ) {
    if (colorMode && filterMode) {
        src.convertTo(src,-1, colorGain , colorBias);
        Mat mask = new Mat(src.size(), CvType.CV_8UC1);
        Imgproc.cvtColor(src,mask,Imgproc.COLOR_RGBA2GRAY);

        Mat copy = new Mat(src.size(), CvType.CV_8UC3);
        src.copyTo(copy);

        Imgproc.adaptiveThreshold(mask,mask,255,Imgproc.ADAPTIVE_THRESH_MEAN_C,Imgproc.THRESH_BINARY_INV,15,15);

        src.setTo(new Scalar(255,255,255));
        copy.copyTo(src,mask);

        copy.release();
        mask.release();

        // special color threshold algorithm
        colorThresh(src,colorThresh);
    } else if (!colorMode) {
        Imgproc.cvtColor(src,src,Imgproc.COLOR_RGBA2GRAY);
        if (filterMode) {
            Imgproc.adaptiveThreshold(src, src, 255, Imgproc.ADAPTIVE_THRESH_MEAN_C, Imgproc.THRESH_BINARY, 15, 15);
        }
    }
}
 
Example 3
Source File: ColorBlobDetectionActivity.java    From OpenCV-AndroidSamples with MIT License 7 votes vote down vote up
public Mat onCameraFrame(CvCameraViewFrame inputFrame) {
    mRgba = inputFrame.rgba();

    if (mIsColorSelected) {
        mDetector.process(mRgba);
        List<MatOfPoint> contours = mDetector.getContours();
        Log.e(TAG, "Contours count: " + contours.size());
        Imgproc.drawContours(mRgba, contours, -1, CONTOUR_COLOR);

        Mat colorLabel = mRgba.submat(4, 68, 4, 68);
        colorLabel.setTo(mBlobColorRgba);

        Mat spectrumLabel = mRgba.submat(4, 4 + mSpectrum.rows(), 70, 70 + mSpectrum.cols());
        mSpectrum.copyTo(spectrumLabel);
    }

    return mRgba;
}
 
Example 4
Source File: OpenCVoperation.java    From Human-hair-detection with Apache License 2.0 6 votes vote down vote up
public void skinSegmentation_WithThreshold()
{ 
    //-----code for skin detection--using hsv color method
    
    orgMask= new Mat(matrix2_grabcut.size(),CvType.CV_8UC3);
    orgMask.setTo(new Scalar(0,0,0));
    
    Mat hsvImage = new Mat();
    Imgproc.cvtColor(matrix2_grabcut, hsvImage, Imgproc.COLOR_BGR2HSV);
    Core.inRange(hsvImage, new Scalar(3,30,50),new Scalar(33,255,255),orgMask);
    
    Imgcodecs.imwrite(resultDirectory + maskOutput, orgMask);
    
    newMask = Imgcodecs.imread(resultDirectory + maskOutput);
    //code for getting rgb skin mask from hsv skin mask
    mask_rgb = new Mat(newMask.size(),CvType.CV_8SC3);
    Imgproc.cvtColor(newMask, mask_rgb, Imgproc.COLOR_HSV2RGB);    
    Imgcodecs.imwrite(resultDirectory+maskRgbOutput, mask_rgb);
    
    //getting only skin image with red background
    matrix3_skindetection= new Mat(sourceImage.size(), sourceImage.type());
    matrix3_skindetection.setTo(new Scalar(0,0,255));
    sourceImage.copyTo(matrix3_skindetection,orgMask);
    
    Imgcodecs.imwrite(resultDirectory+skinDetectionOutput,matrix3_skindetection);
}
 
Example 5
Source File: OpenCVoperation.java    From Human-hair-detection with Apache License 2.0 6 votes vote down vote up
public void skinSegmentation()
{
    matrix3_skindetection = new Mat(matrix2_grabcut.size(),matrix2_grabcut.type()); 
    matrix3_skindetection.setTo(new Scalar(0,0,255));
    Mat skinMask = new Mat();
    Mat hsvMatrix = new Mat();
    
    Scalar lower = new Scalar(0,48,80);
    Scalar upper = new Scalar(20,255,255);
    
    Imgproc.cvtColor(matrix2_grabcut, hsvMatrix, Imgproc.COLOR_BGR2HSV);
    Core.inRange(hsvMatrix, lower, upper, skinMask);
    
    Mat kernel =Imgproc.getStructuringElement(Imgproc.MORPH_ELLIPSE,new Size(11,11));
    Imgproc.erode(skinMask, skinMask, kernel);
    Imgproc.dilate(skinMask, skinMask, kernel);  
    
    Imgproc.GaussianBlur(skinMask,skinMask, new Size(3,3), 0);
    
    Core.bitwise_and(matrix2_grabcut, matrix2_grabcut, matrix3_skindetection,skinMask);
    Imgcodecs.imwrite(resultDirectory+skinDetectionOutput , matrix3_skindetection);
}
 
Example 6
Source File: AutoCalibrationManager.java    From ShootOFF with GNU General Public License v3.0 6 votes vote down vote up
private void blankRotatedRect(Mat mat, final RotatedRect rect) {
	final Mat tempMat = Mat.zeros(mat.size(), CvType.CV_8UC1);

	final Point points[] = new Point[4];
	rect.points(points);
	for (int i = 0; i < 4; ++i) {
		Core.line(tempMat, points[i], points[(i + 1) % 4], new Scalar(255, 255, 255));
	}

	final Mat tempMask = Mat.zeros((mat.rows() + 2), (mat.cols() + 2), CvType.CV_8UC1);
	Imgproc.floodFill(tempMat, tempMask, rect.center, new Scalar(255, 255, 255), null, new Scalar(0, 0, 0),
			new Scalar(254, 254, 254), 4);

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

	mat.setTo(new Scalar(0, 0, 0), tempMat);
}
 
Example 7
Source File: OpenCVoperation.java    From Human-hair-detection with Apache License 2.0 5 votes vote down vote up
public void findImageDifference()
{
    matrix7_output = new Mat(sourceImage.size(),sourceImage.type());
    matrix7_output.setTo(new Scalar(255,255,255));      //white colored image 
    int rows = sourceImage.rows();
    int cols = sourceImage.cols();
    
    for(int r=0;r <rows ; r++)
    {
        for(int c =0; c < cols; c++)
        {
          //  double grabcut_pixel_val[] =matrix2_grabcut.get(r, c);
          //  double skin_pixel_val[] = newMask.get(r, c);                  
             double grabcut_pixel_val[] =matrix5_grabcut_quantized.get(r,c);
             double skin_pixel_val[] =  matrix6_skin_quantized.get(r,c);      
                //extract those pixels which are non blue in 1st image and red in 2nd image
              if(  ( (grabcut_pixel_val[0] != 255 ) && (grabcut_pixel_val[1]!=255 ) && (grabcut_pixel_val[2] !=255) )  && ( (skin_pixel_val[0]== 0) && (skin_pixel_val[1]== 0) &&(skin_pixel_val[2]== 255) ) )              
              {
                   double orgImage_pixel_val[] = sourceImage.get(r, c);
                  //double orgImage_pixel_val[] = new double[]{0,0,0};
                  //double pixel_val[] = new double[3];
                  //pixel_val[0]=pixel_val[1]=pixel_val[2]=0;
                   matrix7_output.put(r, c, orgImage_pixel_val);
              }
        }
    }       
    Imgcodecs.imwrite(resultDirectory +finalImage_Output, matrix7_output);
    
}
 
Example 8
Source File: ImageUtils.java    From Machine-Learning-Projects-for-Mobile-Applications with MIT License 4 votes vote down vote up
/**
 * Makes gamma correction on an input bitmap. Uses OpenCV.
 *
 * @param bm    input image.
 * @param gamma gamma value, 0. <= gamma <= 2.0
 * @return gamma corrected bitmap.
 */
static Bitmap gammaCorrection(Bitmap bm, double gamma) {

    Mat im = new Mat();
    bitmapToMat(bm, im);

    Mat lut = new Mat(1, 256, CvType.CV_8UC1);
    lut.setTo(new Scalar(0));
    Imgproc.cvtColor(im, im, Imgproc.COLOR_BGRA2BGR);


    for (int i = 0; i < 256; i++)
        lut.put(0, i, Math.pow(1.0 * i / 255, 1 / gamma) * 255);

    Core.LUT(im, lut, im);

    Imgproc.cvtColor(im, im, Imgproc.COLOR_BGR2BGRA);

    Bitmap res = Bitmap.createBitmap(bm.getWidth(), bm.getHeight(), Bitmap.Config.ARGB_8888);
    matToBitmap(im, res);

    return res;
}
 
Example 9
Source File: OpenCVoperation.java    From Human-hair-detection with Apache License 2.0 4 votes vote down vote up
public void skinDetection2()
{
    matrix3_skindetection = new Mat(matrix2_grabcut.size(),matrix2_grabcut.type()); 
    matrix3_skindetection.setTo(new Scalar(0,0,255));
    
    Mat  src_YCrCb = new Mat(matrix2_grabcut.size(),CvType.CV_8SC3);
    Mat src_hsv = new Mat(matrix2_grabcut.size(),CvType.CV_8SC3);
    
    Imgproc.cvtColor(matrix2_grabcut, src_YCrCb, Imgproc.COLOR_BGR2YCrCb);  
    matrix2_grabcut.convertTo(src_hsv, CvType.CV_32FC3);
    Imgproc.cvtColor(src_hsv, src_hsv, Imgproc.COLOR_BGR2HSV);
    Core.normalize(src_hsv, src_hsv, 0.00, 255.00, Core.NORM_MINMAX,CvType.CV_32FC3);
    
    for(int r = 0 ; r< matrix2_grabcut.rows(); r++)
    {
        for(int c = 0 ; c< matrix2_grabcut.cols(); c++)
        {
            double[] Pixel_val_rgb = matrix2_grabcut.get(r, c);
            int B= (int)Pixel_val_rgb[0];
            int G= (int)Pixel_val_rgb[1];
            int R= (int)Pixel_val_rgb[2];
            boolean a1= R1(R,G,B);
            
            double[] Pixel_val_YCrCb = src_YCrCb.get(r, c);
            int Y =(int)Pixel_val_YCrCb[0];
            int Cr =(int)Pixel_val_YCrCb[1];
            int Cb =(int)Pixel_val_YCrCb[2];
            boolean a2= R2(Y,Cr,Cb);
            
            double[] Pixel_val_hsv =src_hsv.get(r,c);
            float H = (float)Pixel_val_hsv[0];
            float S = (float)Pixel_val_hsv[1];
            float V = (float)Pixel_val_hsv[2];
            boolean a3= R3(H,S,V);
            
            if(!(a1 && a2 && a3))
               matrix3_skindetection.put(r, c, new double[]{0,0,255});
            else
               matrix3_skindetection.put(r,c,sourceImage.get(r, c));
         }
    }
    Imgcodecs.imwrite(resultDirectory+skinDetectionOutput , matrix3_skindetection);
}