How to sharpen a image in android? - java

I want to sharpen a image, my code is given below:
public Bitmap RuiHuaBitmap(Bitmap bitmap) {
int width, height;
height = bitmap.getHeight();
width = bitmap.getWidth();
int red, green, blue;
int a1, a2, a3, a4, a5, a6, a7, a8, a9;
Bitmap bmpBlurred = Bitmap.createBitmap(width, height,bitmap.getConfig());
Canvas canvas = new Canvas(bmpBlurred);
canvas.drawBitmap(bitmap, 0, 0, null);
for (int i = 1; i < width - 1; i++) {
for (int j = 1; j < height - 1; j++) {
a1 = bitmap.getPixel(i - 1, j - 1);
a2 = bitmap.getPixel(i - 1, j);
a3 = bitmap.getPixel(i - 1, j + 1);
a4 = bitmap.getPixel(i, j - 1);
a5 = bitmap.getPixel(i, j);
a6 = bitmap.getPixel(i, j + 1);
a7 = bitmap.getPixel(i + 1, j - 1);
a8 = bitmap.getPixel(i + 1, j);
a9 = bitmap.getPixel(i + 1, j + 1);
red = (Color.red(a1) + Color.red(a2) + Color.red(a3) + Color.red(a4) + Color.red(a6) + Color.red(a7) + Color.red(a8) + Color.red(a9)) *(-1) + Color.red(a5)*9 ;
green = (Color.green(a1) + Color.green(a2) + Color.green(a3) + Color.green(a4) + Color.green(a6) + Color.green(a7) + Color.green(a8) + Color.green(a9)) *(-1) + Color.green(a5)*9 ;
blue = (Color.blue(a1) + Color.blue(a2) + Color.blue(a3) + Color.blue(a4) + Color.blue(a6) + Color.blue(a7) + Color.blue(a8) + Color.blue(a9)) *(-1) + Color.blue(a5)*9 ;
bmpBlurred.setPixel(i, j, Color.rgb(red, green, blue));
}
}
return bmpBlurred;
}
but I cannot get the ideal effect. Can someone give more clue, or tell me what is mistake in my code?
Thank you.

You are missing range checking on the rgb values passed to Color.rgb(); you need to normalize rgb values in the [0..255] range before calling Color.rgb() method:
public static int rgb (int red, int green, int blue) Since: API Level
1
Return a color-int from red, green, blue components. The alpha
component is implicity 255 (fully opaque). These component values
should be [0..255], but there is no range check performed, so if they
are out of range, the returned color is undefined. Parameters red Red
component [0..255] of the color green Green component [0..255] of the
color blue Blue component [0..255] of the color
Your convolution matrix seems good for a shapen transformation:
0 0 0 0 0
0 -1 -1 -1 0
0 -1 9 -1 0
0 -1 -1 -1 0
0 0 0 0 0
if you think your effect is too strong, you could also try with:
0 0 0 0 0
0 0 -1 0 0
0 -1 5 -1 0
0 0 -1 0 0
0 0 0 0 0
as an alternative

red = (Color.red(a1) + (Color.red(a2))*(-1) + Color.red(a3) +
(Color.red(a4))*(-1) + (Color.red(a6))*(-1) + Color.red(a7) +
(Color.red(a8))*(-1) + Color.red(a9)) + Color.red(a5)*5 ;
if(red < 0 )
red = 0;
if(red > 255)
red = 255;
green = (Color.green(a1) + (Color.green(a2)) *(-1) + Color.green(a3) +
(Color.green(a4)) *(-1) + (Color.green(a6))*(-1) + Color.green(a7) +
(Color.green(a8))*(-1) + Color.green(a9)) + Color.green(a5)*5 ;
if(green < 0 )
green = 0;
if(green > 255)
green = 255;
blue = (Color.blue(a1) + (Color.blue(a2))*(-1) + Color.blue(a3) +
(Color.blue(a4))*(-1) + (Color.blue(a6))*(-1) + Color.blue(a7) +
(Color.blue(a8)) *(-1) + Color.blue(a9)) + Color.blue(a5)*5 ;
if(blue < 0 )
blue = 0;
if(blue > 255)
blue = 255;

Related

How read every pixel of image and take couple of last bits from every color channel(RGBA)

