How to process information in an image? - java

I want to write a java program so that when I capture the image of any one face of a Rubik's Cube, it tells which color is present on which tile. I dont want to use any prewritten library/api. I want to write the code myself. I want to ask how I should go about....I mean the steps.
Thanks in advance!

You can do something like this it analyzing one pixel at the time
img = ImageIO.read(new File("/mydir/pic.png"));
for (int y = 0; y < img.getHeight(); y++) {
for (int x = 0; x < img.getWidth(); x++) {
int rgb = img.getRGB(x, y);
if (rgb == Color.RED.getRGB()) {
//Do stuff
} else if (rgb == Color.GREEN.getRGB()){
//Do more stuff
}
}
}

If the size of face of the cube in the image is variable then its not an easy task. Otherwise you can just use #urag's code but instead of checking for all pixels just check only 6 pixels in a row with an offset of tile width starting from first center of 1st tile.

Related

How to change colors of a Java Buffered Image

I have a JavaBufferedImage. The foreground is Black and the background is transparent. I would like to recolor the image as Red.
I have read through other people's postings on this and tried using this code, but my Image winds up completey transparent when I run it.
Does anyone have any ideas? I am new to the Java 2D Image processing library. Thanks.
imageIcon= new ImageIcon(getImageURL("/ImagesGun/GunBase.png"));
gunBaseImage= Utilities.toBufferedImage(imageIcon.getImage());
int red = 0x00ff0000;
int green = 0x0000ff00;
int blue = 0x000000ff;
int width = gunBaseImage.getWidth();
int height = gunBaseImage.getHeight();
//Loop through the image and set the color to red
for(int x = 0; x < width; x++){
for(int y = 0; y < height; y++){
long pixel = gunBaseImage.getRGB(x, y);
if(pixel != 0){
red = 0x00ff0000;
gunBaseImage.setRGB(x,y,red);
}
}
}
You are using a fully transparent value of red. The first entry in the color definition is the alpha value. If you want a fully opaque color you need to use ff as the first value. Therefore your red should be 0xffff0000, your green 0xff00ff00 and so on. This also means that black is ff000000.

parts of the images in rectangle shape

