I have an array that I'm trying to blur using a kernel, but the loop doesn't finish for some reason, here's the code:
for (int x = 0; x < 128; x++) {
for (int y = 0; y < 128; y++) {
for (int kx = -2; x <= 6; x++) {
for (int ky = -2; y <= 6; y++) {
nlm2[x][y] += 100 * (int) ((float) nlm[x][y]*(float)kernel[(kx+3)*(ky+3)-1]);
System.out.println(x+" "+y);
System.out.println(kx+" NLM: "+(float) nlm[x][y]);
System.out.println(ky+" Kernel: "+(float)kernel[(kx+3)*(ky+3)-1]);
}
}
}
}
It seems to stop after
x = 0; y = 6
kx = ky = -2
There are no errors in the console, and it shows another print screen after this little loop-de-loop.
This lines might be the problem.-
for (int kx = -2; x <= 6; x++) {
for (int ky = -2; y <= 6; y++) {
You're incrementing x and y respectively, instead of kx and ky.
I'm guessing you really meant.-
for (int kx = -2; kx <= 6; kx ++) {
for (int ky = -2; ky <= 6; ky ++) {
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 created a mitsubishi logo builder as shown below. It takes input from the logo.txt file and uses those ints/chars to create however many logos that are identified in the logo.txt file. I just feel that my code is very repetitive and I would like to clean it up in terms of cutting it into separate methods instead of repeating over and over, are there any reccomemndations on how to clean this up? I would appreciate any and all feedback. Or even just a starting point. Thank you in advance.
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
try {
input = new Scanner(new FileInputStream("config.txt"));
//initialize ithe variable's input and output
} catch (FileNotFoundException e) {
System.out.println("File not found.");
System.exit(0);
}
int size = Integer.parseInt(inputStream.nextLine());
int numLogos = Integer.parseInt(inputStream.nextLine());
char letter1 = inputStream.next().charAt(0);
char letter2 = inputStream.next().charAt(0);
for(int bee = numLogos; bee > 0; bee--){
for(int y = 0; y < size; y++) {
for(int x = 0; x < y; x++) {
System.out.print(letter2);
}
for(int x = 0; x < size - y; x++) {
System.out.print(letter2);
}
for(int x = 0; x < y; x++) {
System.out.print(letter2);
}
for(int x = 0; x < size - y; x++) {
System.out.print(letter2);
}
for(int x = size; x > y; x--) {
System.out.print(letter2);
}
for(int x = 0; x < y; x++) {
System.out.print(letter1);
}
for(int x = 0; x < y; x++) {
System.out.print(letter1);
}
for(int x = size; x > y; x--) {
System.out.print(letter2);
}
for(int x = 0; x < y; x++) {
System.out.print(letter2);
}
for(int x = 0; x < size - y; x++) {
System.out.print(letter2);
}
for(int x = 0; x < y; x++) {
System.out.print(letter2);
}
for(int x = 0; x < size - y; x++) {
System.out.print(letter2);
}
System.out.println();
}
for(int y = 0; y < size; y++) {
for(int x = 0; x < y; x++) {
System.out.print(letter2);
}
for(int x = 0; x < size - y; x++) {
System.out.print(letter2);
}
for(int x = 0; x < y; x++) {
System.out.print(letter2);
}
for(int x = 0; x < size - y; x++) {
System.out.print(letter2);
}
for(int x = 0; x < y; x++) {
System.out.print(letter2);
}
for(int x = 0; x < size - y; x++) {
System.out.print(letter1);
}
for(int x = 0; x < size - y; x++) {
System.out.print(letter1);
}
for(int x = 0; x < y; x++) {
System.out.print(letter2);
}
for(int x = 0; x < y; x++) {
System.out.print(letter2);
}
for(int x = 0; x < size - y; x++) {
System.out.print(letter2);
}
for(int x = 0; x < y; x++) {
System.out.print(letter2);
}
for(int x = 0; x < size - y; x++) {
System.out.print(letter2);
}
System.out.println();
}
for(int y = 0; y < size; y++) {
for(int x = size; x > y; x--) {
System.out.print(letter2);
}
for(int x = 0; x < y; x++) {
System.out.print(letter1);
}
for(int x = 0; x < y; x++) {
System.out.print(letter1);
}
for(int x = 0; x < size - y; x++) {
System.out.print(letter1);
}
for(int x = size; x > y; x--) {
System.out.print(letter1);
}
for(int x = 0; x < y; x++) {
System.out.print(letter2);
}
for(int x = 0; x < y; x++) {
System.out.print(letter2);
}
for(int x = 0; x < size - y; x++) {
System.out.print(letter1);
}
for(int x = 0; x < y; x++) {
System.out.print(letter1);
}
for(int x = 0; x < size - y; x++) {
System.out.print(letter1);
}
for(int x = 0; x < y; x++) {
System.out.print(letter1);
}
for(int x = size; x > y; x--) {
System.out.print(letter2);
}
System.out.println();
}
}
}
}
You use this many times in the code:
for(int x = 0; x < y; x++) {
System.out.print(letter2);
}
for(int x = 0; x < size - y; x++) {
System.out.print(letter2);
}
for(int x = 0; x < y; x++) {
System.out.print(letter2);
}
for(int x = 0; x < size - y; x++) {
System.out.print(letter2);
}
It can be replaced by
for(int x = 0; x < 2*size; x++) {
System.out.print(letter2);
}
Also, this:
for(int x = 0; x < size - y; x++) {
System.out.print(letter1);
}
for(int x = 0; x < size - y; x++) {
System.out.print(letter1);
}
Can be replaced by
for(int x = 0; x < 2*(size - y); x++) {
System.out.print(letter1);
}
I have this problem, i have a NxM matrix and i want to multiply it by a 3x3 matrix just as a convolutional matrix multiplication
example in this link
This are the code of the matrix:
int width = img.getWidth();
int height = img.getHeight();
int matrix[][] = new int[width][height];
int edgeMatrix[][] = {
{-1,-1,-1},
{-1,8,-1},
{-1,-1,-1}
};
This is the code of the cycle:
for (int x = 0; x < width; x++) {
w = 0;
holderX = x;
for (w = 0; w < 3; w++) {
v = 0;
if (w > 0)
x++;
for (v = 0; v < 3; v++) {
sum = sum + matrix[v][x] * edgeMatrix[v][w];
if (w == 2 && v == 2)
x = holderX;
}
}
}
This cycle already multiply the first "row" of 3 of the matrix.
T tried in a different ways to achieve this but i just cant get that when the matrix reach the end of the width automatically the N value increase one and then starts over again and in the same time the value still working on the internal matrix multiplication.
Thanks for the help.
You dont need holderX, but need one more loop.
int width = img.getWidth();
int height = img.getHeight();
int input[][] = img.getPixels(); // or whatever api you use
int output[][] = new int[height][width];
int kernel[][] = {
{-1,-1,-1},
{-1,8,-1},
{-1,-1,-1}
};
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
int accumulator = 0;
for (int v = 0; v < 3; v++) {
for (int w = 0; w < 3; w++) {
int sy = y + v - 1;
int sx = x + w - 1;
if (sy >= 0 && sy < height && sx >= 0 && sx < width)) {
accumulator += input[sy][sx] * kernel[v][w];
}
}
}
output[sy][sx] = accumulator;
}
}
I have created a for loop to produce a grid map. When I click each grid on the map I get X and Y of the grid. When map width is greater than map length, everything is fine, but when attempt to create a map where length is greater than with, the returned x becomes the y, and y becomes the x. The issue is at the second for loop when creating the map but I cannot figure it out.
if(mapWidth>mapLength) {
for (int i = 0; i < mapWidth * mapLength; i++) {
y = i / mapLength;
for(int j=0; j<i+1; j++) {
x = j % mapLength;
}
GridPanel gb = new GridPanel(x, y);
list.add(gb);
mapPanel.add(gb);
}
} else if(mapWidth<mapLength) { //problematic map is created after this condition
for (int i = 0; i < mapWidth * mapLength; i++) {
x = i / mapLength;
for(int j=0; j < i+1; j++){
y = j % mapLength;
}
GridPanel gb = new GridPanel(x, y);
list.add(gb);
mapPanel.add(gb);
}
}
Maps look like this:
Well, maybe I didn't understand well what you expect but I don't think you have to make a special case for the case where mapWidth< mapLength.
Furthermore, I don't get what you intend to do with your nested loop except using CPU resources?
for(int j=0; j<i+1; j++){
x = j % mapLength;
}
When it is left, you will always have x = i % mapLength
Furthermore, as flkes suggested, why don't you use nested loops?
for (int y=0; y < mapLength; y++) {
for(int x=0; x < mapWidth; x++){
GridPanel gb = new GridPanel(x, y);
list.add(gb);
mapPanel.add(gb);
}
}
Could you try this code:
for (int i = 0; i < mapWidth * mapLength; i++) {
y = i / mapLength;
x = i % mapLength;
GridPanel gb = new GridPanel(x, y);
list.add(gb);
mapPanel.add(gb);
}
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);
}
}