i hava done with get grayscale value, but i don't know how to use function to convert the grayscale to be binary image. Please help me, here my function code:
public Bitmap toBinary(Bitmap bmpOriginal) {
int width, height, threshold;
height = bmpOriginal.getHeight();
width = bmpOriginal.getWidth();
threshold = 127;
final Bitmap bmpBinary = null;
for(int x = 0; x < width; ++x) {
for(int y = 0; y < height; ++y) {
// get one pixel color
int pixel = bmpOriginal.getPixel(x, y);
//get grayscale value
int gray = (int)(pixel & 0xFF);
//get binary value
if(gray < threshold){
bmpBinary.setPixel(x, y, 0);
} else{
bmpBinary.setPixel(x, y, 255);
}
}
}
return bmpBinary;
}
here my full code:
public class MainActivity extends Activity {
ImageView img;
Button btn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//convert imageview to bitmap
img =(ImageView) findViewById(R.id.imageView1);
BitmapDrawable drawable = (BitmapDrawable) img.getDrawable();
final Bitmap imgbitmap = drawable.getBitmap();
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//convert bitmap to grayscale
Bitmap imgnew;
imgnew = toGrayscale(imgbitmap);
//convert to binary
imgnew = toBinary(imgnew);
//convert bitmap to imageview
ImageView imgbit;
imgbit = (ImageView) findViewById(R.id.imageView2);
imgbit.setImageBitmap(imgnew);
}
});
}
public Bitmap toGrayscale(Bitmap bmpOriginal){
int width, height;
height = bmpOriginal.getHeight();
width = bmpOriginal.getWidth();
Bitmap bmpGrayscale = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
Canvas c = new Canvas(bmpGrayscale);
Paint paint = new Paint();
ColorMatrix cm = new ColorMatrix();
cm.setSaturation(0);
ColorMatrixColorFilter f = new ColorMatrixColorFilter(cm);
paint.setColorFilter(f);
c.drawBitmap(bmpOriginal, 0, 0, paint);
return bmpGrayscale;
}
public Bitmap toBinary(Bitmap bmpOriginal) {
int width, height, threshold;
height = bmpOriginal.getHeight();
width = bmpOriginal.getWidth();
threshold = 127;
final Bitmap bmpBinary = null;
for(int x = 0; x < width; ++x) {
for(int y = 0; y < height; ++y) {
// get one pixel color
int pixel = bmpOriginal.getPixel(x, y);
//get grayscale value
int gray = (int)(pixel & 0xFF);
//get binary value
if(gray < threshold){
bmpBinary.setPixel(x, y, 0);
} else{
bmpBinary.setPixel(x, y, 255);
}
}
}
return bmpBinary;
}
}
First, you get a NullReferenceException because bmpBinary is NULL.
Second, to get one Color chanel you can use int red = Color.red(pixel);
Third, to set a pixel white use bmpBinary.setPixel(x, y, 0xFFFFFFFF);
I modified your code a bit:
public Bitmap toBinary(Bitmap bmpOriginal) {
int width, height, threshold;
height = bmpOriginal.getHeight();
width = bmpOriginal.getWidth();
threshold = 127;
Bitmap bmpBinary = Bitmap.createBitmap(bmpOriginal);
for(int x = 0; x < width; ++x) {
for(int y = 0; y < height; ++y) {
// get one pixel color
int pixel = bmpOriginal.getPixel(x, y);
int red = Color.red(pixel);
//get binary value
if(red < threshold){
bmpBinary.setPixel(x, y, 0xFF000000);
} else{
bmpBinary.setPixel(x, y, 0xFFFFFFFF);
}
}
}
return bmpBinary;
}
An even better way is not to use just the value of one color chanel but a weighted average of red green and blue for example:
int gray = (int)(red * 0.3 + green * 0.59 + blue * 0.11);
Related
when i load image from gallery to the imageview, it can't recognize the height and width of the image, but i load the image from drawable , it recognize the height and width of the image. please tell where i made a mistake?
here i attached the code below,
Blockquote
source = BitmapFactory.decodeResource(getResources(),R.id.image);
mRed = findViewById(R.id.red);
mRed.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
bitmap = applyColorFilterEffect(source, 255, 0, 0);
imageView.setImageBitmap(bitmap);
}
});
Blockquote
public Bitmap applyColorFilterEffect(Bitmap src, double red, double green, double blue) {
// image size
int width = src.getWidth();
int height = src.getHeight();
// create output bitmap
Bitmap bmOut = Bitmap.createBitmap(width, height, src.getConfig());
// color information
int A, R, G, B;
int pixel;
// scan through all pixels
for (int x = 0; x < width; ++x) {
for (int y = 0; y < height; ++y) {
//get pixel color
pixel = src.getPixel(x, y);
// apply filtering on each channel R, G, B
A = Color.alpha(pixel);
R = (int) (Color.red(pixel) * red);
G = (int) (Color.green(pixel) * green);
B = (int) (Color.blue(pixel) * blue);
// set new color pixel to output bitmap
bmOut.setPixel(x, y, Color.argb(A, R, G, B));
}
}
return bmOut;
}
Am creating an app, in which it detects circles/dots (might be irregular) in an image.To detect it I converted the RGB image to Binary. Using threshold values, I got the sample Binary image given below.
In the image, i want to eliminate the two bigger shapes and i want to detect the small dots/circles, without using any libraries like OpenCV4android. Please help me to solve this.
Here is the program to convert the image to binary:
public class MainActivity extends Activity {
ImageView img;
Button btn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn = (Button)findViewById(R.id.button);
//convert imageview to bitmap
img =(ImageView) findViewById(R.id.imageView);
BitmapDrawable drawable = (BitmapDrawable) img.getDrawable();
final Bitmap imgbitmap = drawable.getBitmap();
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//convert bitmap to grayscale
Bitmap imgnew;
imgnew = toGrayscale(imgbitmap);
//convert to binary
imgnew = toBinary(imgnew);
//convert bitmap to imageview
ImageView imgbit;
imgbit = (ImageView) findViewById(R.id.imageView2);
imgbit.setImageBitmap(imgnew);
}
});
}
public Bitmap toGrayscale(Bitmap bmpOriginal){
int width, height;
height = bmpOriginal.getHeight();
width = bmpOriginal.getWidth();
Bitmap bmpGrayscale = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
Canvas c = new Canvas(bmpGrayscale);
Paint paint = new Paint();
ColorMatrix cm = new ColorMatrix();
cm.setSaturation(0);
ColorMatrixColorFilter f = new ColorMatrixColorFilter(cm);
paint.setColorFilter(f);
c.drawBitmap(bmpOriginal, 0, 0, paint);
return bmpGrayscale;
}
public Bitmap toBinary(Bitmap bmpOriginal) {
int width, height, threshold;
height = bmpOriginal.getHeight();
width = bmpOriginal.getWidth();
threshold = 65;
Bitmap bmpBinary = Bitmap.createBitmap(bmpOriginal);
for(int x = 0; x < width; ++x) {
for(int y = 0; y < height; ++y) {
// get one pixel color
int pixel = bmpOriginal.getPixel(x, y);
int red = Color.red(pixel);
//get binary value
if(red < threshold){
bmpBinary.setPixel(x, y,0xFFFFFFFF );
} else{
bmpBinary.setPixel(x, y,0xFF000000 );
}
}
}
return bmpBinary;
}
}
Anyone know how to add subtle noise to grayscale image?
I have a grayscale image that I want to add some subtle black and white noise to. Anyone know how to do this?
I'm currently using this method, but this is just generating random colors and swapping existing pixels with those colors. How can I add some subtle black and white noise to only SOME pixels on a bitmap?
public static Bitmap applyFleaEffect(Bitmap source) {
// get source image size
int width = source.getWidth();
int height = source.getHeight();
int[] pixels = new int[width * height];
// get pixel array from source
source.getPixels(pixels, 0, width, 0, 0, width, height);
// create a random object
Random random = new Random();
int index = 0;
// iteration through pixels
for(int y = 0; y < height; ++y) {
for(int x = 0; x < width; ++x) {
// get current index in 2D-matrix
index = y * width + x;
// get random color
int randColor = Color.rgb(random.nextInt(255),
random.nextInt(255), random.nextInt(255));
// OR
pixels[index] |= randColor;
}
}
// output bitmap
Bitmap bmOut = Bitmap.createBitmap(width, height, source.getConfig());
bmOut.setPixels(pixels, 0, width, 0, 0, width, height);
return bmOut;
}
}
UPDATE: Adding complete code to show grayscale step, and attempted noise step
//First, apply greyscale to make Image black and white
Bitmap bmpGrayscale = Bitmap.createBitmap(width, height, mBitmap.getConfig());
Canvas c = new Canvas(bmpGrayscale);
Paint paint = new Paint();
ColorMatrix cm = new ColorMatrix();
cm.setSaturation(0);
ColorMatrixColorFilter f = new ColorMatrixColorFilter(cm);
paint.setColorFilter(f);
//Apply noise after grayscale
int[] pixels = new int[width * height];
bmpGrayscale.getPixels(pixels, 0, width, 0, 0, width, height);
// a random object
Random random = new Random();
int index = 0;
// Note: Declare the c and randColor variables outside of the for loops
int co = 0;
int randColor = 0;
// iteration through pixels
for (int y = 0; y < height; ++y) {
for (int x = 0; x < width; ++x) {
if (random.nextInt(101) < percentNoise) {
// Skip this iteration a certain percentage of the time
continue;
}
// get current index in 2D-matrix
index = y * width + x;
co = random.nextInt(255);
randColor = Color.rgb(co, co, co);
pixels[index] |= randColor;
}
}
// output bitmap
mBitmap = Bitmap.createBitmap(width, height, bmpGrayscale.getConfig());
mBitmap.setPixels(pixels, 0, width, 0, 0, width, height);
c.drawBitmap(mBitmap, 0, 0, paint);
return bmpGrayscale;
}
First, randColor is not likely to be a gray-scale color with your code. To generate a random, gray-scale color:
int c = random.nextInt(255);
int randColor = Color.rgb(c, c, c);
With that in mind, here is how I would re-write your code (note: adjust the percentNoise parameter to your liking):
public static Bitmap applyFleaEffect(Bitmap source, int percentNoise) {
// get source image size
int width = source.getWidth();
int height = source.getHeight();
int[] pixels = new int[width * height];
// get pixel array from source
source.getPixels(pixels, 0, width, 0, 0, width, height);
// create a random object
Random random = new Random();
int index = 0;
// Note: Declare the c and randColor variables outside of the for loops
int c = 0;
int randColor = 0;
// iterate through pixels
for (int y = 0; y < height; ++y) {
for (int x = 0; x < width; ++x) {
if (random.nextInt(101) < percentNoise) {
// Skip this iteration a certain percentage of the time
continue;
}
// get current index in 2D-matrix
index = y * width + x;
// get random color
c = random.nextInt(255);
randColor = Color.rgb(c, c, c);
pixels[index] |= randColor;
}
}
Bitmap bmOut = Bitmap.createBitmap(width, height, source.getConfig());
bmOut.setPixels(pixels, 0, width, 0, 0, width, height);
return bmOut;
}
I created an example image with brightness adjusted in Android. I used Bitmap to adjust the brightness but it takes a very long time to run. Instead, I want to set the image brightness in Android using OpenCV.
This is my example code, but it only changes the colour of the image:
Bitmap bmp = BitmapFactory.decodeResource(getResources(),
R.drawable.a001);
int width = bmp.getWidth();
int height = bmp.getHeight();
Mat mRgba = new Mat(width, height, CvType.CV_8UC1);
Utils.bitmapToMat(bmp, mRgba);
Mat mRay = new Mat();
Imgproc.cvtColor(mRgba, mRay, Imgproc.COLOR_BGRA2RGB, 4);
Utils.matToBitmap(mRay, bmp);
mImageview_01.setImageBitmap(bmp);
[Update]
I try add code, but it error
Bitmap bmp = BitmapFactory.decodeResource(getResources(),
R.drawable.a001);
int width = bmp.getWidth();
int height = bmp.getHeight();
Mat mRgba = new Mat(width, height, CvType.CV_8UC1);
Utils.bitmapToMat(bmp, mRgba);
Mat mRay = new Mat();
Imgproc.cvtColor(mRgba, mRay, Imgproc.COLOR_BGRA2RGB, 4);
/*
* Use Adaptive Thresholding on the grayscaled Mats crop -> threshed Mat
* src, Mat dst, double maxValue, int adaptiveMethod, int thresholdType,
* int blockSize, double C
*/
Imgproc.adaptiveThreshold(threshed, threshed, 255,
Imgproc.ADAPTIVE_THRESH_GAUSSIAN_C, Imgproc.THRESH_BINARY_INV, 15, 8);
Utils.matToBitmap(mRay, bmp);
mImageview_01.setImageBitmap(bmp);
[Error]
CvException [org.opencv.core.CvException: /home/reports/ci/slave_desktop/50-SDK/opencv/modules/imgproc/src/thresh.cpp:796: error: (-215) src.type() == CV_8UC1 in function void cv::adaptiveThreshold(cv::InputArray, cv::OutputArray, double, int, int, int, double)
Pls view examples of what I'm trying to do here.
There is many fast and easy ways to adjust a bitmap brightness, such as ColorMatrix or RenderScript. But if you still considered to use OpenCV that's the way :
private Bitmap changeBrightness(Bitmap bmp, int value){
Mat src = new Mat(bmap.getHeight(),bmap.getWidth(), CvType.CV_8UC1);
Utils.bitmapToMat(bmap,src);
src.convertTo(src,-1,1,value);
Bitmap result = Bitmap.createBitmap(src.cols(),src.rows(), Bitmap.Config.ARGB_8888);
Utils.matToBitmap(src,result);
return result;
}
check the example given at Increase/Decrease Brightness of Image
pls see this
int brightness;
SeekBar seekBarBrightness=(SeekBar)findViewById(R.id.seekBar1);
seekBarBrightness.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
#Override
public void onStopTrackingTouch(SeekBar arg0) {
}
#Override
public void onStartTrackingTouch(SeekBar arg0) {
}
#Override
public void onProgressChanged(SeekBar arg0, int progress, boolean arg2) {
Bitmap newBitMap = doBrightness(bitMap,progress);
imageView.setImageBitmap(newBitMap);
}
});
public static Bitmap doBrightness(Bitmap src, int value)
{
// image size
int width = src.getWidth();
int height = src.getHeight();
// create output bitmap
Bitmap bmOut = Bitmap.createBitmap(width, height, src.getConfig());
// color information
int A, R, G, B;
int pixel;
// scan through all pixels
for (int x = 0; x < width; ++x)
{
for (int y = 0; y < height; ++y)
{
// get pixel color
pixel = src.getPixel(x, y);
A = Color.alpha(pixel);
R = Color.red(pixel);
G = Color.green(pixel);
B = Color.blue(pixel);
// increase/decrease each channel
R += value;
if (R > 255)
{
R = 255;
} else if (R < 0)
{
R = 0;
}
G += value;
if (G > 255)
{
G = 255;
} else if (G < 0)
{
G = 0;
}
B += value;
if (B > 255)
{
B = 255;
} else if (B < 0)
{
B = 0;
}
// apply new pixel color to output bitmap
bmOut.setPixel(x, y, Color.argb(A, R, G, B));
}
}
// return final image
return bmOut;
}
How can i to get color/alpha of pixel from Sprite in andengine without creating any additional Bitmap?
I solved this problem by using this function
private void loadObjectsMask()
{
InputStream in = null;
Bitmap maskForObjects=null;
try {
final BitmapFactory.Options decodeOptions = new BitmapFactory.Options();
decodeOptions.inPreferredConfig = Bitmap.Config.ARGB_4444;
in = GameActivity.this.getAssets().open("images/" + BACKGROUND_PATH + getObjectImgFile());
maskForObjects = BitmapFactory.decodeStream(in, null, decodeOptions);
} catch (final IOException e) {
} finally {
StreamUtils.close(in);
}
if (maskForObjects != null)
{
objectsMask=new byte[maskForObjects.getWidth()][maskForObjects.getHeight()];
for(int pw=0;pw<maskForObjects.getWidth();++pw)
{
for(int ph=0;ph<maskForObjects.getHeight();++ph)
{
objectsMask[pw][ph]=(byte)(Color.alpha(maskForObjects.getPixel(pw, ph))==0?0:1);
}
}
maskForObjects.recycle();
System.out.printf("Bitmap size %d %d\n", maskForObjects.getWidth(),maskForObjects.getHeight());
}
else System.out.printf("Bitmap size error\n");
}
have a look this method, this method are used to effect on each pix
public Bitmap invert(Bitmap src) {
// image size
int width = src.getWidth();
int height = src.getHeight();
// create output bitmap
Bitmap bmOut = Bitmap.createBitmap(width, height, src.getConfig());
// color information
int A, R, G, B;
int pixel;
// scan through all pixels
for(int x = 0; x < width; ++x) {
for(int y = 0; y < height; ++y) {
// get pixel color
pixel = src.getPixel(x, y);
// get color on each channel
A = Color.alpha(pixel);
R = Color.red(pixel);
G = Color.green(pixel);
B = Color.blue(pixel);
// set new pixel color to output image
bmOut.setPixel(x, y, Color.argb(A, 255-R, 255-G, 255-B));
}
}
// return final image
return bmOut;
}
To convert Gray Scale
public static Bitmap toGrayscale(Bitmap bmpOriginal)
{
int width, height;
height = bmpOriginal.getHeight();
width = bmpOriginal.getWidth();
Bitmap bmpGrayscale = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
Canvas c = new Canvas(bmpGrayscale);
Paint paint = new Paint();
ColorMatrix cm = new ColorMatrix();
cm.setSaturation(0);
ColorMatrixColorFilter f = new ColorMatrixColorFilter(cm);
paint.setColorFilter(f);
c.drawBitmap(bmpOriginal, 0, 0, paint);
return bmpGrayscale;
}