Java Code Examples for org.opencv.highgui.Highgui#imdecode()

The following examples show how to use org.opencv.highgui.Highgui#imdecode() . 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: Utils.java    From SoftwarePilot with MIT License 6 votes vote down vote up
public static Mat loadResource(Context context, int resourceId, int flags) throws IOException
{
    InputStream is = context.getResources().openRawResource(resourceId);
    ByteArrayOutputStream os = new ByteArrayOutputStream(is.available());

    byte[] buffer = new byte[4096];
    int bytesRead;
    while ((bytesRead = is.read(buffer)) != -1) {
        os.write(buffer, 0, bytesRead);
    }
    is.close();

    Mat encoded = new Mat(1, os.size(), CvType.CV_8U);
    encoded.put(0, 0, os.toByteArray());
    os.close();

    Mat decoded = Highgui.imdecode(encoded, flags);
    encoded.release();

    return decoded;
}
 
Example 2
Source File: Utils.java    From android-object-distance with Apache License 2.0 6 votes vote down vote up
public static Mat loadResource(Context context, int resourceId, int flags) throws IOException
{
    InputStream is = context.getResources().openRawResource(resourceId);
    ByteArrayOutputStream os = new ByteArrayOutputStream(is.available());

    byte[] buffer = new byte[4096];
    int bytesRead;
    while ((bytesRead = is.read(buffer)) != -1) {
        os.write(buffer, 0, bytesRead);
    }
    is.close();

    Mat encoded = new Mat(1, os.size(), CvType.CV_8U);
    encoded.put(0, 0, os.toByteArray());
    os.close();

    Mat decoded = Highgui.imdecode(encoded, flags);
    encoded.release();

    return decoded;
}
 
Example 3
Source File: Utils.java    From marvel with MIT License 6 votes vote down vote up
public static Mat loadResource(Context context, int resourceId, int flags) throws IOException
{
    InputStream is = context.getResources().openRawResource(resourceId);
    ByteArrayOutputStream os = new ByteArrayOutputStream(is.available());

    byte[] buffer = new byte[4096];
    int bytesRead;
    while ((bytesRead = is.read(buffer)) != -1) {
        os.write(buffer, 0, bytesRead);
    }
    is.close();

    Mat encoded = new Mat(1, os.size(), CvType.CV_8U);
    encoded.put(0, 0, os.toByteArray());
    os.close();

    Mat decoded = Highgui.imdecode(encoded, flags);
    encoded.release();

    return decoded;
}
 
Example 4
Source File: Utils.java    From Android-Car-duino with GNU General Public License v2.0 6 votes vote down vote up
public static Mat loadResource(Context context, int resourceId, int flags) throws IOException
{
    InputStream is = context.getResources().openRawResource(resourceId);
    ByteArrayOutputStream os = new ByteArrayOutputStream(is.available());

    byte[] buffer = new byte[4096];
    int bytesRead;
    while ((bytesRead = is.read(buffer)) != -1) {
        os.write(buffer, 0, bytesRead);
    }
    is.close();

    Mat encoded = new Mat(1, os.size(), CvType.CV_8U);
    encoded.put(0, 0, os.toByteArray());
    os.close();

    Mat decoded = Highgui.imdecode(encoded, flags);
    encoded.release();

    return decoded;
}
 
Example 5
Source File: Utils.java    From ResistorScanner with MIT License 6 votes vote down vote up
public static Mat loadResource(Context context, int resourceId, int flags) throws IOException
{
    InputStream is = context.getResources().openRawResource(resourceId);
    ByteArrayOutputStream os = new ByteArrayOutputStream(is.available());

    byte[] buffer = new byte[4096];
    int bytesRead;
    while ((bytesRead = is.read(buffer)) != -1) {
        os.write(buffer, 0, bytesRead);
    }
    is.close();

    Mat encoded = new Mat(1, os.size(), CvType.CV_8U);
    encoded.put(0, 0, os.toByteArray());
    os.close();

    Mat decoded = Highgui.imdecode(encoded, flags);
    encoded.release();

    return decoded;
}
 
Example 6
Source File: Utils.java    From effective_android_sample with Apache License 2.0 6 votes vote down vote up
public static Mat loadResource(Context context, int resourceId, int flags) throws IOException
{
    InputStream is = context.getResources().openRawResource(resourceId);
    ByteArrayOutputStream os = new ByteArrayOutputStream(is.available());

    byte[] buffer = new byte[4096];
    int bytesRead;
    while ((bytesRead = is.read(buffer)) != -1) {
        os.write(buffer, 0, bytesRead);
    }
    is.close();

    Mat encoded = new Mat(1, os.size(), CvType.CV_8U);
    encoded.put(0, 0, os.toByteArray());
    os.close();

    Mat decoded = Highgui.imdecode(encoded, flags);
    encoded.release();

    return decoded;
}
 
