How To Read & Display An Image File Using OpenCV
To Read & Display An Image File Using OpenCV We Can Use Mat Object.
In OpenCV, a Mat (Matrix) object is the primary data structure used to store and manipulate image data. It represents an n-dimensional array, typically used for 2D images, where each element contains pixel data (e.g., RGB or grayscale values).
To Load & Display An Image Using OpenCV Follow The Following Steps.
Step1. In The View Area Add ImageView Or Any Other Image Displaying View.
Step2. Make Sure You Select OpenCV Android SDK In The Library Manager.
Step3. In The Custom Imports Add The Following Classes From OpenCV.
Step4. Load OpenCV C++ Binary Using The Following Code.
Step5. In Any Event You Want(OnCreate, OnButton Click Or Any Other Event) Where You Want To Load & Display The Image Use This Code. Read Comment's For Better Understanding.
To Read & Display An Image File Using OpenCV We Can Use Mat Object.
In OpenCV, a Mat (Matrix) object is the primary data structure used to store and manipulate image data. It represents an n-dimensional array, typically used for 2D images, where each element contains pixel data (e.g., RGB or grayscale values).
To Load & Display An Image Using OpenCV Follow The Following Steps.
Step1. In The View Area Add ImageView Or Any Other Image Displaying View.
Step2. Make Sure You Select OpenCV Android SDK In The Library Manager.
Step3. In The Custom Imports Add The Following Classes From OpenCV.
import org.opencv.core.Mat;
import org.opencv.android.Utils;
import org.opencv.imgproc.Imgproc;
import org.opencv.imgcodecs.Imgcodecs;
Step4. Load OpenCV C++ Binary Using The Following Code.
}
static {
System.loadLibrary("opencv_java4");
}
{
Step5. In Any Event You Want(OnCreate, OnButton Click Or Any Other Event) Where You Want To Load & Display The Image Use This Code. Read Comment's For Better Understanding.
//Set The Image File Path
String ImageFilePath = "/sdcard/image_file.png";
//Read The Image File From The Specified Path In ImageFilePath String Variable & Store It In OpenCV Mat(Matrix) Object.
Mat mat = Imgcodecs.imread(ImageFilePath);
//By Default OpenCV Uses BGR(Blue, Green, Red) Color Format For The Image & As You Know Android Uses RGB(Red, Green, Blue) Color Format. So It Causes The Image To Have Unusual Color So We Need To Convert Them From BGR(Blue, Green, Red) Color Format To RGB(Red, Green, Blue) Color Format Using This Method.
Imgproc.cvtColor(mat, mat, Imgproc.COLOR_BGR2RGB);
//Create Bitmap From The Mat Object Columns & Rows.
Bitmap bitmap = Bitmap.createBitmap(mat.cols(), mat.rows(), Bitmap.Config.ARGB_8888);
//Convert The Image From OpenCV Matrix Object To Android Bitmap.
Utils.matToBitmap(mat, bitmap);
//Display The Bitmap In ImageView Called imageview1
imageview1.setImageBitmap(bitmap);
}
👍1😁1
How To Convert Image To Grayscale Using OpenCV
To Convert Image To Grayscale Using OpenCV Follow The Following Steps.
Step1. In The View Area Add ImageView Or Any Other Image Displaying View.
Step2. Make Sure You Select OpenCV Android SDK In The Library Manager.
Step3. In The Custom Imports Add The Following Classes From OpenCV.
Step4. Load OpenCV C++ Binary Using The Following Code.
Step5. In Any Event You Want(OnCreate, OnButton Click Or Any Other Event) Where You Want To Convert The Image To Grayscale Use This Code. Read Comment's For Better Understanding.
To Convert Image To Grayscale Using OpenCV Follow The Following Steps.
Step1. In The View Area Add ImageView Or Any Other Image Displaying View.
Step2. Make Sure You Select OpenCV Android SDK In The Library Manager.
Step3. In The Custom Imports Add The Following Classes From OpenCV.
import org.opencv.core.Mat;
import org.opencv.core.CvType;
import org.opencv.android.Utils;
import android.graphics.Bitmap;
import org.opencv.imgproc.Imgproc;
import org.opencv.imgcodecs.Imgcodecs;
import android.graphics.drawable.BitmapDrawable;
Step4. Load OpenCV C++ Binary Using The Following Code.
}
static {
System.loadLibrary("opencv_java4");
}
{
Step5. In Any Event You Want(OnCreate, OnButton Click Or Any Other Event) Where You Want To Convert The Image To Grayscale Use This Code. Read Comment's For Better Understanding.
//Get Bitmap From ImageView Called imageview1
Bitmap bitmap = ((BitmapDrawable) imageview1.getDrawable()).getBitmap();
//Initialize Mat Object For The Bitmap
Mat mat = new Mat();
//Convert The Bitmap From imageview1 To OpenCV Mat Object.
Utils.bitmapToMat(bitmap, mat);
//Initialize New Mat Object For The Grayscale Image.
Mat graymat = new Mat();
//Convert The Image Color From imageview1 RGB(Red, Green, Blue) Format To Greyscale Format.
Imgproc.cvtColor(mat, graymat, Imgproc.COLOR_RGB2GRAY);
//Convert The Grayscale Mat Object To Android Bitmap
Bitmap graybitmap = Bitmap.createBitmap(graymat.cols(), graymat.rows(), Bitmap.Config.ARGB_8888);
//Convert The Gray Mat Object To Bitmap
Utils.matToBitmap(graymat, graybitmap);
//Display The New Grayscale Image In imageview1
imageview1.setImageBitmap(graybitmap);
//Release The Mat Objects To Prevent Memory Leaks
mat.release();
graymat.release();
Android Developers pinned «OpenCV Image2Grayscale Converter Sketchware Pro Project»
What Did You Feel About The OpenCV Tutorial
Final Results
72%
I Really Like It I Want To Continue Learning
17%
I Don't Want To Continue Learning Please Stop The Tutorial
10%
In Comment