Here we will do some basic image manipulation using javacv (a wrapper for opencv in java) and the javacpp( a java presset library to perform native operations). though JavaCV is not the official release from opencv.org .. Many Many Thanks to Samuel Audet for his enormous effort to to make it possible for us to use opencv in java. (official site- http://bytedeco.org/)
In this example, we will convert an image into gray image and also into HSV image(Hue, saturation, value) format and display them in windows and later save the converted image. for that we will be using the function like cvCvtColor(), cvSaveImage() function of javacv.
But before that, we must download the jars required for this example and must add it to the build path of the project folder in eclipse.
the jars required are
1) javacv_bin.jar
2)javaCpp_bin.jar
3)opencvjar for java
4) vcredist_x64.exe (C++ redistibutable if it is a 64Bit window) .optional if already there
To download all version of java CV please follow this link
1)http://central.maven.org/maven2/org/bytedeco/javacv/
2)To Download OpenCv please go the official website of opencv
we must take care that the javacv version must match with opencv version else version mismatch will occur and Unsatisfied Link error will be thrown
like for the opencv_2.4.9 the matched version is javacv_0.8 or javacv_0.7.
The code for this example is
Some times the Ctrl+Shift+o may not work to import the required classes so we have to manually copy the import .
import static org.bytedeco.javacpp.opencv_core.IPL_DEPTH_8U;
import static org.bytedeco.javacpp.opencv_core.cvCreateImage;
import static org.bytedeco.javacpp.opencv_core.cvGetSize;
import static org.bytedeco.javacpp.opencv_imgcodecs.cvLoadImage;
import static org.bytedeco.javacpp.opencv_imgcodecs.cvSaveImage;
import static org.bytedeco.javacpp.opencv_imgproc.cvCvtColor;
import static org.bytedeco.javacpp.opencv_core.*;
import org.bytedeco.javacpp.opencv_core.IplImage;
import static org.bytedeco.javacpp.opencv_highgui.*;
import static org.bytedeco.javacpp.opencv_imgproc.CV_BGR2GRAY;
import static org.bytedeco.javacpp.opencv_imgproc.CV_BGR2HSV;
public class BasicImgTransform1 {
public static void main(String[] args) {
IplImage img=cvLoadImage("C:/Users/ASD/Downloads/me.jpg");// ur image file location
IplImage imgGray=cvCreateImage(cvGetSize(img),IPL_DEPTH_8U,1);// 1 represents single channel & U is unsigned
IplImage imgHsv=cvCreateImage(cvGetSize(img),IPL_DEPTH_8U,3);
cvCvtColor(img,imgHsv,CV_BGR2HSV);
cvCvtColor(img,imgGray,CV_BGR2GRAY);
cvShowImage("Original",img);
cvShowImage("GrayImage",imgGray);
cvShowImage("HSVImage",imgHsv);
cvWaitKey();
cvSaveImage("D:\\Original.jpg",img);
cvSaveImage("D:\\GrayImage.jpg",imgGray);
cvSaveImage("D:\\HsvImage.jpg",imgHsv);
// now the below part is optional but it is always recommended to release the resources
cvReleaseImage(img);
cvReleaseImage(imgGray);
cvReleaseImage(imgHsv);
}
}
TO know more on this topic please refer the official Open CV Website.
now try this on your own and please let me know if you find any issue.
Happy Coding.
Thank you
Ashutosh
In this example, we will convert an image into gray image and also into HSV image(Hue, saturation, value) format and display them in windows and later save the converted image. for that we will be using the function like cvCvtColor(), cvSaveImage() function of javacv.
But before that, we must download the jars required for this example and must add it to the build path of the project folder in eclipse.
the jars required are
1) javacv_bin.jar
2)javaCpp_bin.jar
3)opencvjar for java
4) vcredist_x64.exe (C++ redistibutable if it is a 64Bit window) .optional if already there
To download all version of java CV please follow this link
1)http://central.maven.org/maven2/org/bytedeco/javacv/
2)To Download OpenCv please go the official website of opencv
we must take care that the javacv version must match with opencv version else version mismatch will occur and Unsatisfied Link error will be thrown
like for the opencv_2.4.9 the matched version is javacv_0.8 or javacv_0.7.
The code for this example is
Some times the Ctrl+Shift+o may not work to import the required classes so we have to manually copy the import .
import static org.bytedeco.javacpp.opencv_core.IPL_DEPTH_8U;
import static org.bytedeco.javacpp.opencv_core.cvCreateImage;
import static org.bytedeco.javacpp.opencv_core.cvGetSize;
import static org.bytedeco.javacpp.opencv_imgcodecs.cvLoadImage;
import static org.bytedeco.javacpp.opencv_imgcodecs.cvSaveImage;
import static org.bytedeco.javacpp.opencv_imgproc.cvCvtColor;
import static org.bytedeco.javacpp.opencv_core.*;
import org.bytedeco.javacpp.opencv_core.IplImage;
import static org.bytedeco.javacpp.opencv_highgui.*;
import static org.bytedeco.javacpp.opencv_imgproc.CV_BGR2GRAY;
import static org.bytedeco.javacpp.opencv_imgproc.CV_BGR2HSV;
public class BasicImgTransform1 {
public static void main(String[] args) {
IplImage img=cvLoadImage("C:/Users/ASD/Downloads/me.jpg");// ur image file location
IplImage imgGray=cvCreateImage(cvGetSize(img),IPL_DEPTH_8U,1);// 1 represents single channel & U is unsigned
IplImage imgHsv=cvCreateImage(cvGetSize(img),IPL_DEPTH_8U,3);
cvCvtColor(img,imgHsv,CV_BGR2HSV);
cvCvtColor(img,imgGray,CV_BGR2GRAY);
cvShowImage("Original",img);
cvShowImage("GrayImage",imgGray);
cvShowImage("HSVImage",imgHsv);
cvWaitKey();
cvSaveImage("D:\\Original.jpg",img);
cvSaveImage("D:\\GrayImage.jpg",imgGray);
cvSaveImage("D:\\HsvImage.jpg",imgHsv);
// now the below part is optional but it is always recommended to release the resources
cvReleaseImage(img);
cvReleaseImage(imgGray);
cvReleaseImage(imgHsv);
}
}
TO know more on this topic please refer the official Open CV Website.
now try this on your own and please let me know if you find any issue.
Happy Coding.
Thank you
Ashutosh
0 comments:
Post a Comment