Ask Your Question
0

Getting the matrix of Mat type image

asked 2016-03-22 00:10:30 -0600

TGS gravatar image

updated 2016-03-22 00:49:07 -0600

berak gravatar image

I m doing an image processing using opencv java. I threshold the image to detect red colour and now I want to get the matrix of destination as a printed value. My codes can be presented as follows.

Mat source = Imgcodecs.imread("C:\\Users\\My Kindom\\Desktop\\hsv.jpg",Imgcodecs.CV_LOAD_IMAGE_COLOR);
Mat destination = new Mat(source.rows(),source.cols(),source.type());
destination = source;

Scalar lowerb=new Scalar (170,50,50);
Scalar upperb=new Scalar (180,255,255);


Core.inRange(source, lowerb, upperb, destination);
Imgcodecs.imwrite("C:\\Users\\My Kindom\\Desktop\\ThreshZero.jpg", destination);

for(int i=0;i<destination.rows();i++){
    for(int j=0;j<destination.cols();j++){
        System.out.println(destination.row(),destination.cols());
    }
}
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
3

answered 2016-03-22 00:45:28 -0600

berak gravatar image

updated 2016-03-22 00:46:52 -0600

your for loop is faulty, i guess, you wanted something like this instead:

for(int i=0;i<destination.rows();i++){
    for(int j=0;j<destination.cols();j++){
        System.out.println(destination.get(i,j));
    }
}

but it's better, to use Mat.dump(), to get a string representation:

// setup some demo data:
byte []data = {1,2,3,4,5,6,7,8,9};
Mat m = new Mat(3,3,CvType.CV_8U);
m.put(0,0,data);

// print info:    
System.out.println(m);
System.out.println(m.dump());


 // output:
 Mat [ 3*3*CV_8UC1, isCont=true, isSubmat=false, nativeObj=0x3dacbe8, dataAddr=0x3da69e0 ]
 [  1,   2,   3;
    4,   5,   6;
    7,   8,   9]
edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2016-03-22 00:10:30 -0600

Seen: 1,468 times

Last updated: Mar 22 '16