use of org.bytedeco.javacpp.indexer.IntIndexer in project javacpp by bytedeco.
the class IndexerTest method testIntIndexer.
@Test
public void testIntIndexer() {
System.out.println("IntIndexer");
long size = 7 * 5 * 3 * 2;
long[] sizes = { 7, 5, 3, 2 };
long[] strides = { 5 * 3 * 2, 3 * 2, 2, 1 };
final IntPointer ptr = new IntPointer(size);
for (int i = 0; i < size; i++) {
ptr.position(i).put((int) i);
}
IntIndexer arrayIndexer = IntIndexer.create(ptr.position(0), sizes, strides, false);
IntIndexer directIndexer = IntIndexer.create(ptr.position(0), sizes, strides, true);
int n = 0;
for (int i = 0; i < sizes[0]; i++) {
assertEquals(n, arrayIndexer.get(i));
assertEquals(n, directIndexer.get(i));
for (int j = 0; j < sizes[1]; j++) {
assertEquals(n, arrayIndexer.get(i, j));
assertEquals(n, directIndexer.get(i, j));
for (int k = 0; k < sizes[2]; k++) {
assertEquals(n, arrayIndexer.get(i, j, k));
assertEquals(n, directIndexer.get(i, j, k));
for (int m = 0; m < sizes[3]; m++) {
long[] index = { i, j, k, m };
assertEquals(n, arrayIndexer.get(index));
assertEquals(n, directIndexer.get(index));
arrayIndexer.put(index, (int) (2 * n));
directIndexer.put(index, (int) (3 * n));
n++;
}
}
}
}
try {
arrayIndexer.get(size);
fail("IndexOutOfBoundsException should have been thrown.");
} catch (IndexOutOfBoundsException e) {
}
try {
directIndexer.get(size);
fail("IndexOutOfBoundsException should have been thrown.");
} catch (IndexOutOfBoundsException e) {
}
System.out.println("arrayIndexer" + arrayIndexer);
System.out.println("directIndexer" + directIndexer);
for (int i = 0; i < size; i++) {
assertEquals(3 * i, ptr.position(i).get());
}
arrayIndexer.release();
for (int i = 0; i < size; i++) {
assertEquals(2 * i, ptr.position(i).get());
}
System.gc();
if (Loader.sizeof(Pointer.class) > 4)
try {
long longSize = 0x80000000L + 8192;
final IntPointer longPointer = new IntPointer(longSize);
assertEquals(longSize, longPointer.capacity());
IntIndexer longIndexer = IntIndexer.create(longPointer);
assertEquals(longIndexer.pointer(), longPointer);
for (long i = 0; i < 8192; i++) {
longPointer.put(longSize - i - 1, (int) i);
}
for (long i = 0; i < 8192; i++) {
assertEquals((long) longIndexer.get(longSize - i - 1), (int) i);
}
System.out.println("longIndexer[0x" + Long.toHexString(longSize - 8192) + "] = " + longIndexer.get(longSize - 8192));
} catch (OutOfMemoryError e) {
System.out.println(e);
}
System.out.println();
}
use of org.bytedeco.javacpp.indexer.IntIndexer in project javacv by bytedeco.
the class ImageSegmentation method main.
public static void main(String[] args) {
// Load the image
Mat src = imread(args[0]);
// Check if everything was fine
if (src.data().isNull())
return;
// Show source image
imshow("Source Image", src);
// Change the background from white to black, since that will help later to extract
// better results during the use of Distance Transform
UByteIndexer srcIndexer = src.createIndexer();
for (int x = 0; x < srcIndexer.rows(); x++) {
for (int y = 0; y < srcIndexer.cols(); y++) {
int[] values = new int[3];
srcIndexer.get(x, y, values);
if (Arrays.equals(values, WHITE)) {
srcIndexer.put(x, y, BLACK);
}
}
}
// Show output image
imshow("Black Background Image", src);
// Create a kernel that we will use for accuting/sharpening our image
Mat kernel = Mat.ones(3, 3, CV_32F).asMat();
FloatIndexer kernelIndexer = kernel.createIndexer();
// an approximation of second derivative, a quite strong kernel
kernelIndexer.put(1, 1, -8);
// do the laplacian filtering as it is
// well, we need to convert everything in something more deeper then CV_8U
// because the kernel has some negative values,
// and we can expect in general to have a Laplacian image with negative values
// BUT a 8bits unsigned int (the one we are working with) can contain values from 0 to 255
// so the possible negative number will be truncated
Mat imgLaplacian = new Mat();
// copy source image to another temporary one
Mat sharp = src;
filter2D(sharp, imgLaplacian, CV_32F, kernel);
src.convertTo(sharp, CV_32F);
Mat imgResult = subtract(sharp, imgLaplacian).asMat();
// convert back to 8bits gray scale
imgResult.convertTo(imgResult, CV_8UC3);
imgLaplacian.convertTo(imgLaplacian, CV_8UC3);
// imshow( "Laplace Filtered Image", imgLaplacian );
imshow("New Sharped Image", imgResult);
// copy back
src = imgResult;
// Create binary image from source image
Mat bw = new Mat();
cvtColor(src, bw, CV_BGR2GRAY);
threshold(bw, bw, 40, 255, CV_THRESH_BINARY | CV_THRESH_OTSU);
imshow("Binary Image", bw);
// Perform the distance transform algorithm
Mat dist = new Mat();
distanceTransform(bw, dist, CV_DIST_L2, 3);
// Normalize the distance image for range = {0.0, 1.0}
// so we can visualize and threshold it
normalize(dist, dist, 0, 1., NORM_MINMAX, -1, null);
imshow("Distance Transform Image", dist);
// Threshold to obtain the peaks
// This will be the markers for the foreground objects
threshold(dist, dist, .4, 1., CV_THRESH_BINARY);
// Dilate a bit the dist image
Mat kernel1 = Mat.ones(3, 3, CV_8UC1).asMat();
dilate(dist, dist, kernel1);
imshow("Peaks", dist);
// Create the CV_8U version of the distance image
// It is needed for findContours()
Mat dist_8u = new Mat();
dist.convertTo(dist_8u, CV_8U);
// Find total markers
MatVector contours = new MatVector();
findContours(dist_8u, contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE);
// Create the marker image for the watershed algorithm
Mat markers = Mat.zeros(dist.size(), CV_32SC1).asMat();
// Draw the foreground markers
for (int i = 0; i < contours.size(); i++) drawContours(markers, contours, i, Scalar.all((i) + 1));
// Draw the background marker
circle(markers, new Point(5, 5), 3, RGB(255, 255, 255));
imshow("Markers", multiply(markers, 10000).asMat());
// Perform the watershed algorithm
watershed(src, markers);
Mat mark = Mat.zeros(markers.size(), CV_8UC1).asMat();
markers.convertTo(mark, CV_8UC1);
bitwise_not(mark, mark);
// imshow("Markers_v2", mark); // uncomment this if you want to see how the mark
// image looks like at that point
// Generate random colors
List<int[]> colors = new ArrayList<int[]>();
for (int i = 0; i < contours.size(); i++) {
int b = theRNG().uniform(0, 255);
int g = theRNG().uniform(0, 255);
int r = theRNG().uniform(0, 255);
int[] color = { b, g, r };
colors.add(color);
}
// Create the result image
Mat dst = Mat.zeros(markers.size(), CV_8UC3).asMat();
// Fill labeled objects with random colors
IntIndexer markersIndexer = markers.createIndexer();
UByteIndexer dstIndexer = dst.createIndexer();
for (int i = 0; i < markersIndexer.rows(); i++) {
for (int j = 0; j < markersIndexer.cols(); j++) {
int index = markersIndexer.get(i, j);
if (index > 0 && index <= contours.size())
dstIndexer.put(i, j, colors.get(index - 1));
else
dstIndexer.put(i, j, BLACK);
}
}
// Visualize the final image
imshow("Final Result", dst);
}
use of org.bytedeco.javacpp.indexer.IntIndexer in project javacv by bytedeco.
the class PrincipalComponentAnalysis method principalComponentAnalysis.
// contour is a one dimensional array
private void principalComponentAnalysis(Mat contour, int entry, Mat matrix) throws Exception {
PCA pca_analysis = null;
Mat mean = null;
Mat eigenVector = null;
Mat eigenValues = null;
// Construct a buffer used by the pca analysis
try (Mat data_pts = new Mat(contour.rows(), 2, CV_64FC1);
Mat placeholder = new Mat();
Point cntr = new Point()) {
IntIndexer contourIndexer = contour.createIndexer();
DoubleIndexer data_idx = data_pts.createIndexer();
for (int i = 0; i < contour.rows(); i++) {
data_idx.put(i, 0, contourIndexer.get(i, 0));
data_idx.put(i, 1, contourIndexer.get(i, 1));
}
contourIndexer.release();
data_idx.release();
// Perform PrincipalComponentAnalysis analysis
ArrayList<Point2d> eigen_vecs = new ArrayList(2);
ArrayList<Double> eigen_val = new ArrayList(2);
pca_analysis = new PCA(data_pts, placeholder, CV_PCA_DATA_AS_ROW);
mean = pca_analysis.mean();
eigenVector = pca_analysis.eigenvectors();
eigenValues = pca_analysis.eigenvalues();
DoubleIndexer mean_idx = mean.createIndexer();
DoubleIndexer eigenVectorIndexer = eigenVector.createIndexer();
DoubleIndexer eigenValuesIndexer = eigenValues.createIndexer();
for (int i = 0; i < 2; ++i) {
eigen_vecs.add(new Point2d(eigenVectorIndexer.get(i, 0), eigenVectorIndexer.get(i, 1)));
eigen_val.add(eigenValuesIndexer.get(0, i));
}
double cntrX = mean_idx.get(0, 0);
double cntrY = mean_idx.get(0, 1);
mean_idx.release();
eigenVectorIndexer.release();
eigenValuesIndexer.release();
double x1 = cntrX + 0.02 * (eigen_vecs.get(0).x() * eigen_val.get(0));
double y1 = cntrY + 0.02 * (eigen_vecs.get(0).y() * eigen_val.get(0));
double x2 = cntrX - 0.02 * (eigen_vecs.get(1).x() * eigen_val.get(1));
double y2 = cntrY - 0.02 * (eigen_vecs.get(1).y() * eigen_val.get(1));
// Draw the principal components, keep accuracy during calculations
cntr.x((int) Math.rint(cntrX));
cntr.y((int) Math.rint(cntrY));
circle(matrix, cntr, 5, new Scalar(255, 0, 255, 0));
double radian1 = Math.atan2(cntrY - y1, cntrX - x1);
double radian2 = Math.atan2(cntrY - y2, cntrX - x2);
double hypotenuse1 = Math.sqrt((cntrY - y1) * (cntrY - y1) + (cntrX - x1) * (cntrX - x1));
double hypotenuse2 = Math.sqrt((cntrY - y2) * (cntrY - y2) + (cntrX - x2) * (cntrX - x2));
// Enhance the vector signal by a factor of 2
double point1x = cntrX - 2 * hypotenuse1 * Math.cos(radian1);
double point1y = cntrY - 2 * hypotenuse1 * Math.sin(radian1);
double point2x = cntrX - 2 * hypotenuse2 * Math.cos(radian2);
double point2y = cntrY - 2 * hypotenuse2 * Math.sin(radian2);
drawAxis(matrix, radian1, cntr, point1x, point1y, Scalar.BLUE);
drawAxis(matrix, radian2, cntr, point2x, point2y, Scalar.CYAN);
} finally {
if (pca_analysis != null) {
pca_analysis.deallocate();
}
if (mean != null) {
mean.deallocate();
}
if (eigenVector != null) {
eigenVector.deallocate();
}
if (eigenValues != null) {
eigenValues.deallocate();
}
}
}
Aggregations