I am currently trying to iterate over every pixel of a black and white image in OpenCV 4.1.2 in Java and to store e 3x3 Matrix for every pixel, which should have a 1, if the pixel currently worked with has e neighbouring pixel there, and an 0 if there is no neighbour at this position. So the value in the center of the Mat should always be 1. Currently im working with that nested for-loops, that should iterate through the Mat.
Mat bwDrawing = new Mat(drawing.size(), CvType.CV_8UC3);
Imgproc.cvtColor(drawing, bwDrawing, Imgproc.COLOR_RGB2GRAY);
List<MatOfInt> freemanChains = new ArrayList<>();
for (double y = 2; y-2 < bwDrawing.size().height; y++) {
for (double x = 2; x-2 < bwDrawing.size().width; x++) {
MatOfInt freemanMat = new MatOfInt();
if (bwDrawing.get((int)y-1, (int)x-1)[0] != 0) {
freemanMat.put(0, 0, 1);
} else {
freemanMat.put(0,0, 0);
}
if (bwDrawing.get((int)y-1, (int)x)[0] != 0) {
freemanMat.put(0, 1, 1);
} else {
freemanMat.put(0,1, 0);
}
if (bwDrawing.get((int)y-1, (int)x+1)[0] == 0) { //right here is the NullPointer Exception
freemanMat.put(0, 2, 1);
} else {
freemanMat.put(0,2, 0);
}
if (bwDrawing.get((int)y, (int)x-1)[0] != 0) {
freemanMat.put(1, 0, 1);
} else {
freemanMat.put(1,0, 0);
}
if (bwDrawing.get((int)y, (int)x)[0] != 0) {
freemanMat.put(1, 1, 1);
} else {
freemanMat.put(1,1, 0);
}
if (bwDrawing.get((int)y, (int)x+1)[0] != 0) {
freemanMat.put(1, 2, 1);
} else {
freemanMat.put(1,2, 0);
}
if (bwDrawing.get((int)y+1, (int)x-1)[0] != 0) {
freemanMat.put(2, 0, 1);
} else {
freemanMat.put(2,0, 0);
}
if (bwDrawing.get((int)y+1, (int)x)[0] != 0) {
freemanMat.put(2, 1, 1);
} else {
freemanMat.put(2,1, 0);
}
if (bwDrawing.get((int)y+1, (int)x+1)[0] != 0) {
freemanMat.put(2, 2, 1);
} else {
freemanMat.put(2,2, 0);
}
freemanChains.add(freemanMat);
// System.out.println(freemanMat.get(2, 2)[0]);
}
}
As marked by the comment in the code, im getting an NullPointer Exception the first time im checking for a pixel at x+1. And i do not understand why.
My overall goal is tho calculate a Freeman Chain Code of some contours, and i hope to achieve it with that somehow. Are there any other options in OpenCV to get the Freeman Chain Code since they removed it from the findContours() function?
Any help to both problems is much appreciated. Thank you.
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 months ago.
Improve this question
I've got a mana bean farm with different segments according of bean type each. There are golems with "Harvest" core and Ordo upgrade are harvesting them. They cannot reach the segments they are not bellongs. But sometimes I saw that in a neighbour segment is growing bean(s) with wrong aspect. In my case there are Vitium and Spiritus, but that is not important. Later I had an investigation of code and found something in it.
`
void harvest() {
this.count = 200;
final int md = this.blockMd;
final FakePlayer fp = FakePlayerFactory.get((WorldServer)this.theWorld, new GameProfile((UUID)null, "FakeThaumcraftGolem"));
fp.func_70107_b(this.theGolem.field_70165_t, this.theGolem.field_70163_u, this.theGolem.field_70161_v);
if (CropUtils.clickableCrops.contains(this.block.func_149739_a() + md)) {
this.block.func_149727_a(this.theWorld, this.xx, this.yy, this.zz, (EntityPlayer)fp, 0, 0.0f, 0.0f, 0.0f);
}
else {
this.theWorld.func_147480_a(this.xx, this.yy, this.zz, true);
if (this.theGolem.getUpgradeAmount(4) > 0) {
final ArrayList<ItemStack> items = new ArrayList<ItemStack>();
final ArrayList<Entity> drops = (ArrayList<Entity>)EntityUtils.getEntitiesInRange(this.theWorld, this.theGolem.field_70165_t, this.theGolem.field_70163_u, this.theGolem.field_70161_v, (Entity)this.theGolem, (Class)EntityItem.class, 6.0);
if (drops.size() > 0) {
for (final Entity e : drops) {
if (e instanceof EntityItem) {
if (e.field_70173_aa < 2) {
Vec3 v = Vec3.func_72443_a(e.field_70165_t - this.theGolem.field_70165_t, e.field_70163_u - this.theGolem.field_70163_u, e.field_70161_v - this.theGolem.field_70161_v);
v = v.func_72432_b();
e.field_70159_w = -v.field_72450_a / 4.0;
e.field_70181_x = 0.075;
e.field_70179_y = -v.field_72449_c / 4.0;
}
boolean done = false;
final EntityItem item = (EntityItem)e;
final ItemStack st = item.func_92059_d();
if (st.func_77973_b() != null && st.func_77973_b() == Items.field_151100_aR && st.func_77960_j() == 3) {
final int var5 = BlockDirectional.func_149895_l(this.blockMd);
final int par2 = this.xx + Direction.field_71583_a[var5];
final int par3 = this.zz + Direction.field_71581_b[var5];
final Block var6 = this.theWorld.func_147439_a(par2, this.yy, par3);
if (var6 == Blocks.field_150364_r && BlockLog.func_150165_c(this.theWorld.func_72805_g(par2, this.yy, par3)) == 3) {
final ItemStack itemStack = st;
--itemStack.field_77994_a;
this.theWorld.func_147465_d(this.xx, this.yy, this.zz, Blocks.field_150375_by, BlockDirectional.func_149895_l(this.blockMd), 3);
}
done = true;
}
else if (st.func_77973_b() != null && st.func_77973_b() == ConfigItems.itemManaBean) {
if (this.block.func_149707_d(this.theWorld, this.xx, this.yy, this.zz, 0)) {
final ItemStack itemStack2 = st;
--itemStack2.field_77994_a;
if (!st.func_77973_b().func_77648_a(st.func_77946_l(), (EntityPlayer)fp, this.theWorld, this.xx, this.yy + 1, this.zz, 0, 0.5f, 0.5f, 0.5f)) {
this.theWorld.func_147465_d(this.xx, this.yy, this.zz, ConfigBlocks.blockManaPod, 0, 3);
}
}
done = true;
}
else {
final int[] xm = { 0, 0, 1, 1, -1, 0, -1, -1, 1 };
final int[] zm = { 0, 1, 0, 1, 0, -1, -1, 1, -1 };
for (int count = 0; st != null && st.field_77994_a > 0 && count < 9; ++count) {
if (st.func_77973_b() != null && (st.func_77973_b() instanceof IPlantable || st.func_77973_b() instanceof ItemSeedFood) && st.func_77973_b().func_77648_a(st.func_77946_l(), (EntityPlayer)fp, this.theWorld, this.xx + xm[count], this.yy - 1, this.zz + zm[count], ForgeDirection.UP.ordinal(), 0.5f, 0.5f, 0.5f)) {
final ItemStack itemStack3 = st;
--itemStack3.field_77994_a;
}
}
}
if (st.field_77994_a <= 0) {
item.func_70106_y();
}
else {
item.func_92058_a(st);
}
if (done) {
break;
}
continue;
}
}
}
}
}
fp.func_70106_y();
this.theGolem.startActionTimer();
}
`
The key is in first 'else' is starts with condition "this.theGolem.getUpgradeAmount(4) > 0". I've seen in the different file that upgrade with 4 ID is exactly an Ordo upgrade, but I'm not familiar with Java and I can't see what's going on here and how to fix my problem with "beans shuffle". Is anybody have any clue how to override this logic or where actually in code is caused a problem? Preventively thanks for any answer
Well, I didn't get an answer after searching this code, but I accidently found that golem is trying to replace bean with dropped ones. That technicaly solved my problem by half. this.theWorld.func_147465_d(this.xx, this.yy, this.zz, ConfigBlocks.blockManaPod, 0, 3); This line is telling me about this, but it has no clue about specific aspect, random only
Here is my code down below, when in run it shows multiple boxes as I wanted but when I try to export, it only shows a single box at that frame position.I tried generating output at particular frame but its same issue
import processing.pdf.*;
int ofs = 500;
boolean record;
void setup() {
size(1900, 1080, P3D);
}
void draw() {
if (record) {
beginRaw(PDF, "box.pdf");
}
background(255);
translate(width/2, height/2, -800);
rotateZ(0.3);
rotateY(frameCount/500.0);
float x = sin(radians(frameCount*0.1));
for ( int xo = -ofs; xo<=ofs; xo += 100) {
for ( int yo = -ofs; yo<=ofs; yo += 100) {
pushMatrix();
translate ( xo, yo, 0);
box(200*x);
fill(155,150,90);
strokeWeight(3*x);
popMatrix();
if (record) {
endRaw();
record = false;
}
}
}
}
void keyPressed() {
if (key == 'r') {
record = true;
}
}
So close! :)
Perhaps the formatting might have made it harder to spot the issue.
You're calling endRaw(); (which wraps up pdf recording) nested in the for loops.
This means it's only picking up the first box since record is set to false so the other boxes are ignored.
I recommend using Edit > Auto Format (CMD + T / Ctrl + T).
Simply move the if(record) after the nested for loops:
import processing.pdf.*;
int ofs = 500;
boolean record;
void setup() {
size(1400, 580, P3D);
}
void draw() {
if (record) {
beginRaw(PDF, "box.pdf");
}
background(255);
translate(width/2, height/2, -800);
rotateZ(0.3);
rotateY(frameCount/500.0);
float x = sin(radians(frameCount*0.1));
for ( int xo = -ofs; xo<=ofs; xo += 100) {
for ( int yo = -ofs; yo<=ofs; yo += 100) {
pushMatrix();
translate ( xo, yo, 0);
box(200*x);
fill(155, 150, 90);
strokeWeight(3*x);
popMatrix();
}
}
// finish recording to PDF only after everything was drawn
if (record) {
endRaw();
record = false;
}
}
void keyPressed() {
if (key == 'r') {
record = true;
}
}
I am doing a project and would like to recreate a version of the image without noise. For example, this is the original image and I would like to produce an image that only displays the badminton court lines and net. This is the code that I wrote for it: `
public SimplePicture findBC()
{
Pixel[][] pixels = img_.getPixels2D();
SimplePicture result = new SimplePicture(img_.getWidth(), img_.getHeight());
int w = img_.getWidth();
int h = img_.getHeight();
boolean bird = false;
boolean line = false;
for(int r = 0; r < h; r++)
{
for(int c = 0; c < w; c++)
{
int ori = (pixels[r][c].getRed() + pixels[r][c].getGreen() + pixels[r][c].getBlue())/3;
if(pixels[r][c].getRed() >= 200 && pixels[r][c].getGreen() >= 200 && pixels[r][c].getBlue() <= 100)
bird = true;
else
bird = false;
if(ori > 195 || ori < 30)
line = true;
else
line = false;
if(line == true)
{
Color black = new Color(0, 0, 0, 0);
result.setBasicPixel(c, r, black.getRGB());
}
else if(bird==true)
{
Color birdie = new Color(255, 255, 0);
result.setBasicPixel(c, r, birdie.getRGB());
}
else if(!bird && !line)
{
Color grey = new Color(240, 240, 240, 0);
result.setBasicPixel(c, r, grey.getRGB());
}
}
}
return result;
}`
However, when I tested the code, it results in image that is noisy from glares and the lines further away are faded. If I know what the court should look like, how do I code that to let the computer know to look for key parts and recreate a cleaner version of the image? Please and thank you :)
I apologize for the somewhat vague title, I'm unsure what you would call this puzzle.
I'm making a path finding method to find the route with the least moves, not the distance traveled.
The rules of the game are simple, you must traverse from the orange square to the green square, but you can only move in a straight line, and cannot stop moving in that direction until you hit a boundary (either the wall of the arena or an obstacle), as if they were sliding across ice.
Example map, and unless I'm mistaken, the desired path (8 moves)
Arena.java: https://gist.github.com/CalebWhiting/3a6680d40610829b1b6d
ArenaTest.java: https://gist.github.com/CalebWhiting/9a4767508831ea5dc0da
I'm assuming this would be best handled with a Dijkstras or A* path finding algorithm, however I'm not only not very experienced with these algorithms, but also don't know how I would go about defining the path rules.
Thank you for any help in advance.
Here's my solution (Java) in case someone is still interested. As #tobias_k suggested in his comment above, indeed BFS is the way to go:
import java.util.LinkedList;
public class PokemonIceCave {
public static void main(String[] args) {
int[][] iceCave1 = {
{0, 0, 0, 1, 0},
{0, 0, 0, 0, 1},
{0, 1, 1, 0, 0},
{0, 1, 0, 0, 1},
{0, 0, 0, 1, 0}
};
System.out.println(solve(iceCave1, 0, 0, 2, 4));
System.out.println();
int[][] iceCave2 = {
{0, 0, 0, 1, 0},
{0, 0, 0, 0, 1},
{0, 1, 1, 0, 0},
{0, 1, 0, 0, 1},
{0, 0, 0, 1, 0},
{0, 0, 0, 0, 0}
};
System.out.println(solve(iceCave2, 0, 0, 2, 5));
}
public static int solve(int[][] iceCave, int startX, int startY, int endX, int endY) {
Point startPoint = new Point(startX, startY);
LinkedList<Point> queue = new LinkedList<>();
Point[][] iceCaveColors = new Point[iceCave.length][iceCave[0].length];
queue.addLast(new Point(0, 0));
iceCaveColors[startY][startX] = startPoint;
while (queue.size() != 0) {
Point currPos = queue.pollFirst();
System.out.println(currPos);
// traverse adjacent nodes while sliding on the ice
for (Direction dir : Direction.values()) {
Point nextPos = move(iceCave, iceCaveColors, currPos, dir);
System.out.println("\t" + nextPos);
if (nextPos != null) {
queue.addLast(nextPos);
iceCaveColors[nextPos.getY()][nextPos.getX()] = new Point(currPos.getX(), currPos.getY());
if (nextPos.getY() == endY && nextPos.getX() == endX) {
// we found the end point
Point tmp = currPos; // if we start from nextPos we will count one too many edges
int count = 0;
while (tmp != startPoint) {
count++;
tmp = iceCaveColors[tmp.getY()][tmp.getX()];
}
return count;
}
}
}
System.out.println();
}
return -1;
}
public static Point move(int[][] iceCave, Point[][] iceCaveColors, Point currPos, Direction dir) {
int x = currPos.getX();
int y = currPos.getY();
int diffX = (dir == Direction.LEFT ? -1 : (dir == Direction.RIGHT ? 1 : 0));
int diffY = (dir == Direction.UP ? -1 : (dir == Direction.DOWN ? 1 : 0));
int i = 1;
while (x + i * diffX >= 0
&& x + i * diffX < iceCave[0].length
&& y + i * diffY >= 0
&& y + i * diffY < iceCave.length
&& iceCave[y + i * diffY][x + i * diffX] != 1) {
i++;
}
i--; // reverse the last step
if (iceCaveColors[y + i * diffY][x + i * diffX] != null) {
// we've already seen this point
return null;
}
return new Point(x + i * diffX, y + i * diffY);
}
public static class Point {
int x;
int y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
#Override
public String toString() {
return "Point{" +
"x=" + x +
", y=" + y +
'}';
}
}
public enum Direction {
LEFT,
RIGHT,
UP,
DOWN
}
}
I think the best solution would probably be the BFS, where you represent the state of the board with a "State" object with the following parameters: number of moves made so far, and coordinates. It should also have a method to find the next states attainable (which should be fairly easy to code, just go N, S, E, W and return an array of the first blocking spots).
Create initial state (0 moves with initial coordinates)
Put in a priority queue (sorting by number moves)
while(priority queue has more states):
Remove node
if it is a goal state:
return the state
Find all neighbors of current state
Add them to priority queue (remembering to increment number of moves by 1)
This uses an implicit graph representation. Optimality is guaranteed because of the priority queue; when the goal state is found, it will have been reached with the fewest moves. If the whole priority queue is exhausted and no state is returned, then no solution exists. This solution takes O(V^2logV) time because of the priority queue, but I think this is the simplest to code. A straight up O(V) BFS solution is possible but you'll have to keep track of what states you have or have not visited yet and the fewest number of moves to reach them, which would take O(V) memory.
What I am trying to do is to convert a pixel from a video cam, into an image
to expalin it better imagine a 3d model so.. the pixels would be each polying, and i want to do is to conver each polyigon into an image.
What i have so far is this:
**
import processing.video.*;
PImage hoja;
Capture cam;
boolean uno, dos, tres, cuatro;
import ddf.minim.*;
Minim minim;
AudioPlayer audio;
float set;
void setup() {
//audio
minim = new Minim(this);
// audio = minim.loadFile("audio");
// audio.loop();
//
uno=false;
dos=false;
tres=false;
cuatro=true;
size(640, 480);
hoja=loadImage("hoja.gif");
cam = new Capture(this, width, height);
cam.start();
}
void draw() {
if (cam.available() == true) {
cam.read();
if (uno==true) {
filtroUno();
image(cam, 0, 0, 640, 480);
}
if (dos==true) {
filtroDos();
}
if(tres==true){
filtroTres();
}
if(cuatro==true){
filtroCuatro();
image(cam, set, 0,640,480);
}
}
// The following does the same, and is faster when just drawing the image
// without any additional resizing, transformations, or tint.
//set(0, 0, cam);
}
void filtroUno() {
cam.loadPixels();
hoja.loadPixels();
for (int i=0;i<cam.pixels.length;i++) {
if (brightness(cam.pixels[i])>110) {
cam.pixels[i]=color(0, 255, 255);
}
else {
cam.pixels[i]=color(255, 0, 0);
}
}
for (int i=0;i<cam.width;i+=10) {
for (int j=0;j<cam.height;j+=10) {
int loc=i+(j*cam.width);
if (cam.pixels[loc]==color(255, 0, 0)) {
for (int x=i;x<i+10;x++) {
for (int y=j;y<j+10;y++) {
// println("bla");
int locDos=i+(j*cam.width);
cam.pixels[locDos]=hoja.get(x, y);
}
}
}
}
}
cam.updatePixels();
}
**
The problem is that each pixel is creating me a matrix, so.. is not recreating what id that to do.
I had the method filtroUno but it wasn't showing ok..
and was the result
void filtroUno() {
cam.loadPixels();
hoja.loadPixels();
for (int i=0;i<cam.pixels.length;i++) {
if (brightness(cam.pixels[i])>110) {
cam.pixels[i]=color(0, 255, 255);
}
else {
cam.pixels[i]=color(255, 0, 0);
}
}
for (int i=0;i<cam.width;i+=10) {
for (int j=0;j<cam.height;j+=10) {
int loc=i+j*hoja.width*10;
if (cam.pixels[loc]==color(255, 0, 0)) {
for (int x=i;x<i+10;x++) {
for (int y=j;y<j+10;y++) {
// println("bla");
int locDos=x+y*hoja.height*10;
cam.pixels[locDos]=hoja.get(x, y);
}
}
}
}
}
cam.updatePixels();
}
i hope you can help me thanks
note: each red pixel should be the gif image the imge size is 10x10
I think what you are doing is looping through every 10th pixel in a webcam image and if the pixel is red you are placing the contents of a 10x10px gif over the webcam image with the top left corner of the gif positioned at the pixel that was red.
// loop through each 10th column in the camera
for (int i=0;i<cam.width;i+=10) {
// loop through each 10th row in the camera
for (int j=0;j<cam.height;j+=10) {
// calculate the pixel location at (i, j)
int loc=i+(j*cam.width);
// check the pixel is red
if (cam.pixels[loc]==color(255, 0, 0)) {
// loop through each column in the gif image
for (int x=0;x<10;x++) {
// loop through each row in the gif image
for (int y=0;y<10;y++) {
int locDos = (i + x) + ((j + y) * cam.width);
cam.pixels[locDos]=hoja.get(x, y);
}
}
}
}
}