setRGB cannot be resolved or is not a field - java

Trying to correct a set of "out of order pixels", listed in a XML file previously parsed in Array (number of column on x assis, number of detectors failed) in TIF image using interpolation of (x-1) and (x+ number of failed detectors)/2.
In Input I have a Tif image, as single layer (PAN, MS1, MS2, MS3, MS4) and the Array. In output I want the new image corrected.
I'm having problems with setRGB in :
new_img.setRGB = ((row + numOfDetectors) + (row - 1)/ 2);
and IOException e, in :
} catch (IOException e) {
Sorry, I'm new in Java language. Thanks ..
class ArrayInterpolatorNew {
public static void main(String[] args) {
String path = "E:/IES_Interpol/Resources/Images/ByPass_PAN_C.tif";
String path1 = "E:/IES_Interpol/Resources/Images/ByPass_PAN_C_interpol.tif";
BufferedImage old_img = null;
try { old_img = ImageIO.read(new File(path));}
catch (Exception e) { e.printStackTrace(); }
// Developer may get all the pixels
int width = old_img.getWidth(null);
int height = old_img.getHeight(null);
BufferedImage new_img = new BufferedImage( old_img.getWidth(),
old_img.getHeight(),
BufferedImage.TYPE_BYTE_GRAY);
Graphics gr = new_img.getGraphics();
gr.drawImage(old_img, 0, 0, null);
gr.dispose();
try {
File file = new File("Resources/Images/ByPass_PAN_C_interpol.tif"); // The file to save to.
String format = "TIF"; // Example: "PNG" or "JPG"
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
ImageIO.write(new_img, "TIF", new File(path1));
//}
//catch (IOException e) { e.printStackTrace(); }
byte pix[] = new byte[1];
int n = new_img.getWidth();
int m = new_img.getHeight();
final int firstRow = 1;
final int numOfDetectors = 1;
//int row = image1.getWidth();
//int col = image1.getHeight();
double[][] matrix = new double[n][m];
double[][] Array = new double[firstRow][numOfDetectors];
//swips the rows
for (int row = 0; row < n; ++row)
{
//swips the columns
for (int col = 0; col < m; ++col)
{
//matrix[row][col] = new_img.getRGB(row, col);
new_img.getRaster().getDataElements(row, col, pix);
matrix[row][col] = pix[0];
}
for(int J = 0; J < firstRow; J++)
{
//if (row != J){
// set a pixel
//int image = image1.getRGB( row, col, pixelValue );
//image.setRGB(x, y, pixelValue);
if (row == J){
System.out.println("x,y: " + row + ", " + J + "num of Detectors: " + numOfDetectors);
new_img.setRGB = ((row + numOfDetectors) + (row - 1)/ 2);
if (row == J && numOfDetectors > 20){
System.out.println("x,y: " + row + ", " + J + "num of Detectors: " + numOfDetectors + "then > 20");
//new_img.setRGB(x, y, pixelValue);
int col = new_img.getHeight();
new_img.getRaster().getDataElements(row, col, pix);
matrix[row][col] = pix[0];
}
}
}
try {
//ImageIO.write(new_img, format, file);
} catch (IOException e) {
e.printStackTrace();
} finally { System.out.println("Images were written succesfully.");}
System.out.println("rest of the code...");
}
} finally {
}
}
}

Related

Auto crop an image white border from all four side in Java

I want to crop all four side white spaces.
the easiest way to auto crop the white border out of an image in java? Thanks in advance...
public class TrimWhite {
public class TrimWhite {
private BufferedImage img;
public TrimWhite(File input) {
try {
img = ImageIO.read(input);
} catch (IOException e) {
throw new RuntimeException( "Problem reading image", e );
}
}
public void trim() {
int width = getTrimmedWidth();
int height = getTrimmedHeight();
BufferedImage newImg = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
Graphics g = newImg.createGraphics();
g.drawImage( img, 0, 0, null );
img = newImg;
}
public void write(File f) {
try {
ImageIO.write(img, "bmp", f);
} catch (IOException e) {
throw new RuntimeException( "Problem writing image", e );
}
}
private int getTrimmedWidth() {
int height = this.img.getHeight();
int width = this.img.getWidth();
int trimmedWidth = 0;
for(int i = 0; i < height; i++) {
for(int j = width - 1; j >= 0; j--) {
if(img.getRGB(j, i) != Color.WHITE.getRGB() &&
j > trimmedWidth) {
trimmedWidth = j;
break;
}
}
}
return trimmedWidth;
}
private int getTrimmedHeight() {
int width = this.img.getWidth();
int height = this.img.getHeight();
int trimmedHeight = 0;
for(int i = 0; i < width; i++) {
for(int j = height - 1; j >= 0; j--) {
if(img.getRGB(i, j) != Color.WHITE.getRGB() &&
j > trimmedHeight) {
trimmedHeight = j;
break;
}
}
}
return trimmedHeight;
}
public static void main(String[] args) {
TrimWhite trim = new TrimWhite(new File("C:\\Users\\Administrator\\Desktop\\New folder (2)\\Untitled.png"));
trim.trim();
trim.write(new File("C:\\Users\\Administrator\\Desktop\\New folder (2)\\test.png"));
}
}
}
I want output must crop all four side white spaces please help!

Numerical image recognition in Java

I want to recognize the numbers in the Pic1.
I did some work on it and it returns to pic2
Here is my code:
package captchadecproj;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
/*
* #author Mr__Hamid
*/
public class NewClass {
public static void main(String args[]) throws IOException {
int width = 110;
int heigth = 40;
BufferedImage image1 = new BufferedImage(width, heigth, BufferedImage.TYPE_INT_RGB);
BufferedImage num1 = new BufferedImage(width, heigth, BufferedImage.TYPE_INT_RGB);
BufferedImage image = null;
File f = null;
try {
f = new File("E:\\Desktop 2\\Captcha Project\\CaptchaDecoder\\captchaDecProj\\167.png");
image = new BufferedImage(width, heigth, BufferedImage.TYPE_INT_ARGB);
image = ImageIO.read(f);
System.out.println("Read!");
} catch (IOException e) {
System.out.println("Error" + e);
}
int[] pixel = null;
for (int y = 0; y < image.getHeight(); y++) {
for (int x = 0; x < image.getWidth(); x++) {
pixel = image.getRaster().getPixel(x, y, new int[3]);
if (pixel[0] < 30 & pixel[1] > 130 & pixel[2] < 110 & pixel[2] > 60) {
image1.setRGB(x, y, Integer.parseInt("ffffff".trim(), 16));
System.out.println(pixel[0] + " - " + pixel[1] + " - " + pixel[2] + " - " + (image.getWidth() * y + x));
} else {
image1.setRGB(x, y, 1);
System.out.println(pixel[0] + " - " + pixel[1] + " - " + pixel[2] + " - " + (image.getWidth() * y + x));
}
}
}
try {
f = new File("D:\\Original.jpg");
ImageIO.write(image, "jpg", f);
f = new File("D:\\black&White.jpg");
ImageIO.write(image1, "jpg", f);
System.out.println("Writed");
} catch (IOException e) {
System.out.println("Error" + e);
}
}
}
I have two questions:
How can I split these numbers?
How can I recognize which one is my number?
For example in the uploaded pic: 7, 1, 6
This is answer for first question, which is how to split numbers.
I recommend you something, is to convert your image to two dimensional array, then all operations will be perform much quicker than when you use BufferedImage.
BufferedImage image = ImageIO.read(new URL("http://i.stack.imgur.com/QaTj5.jpg"));
int startPos = 0, lastValue = 0;
Set<Integer> colours = new HashSet<>();
for (int x = 0; x < image.getWidth(); x++) {
int histValue = 0;
for (int y = 0; y < image.getHeight(); y++) {
colours.add(image.getRGB(x, y) );
if (image.getRGB(x, y) == 0xffffFFFF) {
histValue++;
}
}
if (histValue == 0 && lastValue == 0) {
startPos = x;
} else if (histValue == 0 && lastValue != 0) {
BufferedImage segment = image.getSubimage(startPos, 0, x
- startPos, image.getHeight());
ImageIO.write(segment, "jpg", new File("Segment" + startPos
+ ".jpg"));
}
lastValue = histValue;
}
if (lastValue!=0){
BufferedImage segment = image.getSubimage(startPos, 0, image.getWidth()
- startPos, image.getHeight());
ImageIO.write(segment, "jpg", new File("Segment" + startPos
+ ".jpg"));
}
Now all what you need to do is to find some decent algorithm for ocr.

Java - Why does ImageIO.read(filename) return null and the input image gets overwritten with 0 bytes?

I have several methods that manipulate a .jpg image: mirroring on x and y axis, tiling it, converting to ASCII text, and adjusting its brightness. All the methods work properly except the brightness one. When I run the brightness method, there is a NullPointerException in readGrayscaleImage() (see below - instructor written code). The BufferedImage is null, however it isn't when the method is called from any other of my methods (mirrors, tiling, ascii all read the file correctly). Not only is the BufferedImage null, the input image it is trying to read gets overwritten with 0 bytes and cannot be viewed in an image viewer, which would probably explain why the BufferedImage is null.
Here is a working method that calls readGrayscaleImage():
public static void tileImage(int h, int v, String infile, String outfile) {
int[][] result = readGrayscaleImage(infile);
int[][] tileResult = new int[result.length * h][result[0].length * v];
for (int hh = 0; hh < h; hh++) {
for (int vv = 0; vv < v; vv++) {
for (int i = 0; i < result.length; i++) {
for (int j = 0; j < result[i].length; j++) {
tileResult[i + hh * result.length][j + vv * result[i].length] = result[i][j];
}
}
}
}
writeGrayscaleImage(outfile, tileResult);
}
Here is the brightness method that results in the problem:
public static void adjustBrightness(int amount, String infile, String outfile) {
int[][] result = readGrayscaleImage(infile); // NullPointerException trace points here
for (int i = 0; i < result.length; i++) {
for (int j = 0; j < result[i].length; j++) {
if (result[i][j] + amount > 255)
result[i][j] = 255;
else if (result[i][j] + amount < 0)
result[i][j] = 0;
else
result[i][j] += amount;
}
}
writeGrayscaleImage(outfile, result);
}
Here is the instructor-written code that reads a greyscale .jpg file and returns an array of integers:
public static int[][] readGrayscaleImage(String filename) {
int [][] result = null; //create the array
try {
File imageFile = new File(filename); //create the file
BufferedImage image = ImageIO.read(imageFile);
int height = image.getHeight(); // NullPointerException POINTS HERE - image IS NULL
int width = image.getWidth();
result = new int[height][width]; //read each pixel value
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
int rgb = image.getRGB(x, y);
result[y][x] = rgb & 0xff;
}
}
}
catch (Exception ioe) {
System.err.println("Problems reading file named " + filename);
ioe.printStackTrace();
System.exit(-1);
}
return result; //once we're done filling it, return the new array
}
Here is the instructor written method to write a .jpg:
public static void writeGrayscaleImage(String filename, int[][] array) {
int width = array[0].length;
int height = array.length;
try {
BufferedImage image = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB); //create the image
//set all its pixel values based on values in the input array
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
int rgb = array[y][x];
rgb |= rgb << 8;
rgb |= rgb << 16;
image.setRGB(x, y, rgb);
}
}
//write the image to a file
File imageFile = new File(filename);
ImageIO.write(image, "jpg", imageFile);
}
catch (IOException ioe) {
System.err.println("Problems writing file named " + filename);
System.exit(-1);
}
}
(Wanted to comment but am not able to.)
BufferedImage image = ImageIO.read(filename);
Should this be
BufferedImage image = ImageIO.read(imageFile);
? I don't even see an override of read that takes a string.