So i'm trying to get a byte array of compressed data in an image, the maker told me that I need to read every color channel.
Here's my code so far:
byte[] RGBAb = null;
for (int x = 0; x < bitmap.getWidth(); x++)
{
for (int y = 0; y < bitmap.getHeight(); y++)
{
int mask = 0xFF;
Integer R = Math.abs(bitmap.getPixel(x, y) >> 16 & mask);
Integer G = Math.abs(bitmap.getPixel(x, y) >> 8 & mask);
Integer B = Math.abs(bitmap.getPixel(x, y) & mask);
Integer A = Math.abs(bitmap.getPixel(x, y) >> 24 & mask);
byte Rb = R.byteValue();
byte Gb = G.byteValue();
byte Bb = B.byteValue();
byte Ab = A.byteValue();
RGBAb = new byte[] { Rb, Gb, Bb, Ab};
//System.out.println("Red" + R + "\n Red to bytes: " + Rb);
//System.out.println("Green" + G + "\n Green to bytes: " + Gb);
//System.out.println("Blue" + B + "\n Blue to bytes: " + Bb);
//System.out.println("Alpha" + A + "\n Alpha to bytes: " + Ab);
System.out.println("RGBA Bytes COMBINED \n" + RGBAb);
}
}
I dont know if it's outputting the right value.
This is the photo im reading:
Click Me

Cycle through all the pixels and obtain the number of black and white pixels in an image

could you help me, you can tell me what I'm wrong with, my code has 2 for's that run through a whole 157x127 pixel image, from which I get black and white through RGB; in turn I have 2 counters of those two colors.
As I understand it, if the 157x127 measurement gives 19,939 pixels, of which there is black and white; but adding the counters gives me less (14218 for white and 5643 for black). Where am I wrong or is my logic wrong?
Add the image:
enter image description here
The application does not close itself
Code below:
BitmapDrawable drawable = (BitmapDrawable) imgviewREXMorphologEX.getDrawable();
Bitmap bitmap = drawable.getBitmap();
int Width = bitmap.getWidth();
int Height = bitmap.getHeight();
int contadorB = 0;
int contadorN = 0;
for (int x = 0; x <= Width - 1; x++) {
int xi = x;
for (int y = 0; y <= Height - 1; y++) {
int yi = y;
int coordenada = bitmap.getPixel(xi, yi);// pixel
double r = Color.red(coordenada);
double g = Color.green(coordenada);
double b = Color.blue(coordenada);
if (r == 255 && g == 255 && b == 255) {
contadorB++;
} else if (r == 0 && g == 0 && b == 0) {
contadorN++;
}
txtviewcolor3.setText(contadorB + " - " + contadorN + " - " + (contadorB + contadorN) + " - " + (Width * Height));
}
}
Add an else clause to your if statements, and see if there are actually some other colours in the image?
Your image is not just black or white!
See here an amplified section (800%, bottom right):
There are some pixels that are gray (brighter and darker ones). Depending on purpose, some limits can help (e.g. r < MIN && g < MIN && b < 10) or using the Formula to determine brightness of RGB color compared to some limit(s)

Convert JPEG byte[] to NV21 byte[]

I am trying to convert JPEG byte[] data from Camera.PictureCallback to NV21 byte[] format but it didn't work.
I try to do this:
byte [] getNV21(int inputWidth, int inputHeight, Bitmap scaled) {
int [] argb = new int[inputWidth * inputHeight];
scaled.getPixels(argb, 0, inputWidth, 0, 0, inputWidth, inputHeight);
byte [] yuv = new byte[inputWidth*inputHeight*3/2];
encodeYUV420SP(yuv, argb, inputWidth, inputHeight);
scaled.recycle();
return yuv;
}
void encodeYUV420SP(byte[] yuv420sp, int[] argb, int width, int height) {
final int frameSize = width * height;
int yIndex = 0;
int uvIndex = frameSize;
int a, R, G, B, Y, U, V;
int index = 0;
for (int j = 0; j < height; j++) {
for (int i = 0; i < width; i++) {
a = (argb[index] & 0xff000000) >> 24; // a is not used obviously
R = (argb[index] & 0xff0000) >> 16;
G = (argb[index] & 0xff00) >> 8;
B = (argb[index] & 0xff) >> 0;
// well known RGB to YUV algorithm
Y = ( ( 66 * R + 129 * G + 25 * B + 128) >> 8) + 16;
U = ( ( -38 * R - 74 * G + 112 * B + 128) >> 8) + 128;
V = ( ( 112 * R - 94 * G - 18 * B + 128) >> 8) + 128;
// NV21 has a plane of Y and interleaved planes of VU each sampled by a factor of 2
// meaning for every 4 Y pixels there are 1 V and 1 U. Note the sampling is every other
// pixel AND every other scanline.
yuv420sp[yIndex++] = (byte) ((Y < 0) ? 0 : ((Y > 255) ? 255 : Y));
if (j % 2 == 0 && index % 2 == 0) {
yuv420sp[uvIndex++] = (byte)((V<0) ? 0 : ((V > 255) ? 255 : V));
yuv420sp[uvIndex++] = (byte)((U<0) ? 0 : ((U > 255) ? 255 : U));
}
index ++;
}
}
}

