Reputation: 29
i'm new in Opencv, wanna get the Intensity values of an Imagen in Java.
i made an image using Paint 4x4 pixels, wish get the Intensity values from it and print it in console.
Super tiny Image Demo *This one
import org.opencv.core.Core;
public class helloCV{
public static void main(String[]args){
System.out.println("OpenCv v"+Core.VERSION);
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
//Code for get intesivy values
}
}
Upvotes: 1
Views: 2678
Reputation: 154
mat.get(row,col) returns a double[], this array is of size mat.channels(). The array contains the intensity values.
so for CvType.CV_8UC1 this array is of length 1
and for CvType.CV_8UC3 its returns an array of 3 values.
mat.rows() and mat.cols() provides the number of rows and columns in the mat.
mat.dump() provides intensity values for complete Mat , its same as mat.get() for all row and column.
mat.eye() Returns an identity matrix of the specified size and type. The intensity values will be zeros and one with one along the diagonal of the matrix.
More details about Mat : http://docs.opencv.org/2.4/doc/tutorials/core/mat_the_basic_image_container/mat_the_basic_image_container.html
Upvotes: 1
Reputation: 29
i found something that does what i was looking for:
import java.net.URL;
import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.highgui.Highgui;
public class helloCV{
public static void main(String[]args){
System.out.println(Core.VERSION);
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
Mat mat = Mat.eye(3, 3, CvType.CV_8UC1);
mat = Highgui.imread("C:\\Users\\HM\\Pictures\\ImageDemo.png");
System.out.println("mat = " + mat.dump());
}
}
i guess that prints the matrix of RGB values of each pixel:
run:
2.4.9.0
mat = [88, 88, 88, 127, 127, 127, 255, 255, 255, 255, 255, 255, 255, 255, 255;
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255;
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255;
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255;
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255]
where the first 3 numbers (88, 88, 88) means RGB color of first pixel and so on every 3 numers each pixel. I'm right?
But what does Mat mat = Mat.eye(3, 3, CvType.CV_8UC1)
and mat.dump()
mean?
Upvotes: 0
Reputation: 183
Since, images in OpenCV are represented using matrices. Here is a way to find intensity value of a particular pixel .
Scalar intensity = img.at<uchar>(y, x);
The above line means, we are accessing the pixel (y,x) and specifying its data type to be unsigned char.
To find the same for a given point,
Scalar intensity = img.at<uchar>(Point(x, y));
Do not forget to import Numpy for using the Scalar.
Upvotes: 0