I am trying to image flip and image filter But I have a problem with that When I put a filter on the image then when I flip the image the filter is removed
public void Flipimg(View v1){
img1.setImageBitmap(fliph(scaledBitmap));
}
public Bitmap fliph(Bitmap bms){
Matrix mat = new Matrix();
mat.setScale(-1, 1);
Bitmap bit1=Bitmap.createBitmap( bms, 0, 0, bms.getWidth(), bms.getHeight(), mat, true);
return bit;
public void Filterimg(View v2){
img1.setImageBitmap(applyGreyscaleEffect(scaledBitmap));
}
public Bitmap applyGreyscaleEffect(Bitmap src) {
final double GS_RED = 0.299;
final double GS_GREEN = 0.587;
final double GS_BLUE = 0.114;
Bitmap bmOut = Bitmap.createBitmap(src.getWidth(), src.getHeight(), src.getConfig());
int A, R, G, B;
int pixel;
int width = src.getWidth();
int height = src.getHeight();
for (int x = 0; x < width; ++x) {
for (int y = 0; y < height; ++y) {
pixel = src.getPixel(x, y);
A = Color.alpha(pixel);
R = Color.red(pixel);
G = Color.green(pixel);
B = Color.blue(pixel);
R = G = B = (int) (GS_RED * R + GS_GREEN * G + GS_BLUE * B);
bmOut.setPixel(x, y, Color.argb(A, R, G, B));}
}
return bmOut;
looking images THIS image flip
THIS image apply filter
If you'd like to apply effects sequentially, get current bitmap on the ImageView via BitmapDrawable.
public void Flipimg(View v1)
{
//img1.setImageBitmap(fliph(scaledBitmap));
Bitmap currentBitmap = ((BitmapDrawable) img1.getDrawable()).getBitmap();
img1.setImageBitmap(fliph(currentBitmap));
}
public void Filterimg(View v2)
{
//img1.setImageBitmap(applyGreyscaleEffect(scaledBitmap));
Bitmap currentBitmap = ((BitmapDrawable) img1.getDrawable()).getBitmap();
img1.setImageBitmap(applyGreyscaleEffect(currentBitmap));
}
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;
}
I am here trying to convert my current bitmap in black and white bitmap by writing following method.But it takes very long time to convert because of its for loop of every pixels.Am i wrong here any where? or is there any other way to do this?..Help will be appreciated.Thank you.
My Code:
public Bitmap convert(Bitmap src) {
final double GS_RED = 0.299;
final double GS_GREEN = 0.587;
final double GS_BLUE = 0.114;
int A, R, G, B;
final int width = src.getWidth();
final int height = src.getHeight();
int pixels[] = new int[width * height];
int k = 0;
Bitmap bitmap = Bitmap.createBitmap(width, height, src.getConfig());
src.getPixels(pixels, 0, width, 0, 0, width, height);
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
int pixel = pixels[x + y * width];
A = Color.alpha(pixel);
R = Color.red(pixel);
G = Color.green(pixel);
B = Color.blue(pixel);
R = G = B = (int) (GS_RED * R + GS_GREEN * G + GS_BLUE * B);
pixels[x + y * width] = Color.argb(
A, R, G, B);
}
}
bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
return bitmap;
}
You can easily convert rgb image to grayscale using this method without looping through all pixels
public Bitmap toGrayscale(Bitmap bmp)
{
int originalwidth, originalheight;
originalheight = bmp.getHeight();
originalwidth = bmp.getWidth();
Bitmap bmpGrayscale = Bitmap.createBitmap(originalwidth, originalheight, Bitmap.Config.ARGB_8888);
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(bmp, 0, 0, paint);
return bmpGrayscale;
}
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 to convert photo's and images (bitmaps) to pencil sketch-like pictures(As shown below)? I can't find any good sources on how to do it.
I want to make an android app that can programmatically "convert" JPEG photo's to pencil sketch images.
Can you please help me in this.
First read the image
g = Convert-To-Gray-Scale(s)
i = Invert-Colors(g)
b = Apply-Gaussian-Blur(i)
result = Color-Dodge-Blend-Merge(b,g)
https://github.com/finebyte/android-jhlabs/issues/2
Also see https://github.com/geraintluff/canvas-sketch
`public static Bitmap toGrayscale(Bitmap src) {
final double GS_RED = 0.299;
final double GS_GREEN = 0.587;
final double GS_BLUE = 0.114;
Bitmap bmOut = Bitmap.createBitmap(src.getWidth(), src.getHeight(), src.getConfig());
int A, R, G, B;
int pixel;
int width = src.getWidth();
int height = src.getHeight();
for (int x = 0; x < width; ++x) {
for (int y = 0; y < height; ++y) {
// get one pixel color
pixel = src.getPixel(x, y);
A = Color.alpha(pixel);
R = Color.red(pixel);
G = Color.green(pixel);
B = Color.blue(pixel);
R = G = B = (int) (GS_RED * R + GS_GREEN * G + GS_BLUE * B);
bmOut.setPixel(x, y, Color.argb(A, R, G, B));
}
}
return bmOut;
}
public static Bitmap createInvertedBitmap(Bitmap src) {
Bitmap bmOut = Bitmap.createBitmap(src.getWidth(), src.getHeight(), src.getConfig());
int A, R, G, B;
int pixelColor;
int height = src.getHeight();
int width = src.getWidth();
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
pixelColor = src.getPixel(x, y);
A = Color.alpha(pixelColor);
R = 255 - Color.red(pixelColor);
G = 255 - Color.green(pixelColor);
B = 255 - Color.blue(pixelColor);
bmOut.setPixel(x, y, Color.argb(A, R, G, B));
}
}
return bmOut;
}
public Bitmap ColorDodgeBlend(Bitmap source, Bitmap layer) {
Bitmap base = source.copy(Bitmap.Config.ARGB_8888, true);
Bitmap blend = layer.copy(Bitmap.Config.ARGB_8888, false);
IntBuffer buffBase = IntBuffer.allocate(base.getWidth() * base.getHeight());
base.copyPixelsToBuffer(buffBase);
buffBase.rewind();
IntBuffer buffBlend = IntBuffer.allocate(blend.getWidth() * blend.getHeight());
blend.copyPixelsToBuffer(buffBlend);
buffBlend.rewind();
IntBuffer buffOut = IntBuffer.allocate(base.getWidth() * base.getHeight());
buffOut.rewind();
while (buffOut.position() < buffOut.limit()) {
int filterInt = buffBlend.get();
int srcInt = buffBase.get();
int redValueFilter = Color.red(filterInt);
int greenValueFilter = Color.green(filterInt);
int blueValueFilter = Color.blue(filterInt);
int redValueSrc = Color.red(srcInt);
int greenValueSrc = Color.green(srcInt);
int blueValueSrc = Color.blue(srcInt);
int redValueFinal = colordodge(redValueFilter, redValueSrc);
int greenValueFinal = colordodge(greenValueFilter, greenValueSrc);
int blueValueFinal = colordodge(blueValueFilter, blueValueSrc);
int pixel = Color.argb(255, redValueFinal, greenValueFinal, blueValueFinal);
buffOut.put(pixel);
}
buffOut.rewind();
base.copyPixelsFromBuffer(buffOut);
blend.recycle();
return base;
}
private int colordodge(int in1, int in2) {
float image = (float)in2;
float mask = (float)in1;
return ((int) ((image == 255) ? image:Math.min(255, (((long)mask << 8 ) / (255 - image)))));
}`
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;
}