Java Code Examples for org.opencv.core.CvType#CV_8UC3

The following examples show how to use org.opencv.core.CvType#CV_8UC3 . 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: 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 2
Source File: OpenCVoperation.java    From Human-hair-detection with Apache License 2.0 6 votes vote down vote up
public void testGrabCut()
{
    sourceImage = Imgcodecs.imread(resultDirectory+fileName,Imgcodecs.CV_LOAD_IMAGE_COLOR);
    System.out.println("result Directory is: "+ resultDirectory+fileName);
    
    Mat result = new Mat(sourceImage.size(),sourceImage.type());
    Mat bgModel = new Mat();    //background model
    Mat fgModel = new Mat();    //foreground model
    
    //draw a rectangle 
    Rect rectangle = new Rect(1,1,sourceImage.cols()-1,sourceImage.rows()-1);
    
    Imgproc.grabCut(sourceImage, result,rectangle, bgModel,fgModel,10,Imgproc.GC_INIT_WITH_RECT);  
    Core.compare(result,new Scalar(3,3,3),result,Core.CMP_EQ);
    matrix2_grabcut = new Mat(sourceImage.size(),CvType.CV_8UC3,new Scalar(255,255,255));
    sourceImage.copyTo(matrix2_grabcut, result);
    
    Imgcodecs.imwrite(resultDirectory+grabcutOutput,matrix2_grabcut);
    
   // displayPixels(matrix2_grabcut);
}
 
Example 3
Source File: ColorBlobDetector.java    From hand_finger_recognition_android with MIT License 6 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;

    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] = 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 4
Source File: ColorBlobDetector.java    From OpenCV-AndroidSamples with MIT License 6 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;

    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] = 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 5
Source File: OpenCVoperation.java    From Human-hair-detection with Apache License 2.0 6 votes vote down vote up
public void predict_hair_color()
{
    Mat hsv_input = matrix9_finalOutput.clone();
    List<Mat> channels= new ArrayList<>();
    Mat hsv_histogram = new Mat();
    MatOfFloat  ranges = new MatOfFloat(0,180);
    MatOfInt histSize = new MatOfInt(255);
    Imgproc.cvtColor(hsv_input, hsv_input, Imgproc.COLOR_BGR2HSV);
    Core.split(hsv_input, channels);
    Imgproc.calcHist(channels.subList(0,1), new MatOfInt(0), new Mat(), hsv_histogram, histSize, ranges);
    int hist_w =256;
    int hist_h = 150;
    
    int bin_w =(int)Math.round(hist_w/histSize.get(0,0)[0]);
    Mat histImage= new Mat(hist_h,hist_w,CvType.CV_8UC3,new Scalar(0,0,0));
    
    for(int i=1;i < histSize.get(0,0)[0];i++)
    {
        Imgproc.line(histImage, new Point(bin_w * (i - 1), hist_h - Math.round(hsv_histogram.get(i - 1, 0)[0])), new Point(bin_w * (i), hist_h - Math.round(hsv_histogram.get(i, 0)[0])), new Scalar(255,0,0),2);			    
    }
    Imgcodecs.imwrite(resultDirectory+"histogram_image.png",histImage);
}
 
Example 6
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 7
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 8
Source File: CaptureActivity.java    From FaceT with Mozilla Public License 2.0 5 votes vote down vote up
private Scalar converScalarHsv2Rgba(Scalar hsvColor) {
    Mat pointMatRgba = new Mat();
    Mat pointMatHsv = new Mat(1, 1, CvType.CV_8UC3, hsvColor);
    Imgproc.cvtColor(pointMatHsv, pointMatRgba, Imgproc.COLOR_HSV2RGB_FULL, 4);

    return new Scalar(pointMatRgba.get(0, 0));
}
 
Example 9
Source File: ShowCameraViewActivity.java    From FaceT with Mozilla Public License 2.0 5 votes vote down vote up
private Scalar converScalarHsv2Rgba(Scalar hsvColor) {
    Mat pointMatRgba = new Mat();
    Mat pointMatHsv = new Mat(1, 1, CvType.CV_8UC3, hsvColor);
    Imgproc.cvtColor(pointMatHsv, pointMatRgba, Imgproc.COLOR_HSV2RGB_FULL, 4);

    return new Scalar(pointMatRgba.get(0, 0));
}
 
Example 10
Source File: ColorDetectionActivity.java    From FaceT with Mozilla Public License 2.0 5 votes vote down vote up
private Scalar converScalarHsv2Rgba(Scalar hsvColor) {
    Mat pointMatRgba = new Mat();
    Mat pointMatHsv = new Mat(1, 1, CvType.CV_8UC3, hsvColor);
    Imgproc.cvtColor(pointMatHsv, pointMatRgba, Imgproc.COLOR_HSV2RGB_FULL, 4);

    return new Scalar(pointMatRgba.get(0, 0));
}
 
