This shows you the differences between two versions of the page.
javacv_opencv_7-1_hs_histogram [2010/10/18 12:55] oestape |
javacv_opencv_7-1_hs_histogram [2011/04/26 13:55] (current) |
||
---|---|---|---|
Line 1: | Line 1: | ||
- | =====javacv opencv 7.1 hs histogram===== | + | =====javacv opencv 7.1 hs histogram===== |
- | This is the example 7.1 of the book [[http://oreilly.com/catalog/9780596516130|"Learning OpenCV"]] translated from C++ to [[http://code.google.com/p/javacv/|JavaCV]]. You can download the original C++ version and the other examples of the book from http://examples.oreilly.com/9780596516130/ | + | This is the example 7.1 of the book [[http://oreilly.com/catalog/9780596516130|"Learning OpenCV"]] translated from C++ to [[http://code.google.com/p/javacv/|JavaCV]]. You can download the original C++ version and the other examples of the book from http://examples.oreilly.com/9780596516130/ |
- | + | ||
- | This example shows how to load an image, convert it to the HSV color space and create a 2D histogram of the plane H-S. | + | This example shows how to load an image, convert it to the HSV color space and create a 2D histogram of the plane H-S. |
- | + | ||
- | I wrote this example in order to show a bug that existed in JavaCV. The version 20100730 still has the bug. See the last 2 posts in: | + | I wrote this example in order to show a bug that existed in JavaCV. The version 20100730 still has the bug. See the last 2 posts in: |
- | http://groups.google.com/group/javacv/browse_thread/thread/60d9314f8f97b8a9 | + | http://groups.google.com/group/javacv/browse_thread/thread/60d9314f8f97b8a9 |
- | + | ||
- | You have to download the javacv source and modify the cv.java as explained in the link above. Basically you have to change the "FloatByReference[] ranges" by "Pointer ranges". | + | You have to download the javacv source and modify the cv.java as explained in the link above. Basically you have to change the "FloatByReference[] ranges" by "Pointer ranges". |
- | + | ||
- | <code java Example7_1_HS_Histogram.java> | + | <code java Example7_1_HS_Histogram.java> |
- | package examples; | + | package examples; |
- | + | ||
- | + | ||
- | import com.sun.jna.Memory; | + | import com.sun.jna.Memory; |
- | import com.sun.jna.Pointer; | + | import com.sun.jna.Pointer; |
- | import static name.audet.samuel.javacv.jna.cxcore.*; | + | import static name.audet.samuel.javacv.jna.cxcore.*; |
- | import static name.audet.samuel.javacv.jna.cv.*; | + | import static name.audet.samuel.javacv.jna.cv.*; |
- | import static name.audet.samuel.javacv.jna.highgui.*; | + | import static name.audet.samuel.javacv.jna.highgui.*; |
- | import name.audet.samuel.javacv.*; | + | import name.audet.samuel.javacv.*; |
- | import com.sun.jna.ptr.FloatByReference; | + | import com.sun.jna.ptr.FloatByReference; |
- | + | ||
- | /** | + | /** |
- | * Adapted from the book "Learning OpenCV" by Gary Bradski and Adrian Kaehler, | + | * Adapted from the book "Learning OpenCV" by Gary Bradski and Adrian Kaehler, |
- | * 2008, p.203 (Example 7-1. Histogram computation and display) | + | * 2008, p.203 (Example 7-1. Histogram computation and display) |
- | * | + | * |
- | * @author Octavi Estapé | + | * @author Octavi Estapé |
- | */ | + | */ |
- | public class Example7_1_HS_Histogram { | + | public class Example7_1_HS_Histogram { |
- | public static void main(String[] args) { | + | public static void main(String[] args) { |
- | // Make sure to call JavaCvErrorCallback.redirectError() to prevent your | + | // Make sure to call JavaCvErrorCallback.redirectError() to prevent your |
- | // application from simply crashing with no warning on some error of OpenCV. | + | // application from simply crashing with no warning on some error of OpenCV. |
- | // JavaCvErrorCallback may be subclassed for finer control of exceptions. | + | // JavaCvErrorCallback may be subclassed for finer control of exceptions. |
- | new JavaCvErrorCallback().redirectError(); | + | new JavaCvErrorCallback().redirectError(); |
- | + | ||
- | IplImage src = cvLoadImage("./data/HandOutdoorColor.jpg", 1); | + | IplImage src = cvLoadImage("./data/HandOutdoorColor.jpg", 1); |
- | + | ||
- | if (src == null) { | + | if (src == null) { |
- | System.err.println("Could not load image file."); | + | System.err.println("Could not load image file."); |
- | } else { | + | } else { |
- | IplImage hsv = cvCreateImage(cvGetSize(src), 8, 3); | + | IplImage hsv = cvCreateImage(cvGetSize(src), 8, 3); |
- | cvCvtColor(src, hsv, CV_BGR2HSV); | + | cvCvtColor(src, hsv, CV_BGR2HSV); |
- | + | ||
- | IplImage h_plane = cvCreateImage(cvGetSize(src), 8, 1); | + | IplImage h_plane = cvCreateImage(cvGetSize(src), 8, 1); |
- | IplImage s_plane = cvCreateImage(cvGetSize(src), 8, 1); | + | IplImage s_plane = cvCreateImage(cvGetSize(src), 8, 1); |
- | IplImage v_plane = cvCreateImage(cvGetSize(src), 8, 1); | + | IplImage v_plane = cvCreateImage(cvGetSize(src), 8, 1); |
- | + | ||
- | cvSplit(hsv, h_plane, s_plane, v_plane, null); //cvCvtPixToPlane | + | cvSplit(hsv, h_plane, s_plane, v_plane, null); //cvCvtPixToPlane |
- | + | ||
- | int h_bins = 30; | + | int h_bins = 30; |
- | int s_bins = 32; | + | int s_bins = 32; |
- | CvHistogram hist; | + | CvHistogram hist; |
- | { | + | { |
- | int hist_size[] = {h_bins, s_bins}; | + | int hist_size[] = {h_bins, s_bins}; |
- | float h_ranges[] = {0, 180}; | + | float h_ranges[] = {0, 180}; |
- | float s_ranges[] = {0, 255}; | + | float s_ranges[] = {0, 255}; |
- | + | ||
- | float[][] ranges = {h_ranges, s_ranges}; | + | float[][] ranges = {h_ranges, s_ranges}; |
- | + | ||
- | + | ||
- | Pointer rangesP = new Memory(Pointer.SIZE*ranges.length); | + | Pointer rangesP = new Memory(Pointer.SIZE*ranges.length); |
- | for(int i=0; i<ranges.length; i++) { | + | for(int i=0; i<ranges.length; i++) { |
- | Pointer p = new Memory(ranges[i].length*Float.SIZE/8); | + | Pointer p = new Memory(ranges[i].length*Float.SIZE/8); |
- | p.write(0, ranges[i], 0, ranges[i].length); | + | p.write(0, ranges[i], 0, ranges[i].length); |
- | rangesP.setPointer(i*Pointer.SIZE, p); | + | rangesP.setPointer(i*Pointer.SIZE, p); |
- | } | + | } |
- | + | ||
- | hist = cvCreateHist(hist_size.length, hist_size, CV_HIST_ARRAY, rangesP, 1); | + | hist = cvCreateHist(hist_size.length, hist_size, CV_HIST_ARRAY, rangesP, 1); |
- | } | + | } |
- | + | ||
- | //IplImage mask = cvCreateImage(cvGetSize(src), 8, 1); | + | //IplImage mask = cvCreateImage(cvGetSize(src), 8, 1); |
- | //cvSet(mask, cvScalar(255, 255, 255, 255)); | + | //cvSet(mask, cvScalar(255, 255, 255, 255)); |
- | + | ||
- | IplImage planes[] = {h_plane, s_plane}; | + | IplImage planes[] = {h_plane, s_plane}; |
- | IplImage.PointerByReference planesPointer = new IplImage.PointerByReference(planes); | + | IplImage.PointerByReference planesPointer = new IplImage.PointerByReference(planes); |
- | cvCalcHist(planesPointer, hist, 0, null); | + | cvCalcHist(planesPointer, hist, 0, null); |
- | cvNormalizeHist(hist, 1); | + | cvNormalizeHist(hist, 1); |
- | + | ||
- | int scale = 10; | + | int scale = 10; |
- | IplImage hist_img = cvCreateImage(cvSize(h_bins*scale, s_bins*scale), 8, 3); | + | IplImage hist_img = cvCreateImage(cvSize(h_bins*scale, s_bins*scale), 8, 3); |
- | cvZero(hist_img); | + | cvZero(hist_img); |
- | + | ||
- | FloatByReference max_value = new FloatByReference(0); | + | FloatByReference max_value = new FloatByReference(0); |
- | cvGetMinMaxHistValue(hist, null, max_value, null, null); | + | cvGetMinMaxHistValue(hist, null, max_value, null, null); |
- | + | ||
- | for(int h=0; h<h_bins; h++) { | + | for(int h=0; h<h_bins; h++) { |
- | for(int s=0; s<s_bins; s++) { | + | for(int s=0; s<s_bins; s++) { |
- | float bin_val = cvQueryHistValue_2D(hist, h, s); | + | float bin_val = cvQueryHistValue_2D(hist, h, s); |
- | int intensity = (int)Math.round(bin_val*255/max_value.getValue()); | + | int intensity = (int)Math.round(bin_val*255/max_value.getValue()); |
- | cvRectangle(hist_img, | + | cvRectangle(hist_img, |
- | cvPoint(h*scale, s*scale), | + | cvPoint(h*scale, s*scale), |
- | cvPoint((h+1)*scale-1, (s+1)*scale-1), | + | cvPoint((h+1)*scale-1, (s+1)*scale-1), |
- | CV_RGB(intensity, intensity, intensity), | + | CV_RGB(intensity, intensity, intensity), |
- | CV_FILLED, 8, 0 | + | CV_FILLED, 8, 0 |
- | ); | + | ); |
- | } | + | } |
- | } | + | } |
- | + | ||
- | + | ||
- | CanvasFrame frame = new CanvasFrame("Source"); | + | CanvasFrame frame = new CanvasFrame("Source"); |
- | frame.showImage(src); | + | frame.showImage(src); |
- | + | ||
- | CanvasFrame frame2 = new CanvasFrame("H-S Histogram"); | + | CanvasFrame frame2 = new CanvasFrame("H-S Histogram"); |
- | frame2.showImage(hist_img); | + | frame2.showImage(hist_img); |
- | } | + | } |
- | } | + | } |
- | } | + | } |
- | </code> | + | </code> |
- | + | ||
- | + | ||
- | ====Related pages==== | + | ====Related pages==== |
- | *[[3D Color Histogram]] | + | *[[3D Color Histogram]] |