We are trying to get only the portion of the image out of the captured image. But in java we only get subimage in rectangular form using image.getImage(x,y,width, height). Let say if i virutally split the image as 10 parts as shown below. How can i able to extract only 1,2,4,6,8,9,10 out of it as show in the second image using native java very without consuming too many resources and time.
Update
Below is the sample code
for (int x = 0; x < columns; x++) {
for (int y = 0; y < rows; y++) {
imagePart = img.getSubimage(x * this.smallWidth, y
* this.smallHeight, this.smallWidth,
this.smallHeight);
if (!ifSelectedPart(imagePart)) {
smallImages[x][y] = imagePart;
}
else {
smallImages[x][y] = fillwithAlpha();
}
}
createImage(smallImages[][])
If these rectangles are all the same size you can treat the image as a grid and calculate what region of the image you need with a little math.
int numberColumns = 2;
int numberRows = 5;
public Rectangle getSubregion(int row, int column, int imgWidth, int imgHeight){
int cellWidth = imgWidth / numberColumns;
int cellHeight = imgHeight / numberRows;
return new Rectangle(column*cellWidth, row*cellHeight,cellWidth, cellHeight);
}
//usage
Rectangle cellOne = getSubregion(0, 0, img.getWidth(),img.getHeight());
Then just render each of those subregions to a new image in memory.
Images are by their nature rectangular. Perhaps you wish to draw over the image with 0 alpha composite color to cover up the region that you don't want to see. Either that or create a grid of rectangular sub-images, and keep the ones from the grid that you want to display.

Webcam Pixel Manipulation/Sorting via Processing

I am attempting to write a Processing sketch that will take each row's center pixel's color and apply that color to the entire row. However, I am having trouble with even getting the pixels to change. It seems like the sketch doesn't even go through the for-loops where I am trying to change the pixels because it doesn't print out any of the statements except the end draw at the end of draw(). I just end up with an unmanipulated feed. Does anyone know why this isn't working?
Also, currently using Processing's standard Video library with Capture at the moment, but if there is a better library that I could utilize please let me know! Thanks!
UPDATE: Testing out my algorithm with an array of numbers, it seems like using an inner for loop isn't working like how I thought it should. The i of the outer-loop is only incremented once after the first time the inner for loop completes itself, and then it just exits the outer loop instead of starting the inner loop again. What's going on here?
import processing.video.*;
Capture feed; // webcam
int pixelCount = width * height; // total # of pixels
int center = width / 2; // value for center pixel
int widthPlus = width + 1; // value to go row-to-row
color c; // center pixel color
void setup(){
size(displayWidth, displayHeight);
feed = new Capture(this);
feed.start();
}
void draw(){
if (feed.available() == true){
feed.read();
}
image(feed, 0, 0);
feed.loadPixels(); // load pixels from webcam
/** Use to look at each row one at a time*/
for (int i = 0; i < pixelCount; i+=widthPlus){
println("Outer for-loop");
c = feed.pixels[i + center]; //get center pixel
/** Make each pixel in row the color of 'c' */
for (int j = i; j < width; j++){
println("Inner for-loop");
feed.pixels[j] = c; // set pixel to 'c'
println(i + " - " + j);
}
}
feed.updatePixels(); // update pixels from webcam
println("end draw");
}

if/then with pixel finding and looping, all of which is above me at the moment

I want to take a screenshot, and if the pixel is the correct value RGB then take another screenshot and find next pixel or else repeat.
this is the code to get the pixel and it works like a charm!
{
BufferedImage image = robot.createScreenCapture(rectangle);
search: for(int x = 0; x < rectangle.getWidth(); x++)
{
for(int y = 0; y < rectangle.getHeight(); y++)
{
if(image.getRGB(x, y) == color3.getRGB())
{
break search;
}
}
}
}
what i want to know i guess is how would i go about asking it to repeat this segment of code until the pixel equals the true color. the color i am looking for is:
Color color3 = new Color(114, 46, 33);
Ok context, i am building a program that goes through steps, one opens the given puzzle, i have that down because i can use simple pixel data, then it needs to center the mouse on the center pixel. The problem is i cant just use a second get pixel image because it takes a while for the game to open the relevant jpanel so i need my program to wait until it can find a pixel indicating the game is open before it starts to look for the pixel to center the mouse.
You can probably separate the screenshot code into a method and call it until you get the desired result:
public boolean checkColor(Color inputColor) {
BufferedImage image = robot.createScreenCapture(rectangle);
for(int x = 0; x < rectangle.getWidth(); x++) {
for (int y = 0; y < rectangle.getHeight(); y++) {
if (image.getRGB(x, y) == inputColor.getRGB()) {
return true;
}
}
}
return false;
}
This method will return true if it can find the given inputColor in the screenshot. You might then use it in a loop as follows:
Color newColor = ...;
while (!checkColor(newColor)) {
new Color = new Color(114, 46, 33);
// Or change color in here for every iteration
}
This loop will terminate if it can't match the screenshot to newColor.

Change a Specific Color in an ImageIcon

I am working with 24x24 pixel icons. And I would like to be able to change a specific color within this icon to a different color. For example turn the white areas to red.
I don't know of an API method that does that. And by default, Images are not writable. However, if you have a BufferedImage, you could do it like this:
public void changeColor(BufferedImage img, Color old, Color new) {
final int oldRGB = old.getRGB();
final int newRGB = new.getRGB();
for (int x = 0; x < img.getWidth(); x++) {
for (int y = 0; y < img.getHeight(); y++) {
if (img.getRGB(x, y) == oldRGB)
img.setRGB(x, y, newRGB);
}
}
}
This is not the most efficient way to do it (it's possible to fetch RGB data into an array instead of one pixel at a time), but for 24x24 images it shouldn't be a problem.
You can do this with a BufferedImage. Take a look at the Java Image I/O documentation.

Categories

Resources