So basically I am trying to fill the array[] element with 9 values, however I am not sure how to get the index within this code. I want it to fill the next element in the array each time.
public boolean check(int data[][], int x, int y){
int array[] = new int[9];
int xmax = x+3;
int ymax = y+3;
for(int i = x; i<xmax; i++){
for(int j = y; j<ymax; j++){
array[] = data[i][j];//array[what here?]
}
}
}
for (int i = x; i < xmax; ++i) {
for (int j = y; j < ymax; ++j) {
array[3 * (i - x) + (j - y)] = data[i][j];
}
}
Related
I was wondering how to make this opencv c++ code in Java
uchar *ptr = eye.ptr<uchar>(y);
I have been looking around and I think I can use the uchar as a byte... but I have no idea what the code to get the .ptr in java
Heres my code so far
private Rect getEyeball(Mat eye, MatOfRect circles) {
int[] sums = new int[circles.toArray().length];
for (int y = 0; y < eye.rows(); y++) {
// OpenCV method uchar *ptr = eye.ptr<uchar>(y); Goes here
}
int smallestSum = 9999999;
int smallestSumIndex = -1;
for (int i = 0; i < circles.toArray().length; i++) {
if (sums[i] < smallestSum) {
smallestSum = sums[i];
smallestSumIndex = i;
}
}
return circles.toArray()[smallestSumIndex];
}
The full C++ code is
cv::Vec3f getEyeball(cv::Mat &eye, std::vector<cv::Vec3f> &circles)
{
std::vector<int> sums(circles.size(), 0);
for (int y = 0; y < eye.rows; y++)
{
uchar *ptr = eye.ptr<uchar>(y);
for (int x = 0; x < eye.cols; x++)
{
int value = static_cast<int>(*ptr);
for (int i = 0; i < circles.size(); i++)
{
cv::Point center((int)std::round(circles[i][0]), (int)std::round(circles[i][1]));
int radius = (int)std::round(circles[i][2]);
if (std::pow(x - center.x, 2) + std::pow(y - center.y, 2) < std::pow(radius, 2))
{
sums[i] += value;
}
}
++ptr;
}
}
int smallestSum = 9999999;
int smallestSumIndex = -1;
for (int i = 0; i < circles.size(); i++)
{
if (sums[i] < smallestSum)
{
smallestSum = sums[i];
smallestSumIndex = i;
}
}
return circles[smallestSumIndex];
}
Distilling down your C++:
for (int y = 0; y < eye.rows; y++)
{
uchar *ptr = eye.ptr<uchar>(y);
for (int x = 0; x < eye.cols; x++)
{
int value = static_cast<int>(*ptr);
// A loop not using ptr.
++ptr;
}
}
You're simply getting the pixel value at (x,y) from eye.
So, just use one of the overloads of Mat.get.
int[] values = new int[eye.channels()];
for (int y = 0; y < eye.rows(); y++) {
for (int x = 0; x < eye.cols(); x++) {
eye.get(x, y, values);
int value = values[0];
// A loop not using ptr.
}
}
Note that using get(int, int, int[]) rather than get(int, int) here means that you avoid allocating a new array for each iteration, which will make things a heck of a lot faster.
I have a m*n matrix where every element is unique. From a given starting point I have to move to the smallest point(up, down, left, right)and then have to do the same process again. When all other surrounding point is greater than the existing one I have to stop and print the position from start. suppose I have a matrix(5*5)
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
21 22 23 24 25
and starting point is (2,2) then the output will be 13,8,3,2,1.
I have solved this problem my way, But the problem is its complexity. I do not think my solution is efficient. Can anyone suggest me any better solution?
N.B: Except scanner pkg, I am not allowed to import any other pkg. Here is my code:
import java.util.Scanner;
public class DirectionArray {
public static void main(String [] args) {
Scanner in = new Scanner(System.in);
int row = in.nextInt();
int col = in.nextInt();
int[][] ara = new int[row][col];
int[] ara2 = new int[4];
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
ara[i][j] = in.nextInt();
}
}
System.out.println("Give starting point(x) ");
int x= in.nextInt();
System.out.println("Give starting point(y) ");
int y= in.nextInt();
int sx=x;
int sy =y;
int [] fx={+1,-1,0,0};
int [] fy={0,0,+1,-1};
int p=0;
int l=0;
int v=0;
int r=0;
int [] result=new int[row*col] ;
int min=ara[x][y];
boolean swap=true;
for(int i=0;i<(row*col)-1;i++) {
for (int k = 0; k < 4; k++) {
int nx = x + fx[k];
int ny = y + fy[k];
if (nx >= 0 && nx < row && ny >= 0 && ny < col) {
if (min > ara[nx][ny]) {
ara2[p] = ara[nx][ny];
p++;
}
}
}
p=0;
while(swap) {
swap=false;
r++;
for (int q = 0; q < ara2.length-r; q++) {
if(ara2[q]>ara2[q+1]){
int temp = ara2[q];
ara2[q]=ara2[q+1];
ara2[q+1]=temp;
swap=true;
}
}
}
for(int j=0;j<ara2.length;j++) {
if(ara2[j]!=0)
{
v=ara2[j];
result[l]=v;
l++;
break;
}
}
min=v;
for(int o=0;o<ara2.length;o++) {
ara2[o]=0;
}
for(int m=0;m<row;m++){
for(int n=0;n<col;n++){
if(ara[m][n]==v) {
x = m;
y = n;
}
}
}
I think you need to split up your code in more methods. It would make it easier to read.
For example this is how I would reorganize it:
private static final int[][] COORDINATES_TO_TRY = new int[][]{{-1, 0}, {1, 0}, {0, 1}, {0, -1}};
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int row = in.nextInt();
int col = in.nextInt();
int[][] array = new int[row][col];
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
array[i][j] = in.nextInt();
}
}
System.out.println("Give starting point(x) ");
int x = in.nextInt();
System.out.println("Give starting point(y) ");
int y = in.nextInt();
findMinimum(array, x, y);
return;
}
private static int[] findMinimum(int[][] array, int x, int y) {
for (int i = 0; true; i++) {
if (i > 0) {
System.out.print(",");
}
System.out.print(array[x][y]);
int[] coordinates = findLocalMinimum(array, x, y);
if (x == coordinates[0] && y == coordinates[1]) {
return coordinates;
}
x = coordinates[0];
y = coordinates[1];
}
}
private static int[] findLocalMinimum(int[][] array, int x, int y) {
int min = array[x][y];
int minX = x;
int minY = y;
for (int[] coordinates : COORDINATES_TO_TRY) {
int i = x + coordinates[0];
int j = y + coordinates[1];
if (i >= 0 && i < array.length && j >= 0 && j < array[i].length) {
if (array[i][j] < min) {
minX = i;
minY = j;
min = array[i][j];
}
}
}
return new int[]{minX, minY};
}
How can I create xy matrix (where x <= y) with random integers (from 0 to 9) which has at least x zeros and max (xy)/2 in java?
Size of matrix (x,y) is given. Also I would like to know how to use this matrix with same integers with same index in other class (for example 'public class Game{...}'). I'm a beginner so please make it easy for me :)
My code so far:
import java.util.Random;
public class Solution {
int a[][];
public void P(int x, int y){
Random r = new Random();
a = new int[x][y];
for (int i=0; i<x; i++){
for (int j=0; j<y; j++){
a[i][j] = r.nextInt(10);
System.out.print(a[i][j] + " ");
}
System.out.println();
}
System.out.println();
int zeros = 0;
for (int i=0; i<x; i++){
for (int j=0; j<y; j++){
if(a[i][j]==0){
zeros ++;
}
}
}
System.out.println(zeros);
}
public int[][] getA() {
return a;
}
}
Random r = new Random();
a = new int[x][y];
int noZeros = r.nextInt((y*x)/2-x) + x;
boolean z[][] = new boolean[x][y];
for (int i = 0; i < noZeros; i++) {
z[r.nextInt(x)][r.nextInt(y)] = true;
}
for (int i = 0; i < x; i++) {
for (int j = 0; j < y; j++) {
if (!z[i][j]) {
a[i][j] = r.nextInt(9) + 1;
}
System.out.print(a[i][j] + " ");
}
System.out.println();
}
System.out.println();
int zeros = 0;
for (int i = 0; i < x; i++) {
for (int j = 0; j < y; j++) {
if (a[i][j] == 0) {
zeros++;
}
}
}
System.out.println(zeros);
In my solution I create first boolean matrix, which represents zeros, then I chose random number, but from 1 to 9.
I would first determine how many 0s you will have in the matrix, getting a random int from within your min/max bound. Then I would calculate the other xy - numZeros random integers. Finally, for each cell in the matrix, I would pick a random element from a combined list (your 0s + other random integers, removing the element when it is picked).
I have a 2d array called tiles[x][y] which goes till 9 so has 100 things inside of it.
How can I get another array and put everything from the 2d array into the normal array?
int counter = 0;
for (int x = 0; x < mapWidth; x++) {
for (int y = 0; y < mapHeight; y++) {
tiles[y][x] = new loopVak(Color.WHITE, x*tileWidth, y*tileHeight);
}
}
This is how the 2d array is made, mapwidth and mapheight is 10.
If you want to convert tiles to a new 1D Array then you can simply do something like this:
int k = 0, newArray[] = new loopVak[100];
for(int i = 0; i < mapWidth; i++) {
for(int j = 0; j < mapHeight; j++) {
newArray[k++] = tiles[i][j];
}
}
If you do not want the 2D array in the first place then you can do something like this:
int counter = 0, newArray[] = new loopVak[100];
for(int x = 0; x < mapWidth; x++) {
for(int y = 0; y < mapHeight; y++) {
newArray[counter++] = new loopVak(Color.WHITE, x * tileWidth, y * tileHeight);
}
}
I have the two methods which should write pixel values of the absolute difference of a template and the same size patch from the original picture into the pixel where both patch and template coordinates are 0,0.
Here are my 2 methods.
private void normalizeAndDraw(double biggest, double[] temporaryPixels, int[] dstPixels ){
double normalize = 255 / biggest;
for (int c = 0; c < temporaryPixels.length; c++) {
int value = (int) (temporaryPixels[c] * normalize);
dstPixels[c] = 0xFF000000 | (value << 16) | (value << 8) | value;
}
}
private void getAbsolutePicture(int srcPixels[], int srcWidth, int srcHeight, int dstPixels[], int dstWidth, int dstHeight, int templatePixels[], int tmpHeight, int tmpWidth) {
double temporaryPixels[] = new double[dstHeight * dstWidth];
double biggest = 0;
double sumR = 0;
for (int j = 0; j < tmpHeight; j++) {
for (int i = 0; i < tmpWidth; i++) {
int posTmp = j * tmpWidth + i;
sumR += templatePixels[posTmp] & 0xFF;
}
}
for (int y = 0; y < dstHeight; y++) {
for (int x = 0; x < dstWidth; x++) {
double sumI = 0;
for (int j = 0; j < tmpHeight; j++) {
for (int i = 0; i < tmpWidth; i++) {
int pos = (y + j) * dstWidth + (x + i);
sumI += srcPixels[pos] & 0xFF;
}
}
double absDifference = Math.abs(sumI - sumR);
biggest = Math.max(absDifference, biggest);
temporaryPixels[y * dstWidth + x] = absDifference;
}
}
normalizeAndDraw(biggest, temporaryPixels, dstPixels);
}
They get called like this.
getAbsolutePicture(srcPixels, srcWidth, srcHeight, dstPixels, dstWidth, dstHeight, templatePixels, templateWidth, templateHeight);
If values are written into the dstPixels array they will automatically be displayed.
Unfortunately instead of the correct solution which looks like this
http://i.stack.imgur.com/cqlD3.png
I get a result which looks like this
http://i.stack.imgur.com/2Cjhz.png
I am pretty sure that my error lies in the calculation of sumR and sumI but I just cant figure it out?
What exactly is wrong in my code?
My code was actually okay. The biggest problem was that when I called getAbsolutePicture() I mixed up tmpWdith and tmpHeight.