Example 11
Source File: ContoursFinder.java    From go-bees with GNU General Public License v3.0 5 votes vote down vote up
@Override
public Mat process(@NonNull Mat frame) {
    if (frame.empty()) {
        Log.e("Invalid input frame.");
        return null;
    }
    Mat tmp = frame.clone();
    // Finding outer contours
    contourList.clear();
    Imgproc.findContours(tmp, contourList, hierarchy,
            Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);
    // Filter bees
    Mat contours = new Mat(tmp.rows(), tmp.cols(), CvType.CV_8UC3);
    tmp.release();
    double area;
    Scalar color;
    numBees = 0;
    for (int i = 0; i < contourList.size(); i++) {
        area = Imgproc.contourArea(contourList.get(i));
        if (area > minArea && area < maxArea) {
            color = GREEN;
            numBees++;
        } else {
            color = RED;
        }
        // Draw contour
        Imgproc.drawContours(contours, contourList, i, color, -1);
    }
    return contours;
}
 
Example 12
Source File: OptiTrackCamera.java    From ShootOFF with GNU General Public License v3.0 5 votes vote down vote up
public Mat translateCameraArrayToMat(byte[] imageBuffer) {
	if (viewHeight == 0) viewHeight = getViewHeight();
	if (viewWidth == 0) viewWidth = getViewWidth();

	final Mat mat = new Mat(viewHeight, viewWidth, CvType.CV_8UC1);
	final Mat dst = new Mat(viewHeight, viewWidth, CvType.CV_8UC3);

	mat.put(0, 0, imageBuffer);
	Imgproc.cvtColor(mat, dst, Imgproc.COLOR_GRAY2BGR);
	return dst;
}
 
Example 13
Source File: Camera.java    From ShootOFF with GNU General Public License v3.0 5 votes vote down vote up
static Mat bufferedImageToMat(BufferedImage frame) {
	final BufferedImage transformedFrame = ConverterFactory.convertToType(frame, BufferedImage.TYPE_3BYTE_BGR);
	final byte[] pixels = ((DataBufferByte) transformedFrame.getRaster().getDataBuffer()).getData();
	final Mat mat = new Mat(frame.getHeight(), frame.getWidth(), CvType.CV_8UC3);
	mat.put(0, 0, pixels);

	return mat;
}
 
Example 14
Source File: ColorBlobDetectionActivity.java    From OpenCV-AndroidSamples with MIT License 5 votes vote down vote up
private Scalar converScalarHsv2Rgba(Scalar hsvColor) {
    Mat pointMatRgba = new Mat();
    Mat pointMatHsv = new Mat(1, 1, CvType.CV_8UC3, hsvColor);
    Imgproc.cvtColor(pointMatHsv, pointMatRgba, Imgproc.COLOR_HSV2RGB_FULL, 4);

    return new Scalar(pointMatRgba.get(0, 0));
}
 
Example 15
Source File: MainActivity.java    From hand_finger_recognition_android with MIT License 5 votes vote down vote up
private Scalar converScalarHsv2Rgba(Scalar hsvColor) {
    Mat pointMatRgba = new Mat();
    Mat pointMatHsv = new Mat(1, 1, CvType.CV_8UC3, hsvColor);
    Imgproc.cvtColor(pointMatHsv, pointMatRgba, Imgproc.COLOR_HSV2RGB_FULL, 4);

    return new Scalar(pointMatRgba.get(0, 0));
}
 
Example 16
Source File: FaceFragment.java    From OpenCV-android with Apache License 2.0 5 votes vote down vote up
@Override
public void onCameraViewStarted(int width, int height) {
    System.out.println("onCameraViewStarted...");
    mRgba = new Mat(height, width, CvType.CV_8UC3);
    mByte = new Mat(height, width, CvType.CV_8UC1);
    matOfRect = new MatOfRect();
}
 
Example 17
Source File: CVRenderer.java    From faceswap with Apache License 2.0 4 votes vote down vote up
public void onCameraViewStarted(int width, int height) {
    displayFrame=new Mat(width, height, CvType.CV_8UC3);
    transmitFrame=new Mat(width, height, CvType.CV_8UC3);
    Log.d(LOG_TAG, "on camera view started width: "+width+" height:"+height);
}
 
Example 18
Source File: PS3EyeCamera.java    From ShootOFF with GNU General Public License v3.0 4 votes vote down vote up
public Mat translateCameraArrayToMat(byte[] imageBuffer) {
	final Mat mat = new Mat(getViewHeight(), getViewWidth(), CvType.CV_8UC3);

	mat.put(0, 0, imageBuffer);
	return mat;
}
 
Example 19
Source File: ImgWindow.java    From opencv-fun with GNU Affero General Public License v3.0 4 votes vote down vote up
public Mat createBuffer() {
	return new Mat(getHeight(), getWidth(), CvType.CV_8UC3);
}