getsubimage() function in java

this is my code :
for (int ii = 0; ii <= 17; ii++) {
System.out.println(ii);
if(ii == 10) continue;
String name = String.valueOf(ii);
File pic = new File(name + ".jpg");
BufferedImage image = ImageIO.read(pic);
int w ;
int h ;
int x_max = 0;
int x_min = 1000;
int y_max = 0;
int y_min = 1000;
for (int i = 0; i < image.getWidth(); i++) {
for (int j = 0; j < image.getHeight(); j++) {
Color c = new Color(image.getRGB(i, j));
if(c.getBlue() == 0){
x_max = Math.max(x_max, i);
x_min = Math.min(x_min, i);
y_max = Math.max(y_max, j);
y_min = Math.min(y_min, j);
}
}
}
BufferedImage imagea = image;
image = imagea.getSubimage(x_min - 1, y_min - 1,x_max - x_min + 3 , y_max - y_min + 3);
h = Math.abs(y_max - y_min);
w = Math.abs(x_max - x_min);}
and this is output :
0
1
2
3
4
5
6
7
Exception in thread "main" java.awt.image.RasterFormatException: (x + width) is outside of Raster
at sun.awt.image.ByteInterleavedRaster.createWritableChild(Unknown Source)
at java.awt.image.BufferedImage.getSubimage(Unknown Source)
at element.main(element.java:41)
the error is about "getsubimage" function but i dont know its reason.
the code work until eighth image and dont work for it.
how work "getsubimage" function in java?
This line:
image = imagea.getSubimage(x_min - 1, y_min - 1,x_max - x_min + 3 , y_max - y_min + 3);
Assumes that the resulting coordinates are at least 1 pixel offset from the left and upper edge, and 3 pixels offset from the left and bottom edge. Don't assume that, or make sure they will.

How to convert ARGB to RGB array of bytes?

I have a byte array:
byte[] blue_color = {-1,0,112,-64};
How to convert into byte array of RGB?
And also how can I get the real RGB value of the color?
Assuming it's the first element that's the A component:
byte[] rgb = Arrays.copyOfRange(blue_color, 1, 4);
To get the "real" colour values, you need to undo the two's complement representation:
int x = (int)b & 0xFF;
How to convert ARGB array into RGB?
byte[] argb = ...;
byte[] rgb = new byte[(argb.length / 4) * 3];
int index = rgb.length - 1;
for (int i = argb - 1; i >= 0; i -= 4) {
rgb[index--] = argb[i];
rgb[index--] = argb[i - 1];
rgb[index--] = argb[i - 2];
}
How to print integer value:
byte[] oneColor = {..., ..., ..., ...};
int alpha = oneColor[0] & 0xFF;
int red = oneColor[1] & 0xFF;
int green = oneColor[2] & 0xFF;
int blue = oneColor[3] & 0xFF;
System.out.println("Color: " + alpha + ", " + red + ", " + green ", " + blue);
System.out.println("Hexa color: 0x" + Integer.toHexString(alpha) + " " + Integer.toHexString(red) + " " + Integer.toHexString(green) + " " + Integer.toHexString(blue));
Could be done prettier with printf.
How to convert into byte array of RGB?
byte[] rgb = new byte[3];
System.arraycopy(blue_color, 1, rgb, 0, 3);
And also how can I get the real RGB value of the color?
int red = rgb[0] >= 0 ? rgb[0] : rgb[0] + 256;
int green = rgb[1] >= 0 ? rgb[1] : rgb[1] + 256;
int blue = rgb[2] >= 0 ? rgb[2] : rgb[2] + 256;

Categories

Resources