Color correction on android - android

i am new in android programming. Now i am doing a color correction program using java for android platform.
In the program, i should be able to select a point on the bitmap and tell the program that it is actually white color, and the program will re-adjust all the pixels of that bitmap so that all the colors of that bitmap will be correct.
Can anyone tell me how can i do this? I am already able to retrieve a point from a bitmap now and calculate its RGB, but i got no idea how i can carry on. Please show me some examples or articles that i can read up on.
Thank you so much for your precious time. Hope to hear from you soon!
Resulting photo :
http://www.flickr.com/photos/92325795#N02/8392038944/in/photostream
My photo is being updated and despite of the quality/noise/color, there are the weird colors here and there. Anyone have any idea what i should do to remove it? Or even better improve on the method i am using? Heres the code:
The input is the bitmap to edit, inColor is the color of the nose in the photo to be edited, reqcolor is the color of my nose in the sample/optimum photo.
public Bitmap shiftRGB(Bitmap input, int inColor, int reqColor){
int deltaR = Color.red(reqColor) - Color.red(inColor);
int deltaG = Color.green(reqColor) - Color.green(inColor);
int deltaB = Color.blue(reqColor) - Color.blue(inColor);
//--how many pixels ? --
int w = input.getWidth();
int h = input.getHeight();
//-- change em all! --
for (int i = 0 ; i < w; i++){
for (int j = 0 ; j < h ; j++ ){
int pixColor = input.getPixel(i,j);
//-- colors now ? --
int inR = Color.red(pixColor);
int inG = Color.green(pixColor);
int inB = Color.blue(pixColor);
if(inR > 255){ inR = 255;}
if(inG > 255){ inG = 255;}
if(inB > 255){ inB = 255;}
if(inR < 0){ inR = 0;}
if(inG < 0){ inG = 0;}
if(inB < 0){ inB = 0;}
//-- colors then --
input.setPixel(i,j,Color.argb(255,inR + deltaR,inG + deltaG,inB + deltaB));
}
}
return input;
}
Thank you so much for help me! I cant express my gratitude further than saying another thank you in advance!

Read about some white-balancing algorithms. See if you can implement some. Also note that android does not provides awt.graphics / BufferedImage API, used in majority of java tutorials out there.
Android provides ColorMatrixColorFilter, for such use, discussed here.
A basic crude way to manipulate pixels:
public Bitmap shiftRGB(Bitmap input, int inColor, int reqColor){
//--how much change ? --
int deltaR = Color.red(reqColor) - Color.red(inColor);
int deltaG = Color.green(reqColor) - Color.green(inColor);
int deltaB = Color.blue(reqColor) - Color.blue(inColor);
//--how many pixels ? --
int w = input.getWidth();
int h = input.getHeight();
//-- change em all! --
for (int i = 0 ; i < w; i++){
for (int j = 0 ; j < h ; j++ ){
int pixColor = input.getPixel(i,j);
//-- colors now ? --
int inR = Color.red(pixColor);
int inG = Color.green(pixColor);
int inB = Color.blue(pixColor);
//-- colors then --
input.setPixel(i,j,Color.argb(255,inR + deltaR,inG + deltaG,inB + deltaB));
}
}
//-- all done--
return input;
}

Related

Improper Computation of NDVI in Android