Example 7
Source File: PartialMatcher.java    From StormCV with Apache License 2.0 5 votes vote down vote up
/**
 * Calculates descriptors as defined by detectorType and 
 * descriptorType provided at construction for the provided image
 * @param input
 * @return
 * @throws IOException
 */
private Mat calculateDescriptors(byte[] buffer) throws IOException{
	MatOfByte mob = new MatOfByte(buffer);
	Mat image = Highgui.imdecode(mob, Highgui.CV_LOAD_IMAGE_ANYCOLOR);
	
	FeatureDetector siftDetector = FeatureDetector.create(detectorType);
	MatOfKeyPoint mokp = new MatOfKeyPoint();
	siftDetector.detect(image, mokp);
	
	Mat descriptors = new Mat();
	DescriptorExtractor extractor = DescriptorExtractor.create(descriptorType);
	extractor.compute(image, mokp, descriptors);
	return descriptors;
}
 
Example 8
Source File: FeatureMatcherOp.java    From StormCV with Apache License 2.0 5 votes vote down vote up
/**
 * Calculates descriptors as defined by detectorType and 
 * descriptorType provided at construction for the provided image
 * @param input
 * @return
 * @throws IOException
 */
private Mat calculateDescriptors(byte[] buffer) throws IOException{
	MatOfByte mob = new MatOfByte(buffer);
	Mat image = Highgui.imdecode(mob, Highgui.CV_LOAD_IMAGE_ANYCOLOR);
	
	FeatureDetector siftDetector = FeatureDetector.create(detectorType);
	MatOfKeyPoint mokp = new MatOfKeyPoint();
	siftDetector.detect(image, mokp);
	
	Mat descriptors = new Mat();
	DescriptorExtractor extractor = DescriptorExtractor.create(descriptorType);
	extractor.compute(image, mokp, descriptors);
	return descriptors;
}
 
Example 9
Source File: HaarCascadeOp.java    From StormCV with Apache License 2.0 5 votes vote down vote up
@Override
public List<CVParticle> execute(CVParticle input) throws Exception {
	ArrayList<CVParticle> result = new ArrayList<CVParticle>();
	Frame frame = (Frame)input;
	if(frame.getImageType().equals(Frame.NO_IMAGE)) return result;

	MatOfByte mob = new MatOfByte(frame.getImageBytes());
	Mat image = Highgui.imdecode(mob, Highgui.CV_LOAD_IMAGE_COLOR);
	
	/*
	mob = new MatOfByte();
	Highgui.imencode(".png", image, mob);
	BufferedImage bi = ImageUtils.bytesToImage(mob.toArray());
	ImageIO.write(bi, "png", new File("testOutput/"+sf.getStreamId()+"_"+sf.getSequenceNr()+".png"));
	*/
	
	MatOfRect haarDetections = new MatOfRect();
	haarDetector.detectMultiScale(image, haarDetections, scaleFactor, minNeighbors, flags, new Size(minSize[0], minSize[1]), new Size(maxSize[0], maxSize[1]));
	ArrayList<Descriptor> descriptors = new ArrayList<Descriptor>();
	for(Rect rect : haarDetections.toArray()){
		Rectangle box = new Rectangle(rect.x, rect.y, rect.width, rect.height);
		descriptors.add(new Descriptor(input.getStreamId(), input.getSequenceNr(), box, 0, new float[0]));
	}
	
	Feature feature = new Feature(input.getStreamId(), input.getSequenceNr(), name, 0, descriptors, null);
	if(outputFrame){
		frame.getFeatures().add(feature);
		result.add(frame);
	}else{
		result.add(feature);
	}
	return result;
}
 