Image won't show

When bigImg becomes an image it does show, but when sprites[dir] becomes an image it doesn't show.
When running, I drew a small box to show where the sprite will be and there is nothing inside it.
public void getSprite(){
BufferedImage bigImg = null;
try {
bigImg = ImageIO.read(new File("Pacman.png"));
} catch (IOException e) {
System.out.println("hey");
e.printStackTrace();
}
// The above line throws an checked IOException which must be caught.
final int width = 7;
final int height = 7;
final int rows = 4;
final int cols = 3;
BufferedImage[] sprites = new BufferedImage[rows * cols];
System.out.println(bigImg.getHeight() + "," + bigImg.getWidth());
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
sprites[(i * cols) + j] = bigImg.getSubimage(
j * width,
i * height,
width,
height
);
}
}
//image = bigImg;
}
public void setSprite (int direction){
System.out.println("HEY");
System.out.println(sprites[1].getHeight());
image = sprites[direction];
}

Error occurred while analyze pixel values on two similar images

I am a beginner to Java. Currently I want to analyze two similar images and check whether the images have different pixel values at position 2 and 4. I already develop some codes but when running it, the code produces error and it doesn't looping and check for all pixel values on both images.
For example, in the 9th pixel, image B pixel value at position 2 and 4 are not the same as image A pixel value. Then, whenever the code notices a differences between both images pixel value it will output the statement saying the pixel are not the same.
Here's the code:
public class getPixelRGB1
{
private static String[][] img_hex2;
private static String[][] img_hex4;
private static String[][] img2_hex2;
private static String[][] img2_hex4;
public static void main(String[] args) throws IOException
{
FileInputStream image = null;
FileInputStream image2 = null;
getPixelData1 newPD = new getPixelData1();
compareHexaRGB hexRGB = new compareHexaRGB();
try {
BufferedImage img, img2;
File file = new File("eye1.jpg");
File file2 = new File("eye2.jpg");
image = new FileInputStream(file);
image2 = new FileInputStream(file2);
img = ImageIO.read(image);
img2 = ImageIO.read(image2);
int rowcol;
int width = img.getWidth();
int height = img.getHeight();
hexRGB.compareHexaRGB(width, height);
System.out.println("Image's Width: " + width);
System.out.println("Image's Height: " + height);
//hexRGB.check();
int[][] pixelData = new int[width * height][3];
System.out.println("Pixel Data: " + pixelData);
int[] rgb;
int count = 0;
img_hex2 = new String[width][height];
img_hex4 = new String[width][height];
for(int i=0; i<width; i++)
{
for(int j=0; j<height; j++)
{
rgb = newPD.getPixelData(img, i, j);
for(int k = 0; k < rgb.length; k++)
{
pixelData[count][k] = rgb[k];
//img_hex2[i][j] = newPD.getHexa2();
//img_hex4[i][j] = newPD.getHexa4();
}
img_hex2[width][height] = newPD.getHexa2();
img_hex4[width][height] = newPD.getHexa4();
System.out.println("Output: " + img_hex2[i][j]);
System.out.println("Output: " + img_hex4[i][j]);
count++;
System.out.println("\nRGB Counts: " + count);
}
}
int width2 = img2.getWidth();
int height2 = img2.getHeight();
System.out.println("Image's Width: " + width2);
System.out.println("Image's Height: " + height2);
int[][] pixelData2 = new int[width2 * height2][3];
System.out.println("Pixel Data: " + pixelData2);
int[] rgb2;
int counter = 0;
img_hex2 = new String[width2][height2];
img_hex4 = new String[width2][height2];
for(int i=0; i<width2; i++)
{
for(int j=0; j<height2; j++)
{
rgb2 = newPD.getPixelData(img2, i, j);
for(int k = 0; k < rgb2.length; k++)
{
pixelData2[counter][k] = rgb2[k];
}
img2_hex2[width2][height2] = newPD.getHexa2();
img2_hex4[width2][height2] = newPD.getHexa4();
counter++;
System.out.println("\nRGB2 Counts: " + counter);
}
}
}
catch (FileNotFoundException ex) {
Logger.getLogger(getPixelRGB1.class.getName()).log(Level.SEVERE, null, ex);
}
finally
{
try {
image.close();
}
catch (IOException ex) {
Logger.getLogger(getPixelRGB1.class.getName()).log(Level.SEVERE, null, ex);
}
}
hexRGB.check();
}
public String[][] display_imgHex2()
{
return img_hex2;
}
public String[][] display_imgHex4()
{
return img_hex4;
}
public String[][] display_img2Hex2()
{
return img2_hex2;
}
public String[][] display_img2Hex4()
{
return img2_hex4;
}
}
//Get Pixel RGB Process
public class getPixelData1
{
private static final double bitPerColor = 4.0;
private static int red;
private static int green;
private static int blue;
private static String hexa2;
private static String hexa4;
public static int[] getPixelData(BufferedImage img, int w, int h) throws IOException
{
int argb = img.getRGB(w, h);
int rgb[] = new int[]
{
(argb >> 16) & 0xff, //red
(argb >> 8) & 0xff, //green
(argb ) & 0xff //blue
};
red = rgb[0];
green = rgb[1]; //RGB Value in Decimal
blue = rgb[2];
System.out.println("\nRGBValue in Decimal --> " + "\nRed: " + red + " Green: " + green + " Blue: " + blue);
//Convert each channel RGB to Hexadecimal value
String rHex = Integer.toHexString((int)(red));
String gHex = Integer.toHexString((int)(green));
String bHex = Integer.toHexString((int)(blue));
System.out.println("\nRGBValue in Hexa --> " + "\nRed Green Blue " + rHex + gHex + bHex);
return rgb;
}
public String getHexa2()
{
//Check position 2 of hexa value for any changes
String hex = String.format("%02X%02X%02X", red, green, blue);
System.out.println("\nString RGB Hexa: " + hex);
hexa2 = hex.substring(1,2);
System.out.println("\nSubstring at position 2: " + hexa2);
return hexa2;
}
public String getHexa4()
{
//Check position 4 of hexa value for any changes
String hex = String.format("%02X%02X%02X", red, green, blue);
System.out.println("\nString RGB Hexa: " + hex);
hexa4 = hex.substring(3,4);
System.out.println("\nSubstring at position 4: " + hexa4);
return hexa4;
}
}
//Compare 2 images process
public class compareHexaRGB
{
private static int w;
private static int h;
public static void compareHexaRGB(int width, int height) throws IOException
{
w = width;
h = height;
}
public void check()
{
getPixelRGB1 newPD = new getPixelRGB1();
for(int i = 0; i < w; i++)
{
for(int j = 0; j < h; j++)
{
if(newPD.display_imgHex2().equals(newPD.display_img2Hex2()) && (newPD.display_imgHex4().equals(newPD.display_img2Hex4())))
{
System.out.println("Pixel values at position 2 and 4 are the same.");
}
else if(!newPD.display_imgHex2().equals(newPD.display_img2Hex2()) || (!newPD.display_imgHex4().equals(newPD.display_img2Hex4())))
{
System.out.println("Pixel values at position 2 are not the same.");
}
else if(!newPD.display_imgHex2().equals(newPD.display_img2Hex2()) || (!newPD.display_imgHex4().equals(newPD.display_img2Hex4())))
{
System.out.println("Pixel values at position 4 are not the same.");
}
}
}
}
}
Errors:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
at getPixelRGB1.main(getPixelRGB1.java:79)
The change you have made is fundamentally incorrect. Please refer this updated class.
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
public class getPixelRGB1 {
private static String[][] img_hex2;
private static String[][] img_hex4;
private static String[][] img2_hex2;
private static String[][] img2_hex4;
public static void main(String[] args) throws IOException {
FileInputStream image = null;
FileInputStream image2 = null;
getPixelData1 newPD = new getPixelData1();
compareHexaRGB hexRGB = new compareHexaRGB();
try {
BufferedImage img, img2;
File file = new File("eye1.jpg");
File file2 = new File("eye2.jpg");
image = new FileInputStream(file);
image2 = new FileInputStream(file2);
img = ImageIO.read(image);
img2 = ImageIO.read(image2);
int rowcol;
int width = img.getWidth();
int height = img.getHeight();
hexRGB.compareHexaRGB(width, height);
System.out.println("Image's Width: " + width);
System.out.println("Image's Height: " + height);
// hexRGB.check();
int[][] pixelData = new int[width * height][3];
System.out.println("Pixel Data: " + pixelData);
int[] rgb;
int count = 0;
img_hex2 = new String[width][height];
img_hex4 = new String[width][height];
for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
rgb = newPD.getPixelData(img, i, j);
for (int k = 0; k < rgb.length; k++) {
pixelData[count][k] = rgb[k];
// img_hex2[i][j] = newPD.getHexa2();
// img_hex4[i][j] = newPD.getHexa4();
}
img_hex2[i][j] = newPD.getHexa2(); // the code runs and
// stops here
img_hex4[i][j] = newPD.getHexa4();
System.out.println("Output: " + img_hex2[i][j]);
System.out.println("Output: " + img_hex4[i][j]);
count++;
System.out.println("\nRGB Counts: " + count);
}
}
int width2 = img2.getWidth();
int height2 = img2.getHeight();
System.out.println("Image's Width: " + width2);
System.out.println("Image's Height: " + height2);
int[][] pixelData2 = new int[width2 * height2][3];
System.out.println("Pixel Data: " + pixelData2);
int[] rgb2;
int counter = 0;
img2_hex2 = new String[width2][height2];
img2_hex4 = new String[width2][height2];
for (int i = 0; i < width2; i++) {
for (int j = 0; j < height2; j++) {
rgb2 = newPD.getPixelData(img2, i, j);
for (int k = 0; k < rgb2.length; k++) {
pixelData2[counter][k] = rgb2[k];
}
img2_hex2[i][j] = newPD.getHexa2();
img2_hex4[i][j] = newPD.getHexa4();
counter++;
System.out.println("\nRGB2 Counts: " + counter);
}
}
} catch (FileNotFoundException ex) {
Logger.getLogger(getPixelRGB1.class.getName()).log(Level.SEVERE,
null, ex);
} finally {
try {
image.close();
} catch (IOException ex) {
Logger.getLogger(getPixelRGB1.class.getName()).log(
Level.SEVERE, null, ex);
}
}
hexRGB.check();
}
public String[][] display_imgHex2() {
return img_hex2;
}
public String[][] display_imgHex4() {
return img_hex4;
}
public String[][] display_img2Hex2() {
return img2_hex2;
}
public String[][] display_img2Hex4() {
return img2_hex4;
}
}
One possible cause is the four uninitialized variables at the top.
private static String[][] img_hex2;
private static String[][] img_hex4;
private static String[][] img2_hex2;
private static String[][] img2_hex4;
The above four variables are accessed without allocating any memory.
A quick fix is to initialize them before the two for loops that use them.
img_hex2 = new String[width][height];
img_hex4 = new String[width][height];
for(int i=0; i<width; i++)
{
for(int j=0; j<height; j++)
{
.....
img2_hex2 = new String[width2][height2];
img2_hex4 = new String[width2][height2];;
for(int i=0; i<width2; i++)
{
for(int j=0; j<height2; j++

Categories