I am new to image processing. I am trying to implement NDVI index on an image of agricultural farm in Android. But, I am unable to get the desired result.
Original Image:
I am using this code snippet to compute NDVI:
public int[][] applyNDVI(int[][] src1, int[][] src2) {
final int pixels[][] = new int[width][height];
for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
//src1 is the image in red channel and src2 is the image in the blue channel.
final int pixel1 = src1[i][j];
final int pixel2 = src2[i][j];
final double X = (pixel2 - pixel1);
final double Y = (pixel2 + pixel1);
double Z = (X / Y);
//double Z = ((double)pixel1/(double)pixel2);
int NDVI;
NDVI = (int) (Z * 255);
pixels[i][j] = Color.argb(Color.alpha(pixel1), NDVI, NDVI, NDVI);
}
}
return pixels;
}
This code snippet is giving me this output:
From what I have read about NDVI, vegetation has a higher value of NDVI (between 0.2 and 0.9) as compared to barren/dry ground (<0.2). So, plants should be light grey and ground should be dark grey in the NDVI image. But I am getting and exactly opposite image.
Also when I am using a different NDVI formula i.e. NDVI = Red Channel / Blue Channel`i.e.
double Z = ((double)pixel1/(double)pixel2);`
This formula gives me the desired output (almost).
So, I am unable to understand this discrepancy. Also, I want to know if the NDVI formula depends on the camera used and the image taken? Or is the formula of NDVI universal? If it is universal, where am I going wrong and why am not I able to get the desired output. Any kind of help would be deeply appreciated.

OpenCV convert color per pixel

Hello i want to convert the color in image, i'm using per-pixel methods but it seems very slow
src.getPixels(pixels, 0, width, 0, 0, width, height);
// RGB values
int R;
for (int i = 0; i < pixels.length; i++) {
// Get RGB values as ints
// Set pixel color
pixels[i] = color;
}
// Set pixels
src.setPixels(pixels, 0, width, 0, 0, width, height);
my question, is there any way i can do it using openCV? change pixel to the color i want ?
I recommend this excellent article on how to access/modify an opencv image buffer. I recommend
"the efficient way":
int i,j;
uchar* p;
for( i = 0; i < nRows; ++i)
{
p = I.ptr<uchar>(i);
for ( j = 0; j < nCols; ++j)
{
p[j] = table[p[j]];
}
Or "the iterator-safe method":
MatIterator_<Vec3b> it, end;
for( it = I.begin<Vec3b>(), end = I.end<Vec3b>(); it != end; ++it)
{
(*it)[0] = table[(*it)[0]];
(*it)[1] = table[(*it)[1]];
(*it)[2] = table[(*it)[2]];
}
For further optimizations, using cv::LUT() (where possible) can give huge speedups, but it is more intensive to design/code.
You can access Pixels by using:
img.at<Type>(y, x);
So to change an RGB Value you can use:
// read color
Vec3b intensity = img.at<Vec3b>(y, x);
// compute new color using intensity.val[0] etc. to access color values
// write new color
img.at<Vec3b>(y, x) = intensity;
#Boyko mentioned an Article from OpenCV concerning fast access to the image pixels if you want to iterate over all Pixel. The Method I would prefer from this Article is the iterator Method, as it is only slightly slower than direct pointer access but safer to use.
Example Code:
Mat& AssignNewColors(Mat& img)
{
// accept only char type matrices
CV_Assert(img.depth() != sizeof(uchar));
const int channels = img.channels();
switch(channels)
{
// case 1: skipped here
case 3:
{
// Read RGG Pixels
Mat_<Vec3b> _img = img;
for( int i = 0; i < img.rows; ++i)
for( int j = 0; j < img.cols; ++j )
{
_img(i,j)[0] = computeNewColor(_img(i,j)[0]);
_img(i,j)[1] = computeNewColor(_img(i,j)[1]);
_img(i,j)[2] = computeNewColor(_img(i,j)[2]);
}
img = _img;
break;
}
}
return img;
}

How to detect color line in image using android java

If I have an image containing black lines, like the image below. How can I detect the black lines.( to know the x,y of dots on this lines) ?
I want to know how to get started with this topic. What library should I learn to go through
You can use Catalano Framework.
http://code.google.com/p/catalano-framework/
FastBitmap image = new FastBitmap(bitmap);
image.toGrayscale();
int width = image.getWidth();
int height = image.getHeight();
ArrayList<IntPoint> lst = new ArrayList<>();
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
if (image.getGray(i, j) == 0)
lst.add(new IntPoint(i,j));
}
}

Compare RGB color values of two images in android