Example 10
Source File: OpticalFlowOp.java    From StormCV with Apache License 2.0 4 votes vote down vote up
@Override
public List<CVParticle> execute(List<CVParticle> input) throws Exception {
	List<CVParticle> result = new ArrayList<CVParticle>();
	if(input.size() != 2 || !(input.get(0) instanceof Frame) || !(input.get(1) instanceof Frame))
		return result;
	
	Frame frame1 = (Frame)input.get(0);
	Frame frame2 = (Frame)input.get(1);
	
	MatOfByte mob1 = new MatOfByte(frame1.getImageBytes());
	Mat image1 = Highgui.imdecode(mob1, Highgui.CV_LOAD_IMAGE_ANYCOLOR);
	Mat image1Gray = new Mat( image1.size(), CvType.CV_8UC1 );
	Imgproc.cvtColor( image1, image1Gray, Imgproc.COLOR_RGB2GRAY );
	
	MatOfByte mob2 = new MatOfByte(frame2.getImageBytes());
	Mat image2 = Highgui.imdecode(mob2, Highgui.CV_LOAD_IMAGE_ANYCOLOR);
	Mat image2Gray = new Mat( image2.size(), CvType.CV_8UC1 );
	Imgproc.cvtColor( image2, image2Gray, Imgproc.COLOR_RGB2GRAY );
	
	Mat opticalFlow = new Mat( image1Gray.size(), CvType.CV_32FC2 );
	Video.calcOpticalFlowFarneback( image1Gray, image2Gray, opticalFlow, 0.5, 1, 1, 1, 7, 1.5, 1 );
	
	int cols = opticalFlow.cols();
	int rows = opticalFlow.rows();
	int maxz = opticalFlow.get(0,0).length;
	float[] tmp = new float[maxz];
	float[][][] dense = new float[cols][rows][maxz];
	for(int y=0; y<opticalFlow.rows(); y++){
		for(int x=0; x<opticalFlow.cols(); x++){
			opticalFlow.get(y,x, tmp);
			dense[x][y][0] = tmp[0];
			dense[x][y][1] = tmp[1];
		}
	}
	
	Feature feature = new Feature(frame1.getStreamId(), frame1.getSequenceNr(), name, frame2.getSequenceNr()-frame1.getSequenceNr(), null, dense);
	if(outputFrame){
		frame1.getFeatures().add(feature);
		result.add(frame1);
	}else{
		result.add(feature);
	}

	return result;
}
 
Example 11
Source File: FeatureExtractionOp.java    From StormCV with Apache License 2.0 4 votes vote down vote up
@Override
public List<CVParticle> execute(CVParticle particle) throws Exception {
	List<CVParticle> result = new ArrayList<CVParticle>();
	if(!(particle instanceof Frame)) return result;
	
	Frame frame = (Frame)particle;
	if(frame.getImageType().equals(Frame.NO_IMAGE)) return result;
	try{
		MatOfByte mob = new MatOfByte(frame.getImageBytes());
		Mat image = Highgui.imdecode(mob, Highgui.CV_LOAD_IMAGE_ANYCOLOR);
		
		FeatureDetector siftDetector = FeatureDetector.create(detectorType);
		MatOfKeyPoint mokp = new MatOfKeyPoint();
		siftDetector.detect(image, mokp);
		List<KeyPoint> keypoints = mokp.toList();
		
		Mat descriptors = new Mat();
		DescriptorExtractor extractor = DescriptorExtractor.create(descriptorType);
		extractor.compute(image, mokp, descriptors);
		List<Descriptor> descrList = new ArrayList<Descriptor>();
		float[] tmp = new float[1];
		for(int r=0; r<descriptors.rows(); r++){
			float[] values = new float[descriptors.cols()];
			for(int c=0; c<descriptors.cols(); c++){
				descriptors.get(r, c, tmp);
				values[c] = tmp[0];
			}
			descrList.add(new Descriptor(frame.getStreamId(), frame.getSequenceNr(), new Rectangle((int)keypoints.get(r).pt.x, (int)keypoints.get(r).pt.y, 0, 0), 0, values));
		}
		
		Feature feature = new Feature(frame.getStreamId(), frame.getSequenceNr(), featureName, 0, descrList, null);
		if(outputFrame){
			frame.getFeatures().add(feature);
			result.add(frame);
		}else{
			result.add(feature);
		}		
	}catch(Exception e){
		// catching exception at this point will prevent the sent of a fail! 
		logger.warn("Unable to extract features for frame!", e);
	}
	return result;
}
 
Example 12
Source File: ImageUtils.java    From StormCV with Apache License 2.0 2 votes vote down vote up
/**
 * Creates a Mat object for the image in the provided frame
 * @param image the image to be converted to mat
 * @param imageType the encoding to use, see {@link Frame}
 * @return Mat object representing the image of type Highgui.CV_LOAD_IMAGE_COLOR
 * @throws IOException if the image cannot be read or converted into binary format
 */
public static Mat Image2Mat(BufferedImage image, String imageType) throws IOException{
	MatOfByte mob = new MatOfByte( ImageUtils.imageToBytes(image, imageType) );
	return Highgui.imdecode(mob, Highgui.CV_LOAD_IMAGE_COLOR);
}
 
Example 13
Source File: ImageUtils.java    From StormCV with Apache License 2.0 2 votes vote down vote up
/**
 * creates a Mat object directly from a set of bytes 
 * @param bytes binary representation of an image
 * @return Mat object of type Highgui.CV_LOAD_IMAGE_COLOR
 */
public static Mat bytes2Mat(byte[] bytes){
	MatOfByte mob = new MatOfByte( bytes );
	return Highgui.imdecode(mob, Highgui.CV_LOAD_IMAGE_COLOR);
}