Defining grey values in Android - android

I am trying to define specific gray values in an image. In my application, I create empty bitmaps and generate different patterns on it afterwards(e.g. sinusodial 2D patterns with gray values from 0 - 255 or 0 -1).
In my pevious research I could only find this line of code, that was supposed to solve my poblem:
myBitmap.setPixel(x, y, Color.rgb(45, 127, 0));
But this only tells me how to work with colors, not with gray values.
Does anyone have an idea?

I don't know if there is a way to give an Android bitmap a backing store where each pixel is represented by one byte, but you certainly can set a pixel to gray by setting red, green, and blue values to the same:
int gray = 127; // 0-255
myBitmap.setPixel(x, y, Color.rgb(gray, gray, gray));

Related

OpenCV Android Green Color Detection

currently I'm making an app where user will detect green colors. I use this photo for testing:
My problem is that I can not detect any green pixel. Before I worked with blue color and everything worked fine. Now I can't detect anything though I tried different combinations of RGB. I wanted to know whether it's problem with green or my detection range, so I made an image in paint using (0, 255, 0) and it worked. Why it can't see this circle then? I use this code for detection:
Core.inRange(hsv_image, new Scalar([I change this value]), new Scalar(60, 255, 255), ultimate_blue);
It could have been that I set wrong Range, but I use Photoshop to get color of one of green pixels and convert RGB value of it into HSV. Yet it doesn't work. It don't detect even pixel that I've sampled. What's wrong? Thanks in advance.
Using Miki's answer:
Green color is HSV space has H = 120 and it's in range [0, 360].
OpenCV halves the H values to fit the range [0,255], so H value instead of being in range [0, 360], is in range [0, 180].
S and V are still in range [0, 255].
As a consequence, the value of H for green is 60 = 120 / 2.
You upper and lower bound should be:
// sensitivity is a int, typically set to 15 - 20
[60 - sensitivity, 100, 100]
[60 + sensitivity, 255, 255]
UPDATE
Since your image is quite dark, you need to use a lower bound for V. With these values:
sensitivity = 15;
[60 - sensitivity, 100, 50] // lower bound
[60 + sensitivity, 255, 255] // upper bound
the resulting mask would be like:
You can refer to this answer for the details.

What does it do - get transparency and drawing cache?

I'd like to ask you what does it do?
a = Color.alpha(pixel);
Is it geting transparency pixel?
And I'd like to ask what is it Dwaring Cache in Android?
Color.alpha get the value of the alpha channel. Android colors are ARGB, integer 32bits, 8 bits per channel. It takes the value of A and it shift this value to the right of 24. Something like
return pixel >>> 24;
Edit: alpha expresses the opacity of your color/pixel. Its range between 0, fully transparent and 255 fully opaque

How do int color codes in android work?

I have the following problem:
I have a list view, I want to assign a gradient color to item separator ( Divider) of this list view. I am using the following code:
list = (ListView) findViewById(R.id.list);
int[] colors = { 0, 0xffffff00, 0 };
list.setDivider(new GradientDrawable(Orientation.RIGHT_LEFT, colors));
list.setDividerHeight(4);
I looked up the color code (0xffffff00) from: http://developer.android.com/reference/android/graphics/Color.html
PROBLEM:
However this color is Yellow, what I want is golden. I am also interested to know how this works, I mean how can I define the color of my choice, so far I tried to understand from the developer site but it is not much clear.
that you wrote is the hex notation. You can think about a color as composed of 4 components. ARGB. In your example you have 0xffffff00. The first ff is the alpha component, the second ff is the red component, the third ff is green component the fourth 00 is the blue component. Change those hexadecimal values you can get your colors.
Use
int color = Color.argb(255, 255, 175, 64);
or use an iteger to hex converter
For gold you need a yellow that's more red than green, so try 0xffffc000. In decimal that would be red 255 green 192 blue 0. To really get a hold of how the RGB system works spend a while playing with the values, I don't think it's possible to get a deep understanding just by reading about it.

how to use the LightingColorFilter to make the image form dark to light

I want to add light of the image, I want to use LightingColorFilter
LightingColorFilter lcf = new LightingColorFilter( mul, add);
imageView.setColorFilter(lcf);
but I don't know how to adjust mul, add, can you give some link or code or parameters to adjust the light of the image?
Thank you
The integer values are colours (you may want to have a closer look here http://developer.android.com/reference/android/graphics/Color.html)
4 Bytes are used, one for alpha, one for red, one for green, one for blue range - every single from 0 to 255 (hex 0 to FF)
so the colour in hex looks like
0 x 00 00 00 00
alpha red green blue
If you want to set for example red to zero, use
mul: 0xFF00FFFF
add: 0x00000000
If you want to force blue to be full-on, use
mul: 0xFFFFFFFF
add: 0x000000FF
Canvas canvas= new Canvas(my_bitmap); // create canvas from bitmap
Paint my_paint = new Paint(); // create Paint from bitmap
my_paint.setColorFilter(new LightingColorFilter(0x77777777, 0x77777777));//(mul, add)
canvas.drawBitmap(my_bitmap, 0, 0, my_paint );
1- now you can show my_bitmap
2- now my_bitmap have about 50% more light
3- you can change (mul, add) to understand how work
4- also can use my_paint in other graphic method

How can I convert a short[] into a int[] to create a grayscale bitmap?

I am trying to run a denoising algorithm on a bitmap image that I have -- the function returns me a short[], so I tried simply casting it to int[] in order to generate a bitmap and I get this:
I'd like it to be in grayscale, not .. well.. pink. Any ideas?
Instead of replicating the 8-bit intensity in each of the RGB channels, you can use the intensity as the alpha channel. In this scheme, 0 corresponds to transparent (background color) and 255 corresponds to fully opaque (black, or whatever color you want--even pink). The idea is similar to Jason LeBrun's proposal: take the high-order 8 bits of each value, shift 24 bits left, then bitwise-OR with the color you want to use for full intensity (or with nothing, if you want black to represent full intensity).
The pixels of a bitmap are encoded using either ARGB_8888, RGB_565, ARGB_4444, or ALPHA_8. So, the short values that you're returning must happen to correspond to values that look slightly pink-ish in one of those formats.
If you want a grayscale bitmap, you can only have values in the range of 0-256 (For the maximum precious color component of 8 bits if you're using ARGB_8888). So, you'll need to map your short to values within that range, and then replicate that value for each of the RGB components.

Categories

Resources