To get RGB values of one image i used the follwing code snippet
int[] pix = new int[picw * pich];
bitmap.getPixels(pix, 0, picw, 0, 0, picw, pich);
int R, G, B,Y;
for (int y = 0; y < pich; y++){
for (int x = 0; x < picw; x++)
{
int index = y * picw + x;
int R = (pix[index] >> 16) & 0xff; //bitwise shifting
int G = (pix[index] >> 8) & 0xff;
int B = pix[index] & 0xff;
//R,G.B - Red, Green, Blue
//to restore the values after RGB modification, use
//next statement
pix[index] = 0xff000000 | (R << 16) | (G << 8) | B;
}}
I want to compare two images,i know that comparing pixel values would be more expensive.I also analysed OpenCV library but i won't
get into my requirement.
Is there any algorithm to compare images using RGB values in android?
or
Is any other method to compare RGB values?
Thanks,
I'm not sure what your requirements are, but if all you want to do is compare the (RGB) color palettes of two images, you might want to use the PaletteFactory methods from Apache Commons Imaging (fka "Sanselan"):
The PaletteFactory methods build up collections (int[] and List<>) which can then be iterated over. I'm not sure just what kind of comparison you need to do, but a fairly simple case, using e.g. makeExactRgbPaletteSimple(), would be:
final File img1 = new File("path/to/image_1.ext")
final File img2 = new File("path/to/image_2.ext")
final PaletteFactory pf;
final int MAX_COLORS = 256;
final Palette p1 = pf.makeExactRgbPaletteSimple(img1, MAX_COLORS);
final Palette p2 = pf.makeExactRgbPaletteSimple(img2, MAX_COLORS);
final ArrayList<Int> matches = new ArrayList<Int>(Math.max(p1.length(), p2.length()));
int matchPercent;
// Palette objects are pre-sorted, afaik
if ( (p1 != null) && (p2 != null) ) {
if (p1.length() > p2.length()) {
for (int i = 0; i < p1.length(); i++) {
final int c1 = p1.getEntry(i);
final int c2 = p2.getPaletteIndex(c1);
if (c2 != -1) {
matches.add(c1);
}
}
matchPercent = ( (int)( (float)matches.size()) / ((float)p1.length) * 100 ) )
} else if (p2.length() >= p1.length()) {
for (int i = 0; i < p1.length(); i++) {
final int c1 = p2.getEntry(i);
final int c2 = p1.getPaletteIndex(c1);
if (c2 != -1) {
matches.add(c1);
}
}
matchPercent = ( (int)( (float)matches.size()) / ((float)p2.length) * 100 ) )
}
}
This is just a minimal example which may or may not compile and is almost certainly not what you're looking for in terms of comparison logic.
Basically what it does is check if each member of p1 is also a member of p2, and if so, adds it to matches. Hopefully the logic is correct, no guarantees. matchPercent is the percentage of colors which exist in both Palettes.
This is probably not the comparison method you want. It is just a simple example.
You will definitely need to play around with the 2nd parameter to makeExactRgbPaletteSimple(), int max, as I chose 256 arbitrarily - remember, the method will (annoyingly, imo) return null if max is too small.
I would suggest building from source as the repos have not been updated for quite some time. The project is definitely not mature, but it is fairly small, reasonably fast for medium-sized images, and pure Java.
Hope this helps.

Any algorithm to check every pixels that contain specific color?. in Android :)

Now I have 2 dimension array that collects all color's pixel
and I have 1 dimension array that collects a specific color that every pixel need to check with this array. But how to check this 2 arrays
first array is
array_A = new String[bitmap.getWidth()][bitmap.getHeight()];
Another is final String[] array_B = { "ffcc33","ffcc00",....} so how can I check this 2 arrays :)) Thanks in Advance
Hope that snippets would help you. Also, you can use pixel[] = new int [width*height] to get pixel from image.
for(int w= 0; w < bitmap.getwidth(); w++)
{
for(int h = 0 ; h < bitmap,getheight() ; h++ )
{
int c = bitmap.getPixel(w, h);
Log.i("Pixels", h+"X"+w);
}
}

Categories

Resources