|
| 1 | +#include "image_processing.h" |
| 2 | +#include <android/log.h> |
| 3 | + |
| 4 | +#define LOG_TAG "Image Processing" |
| 5 | +# define LOGI(...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__) |
| 6 | +# define LOGE(...) __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__) |
| 7 | + |
| 8 | + |
| 9 | +using namespace cv; |
| 10 | +using namespace std; |
| 11 | + |
| 12 | +/* Calculate the circularity of a contour */ |
| 13 | +double calcCircularity(vector<Point> contour) { |
| 14 | + // Check circularity |
| 15 | + double perimeter = arcLength(contour, true); |
| 16 | + double area = contourArea(contour); |
| 17 | + double circularity = 4 * 3.14159265 * area / (perimeter * perimeter); |
| 18 | + return circularity; |
| 19 | +} |
| 20 | + |
| 21 | + |
| 22 | +/* |
| 23 | + * Finds all the shapes (contours) in a region |
| 24 | + * 1. gets a thesholded matrix to find outlines |
| 25 | + * 2. gets all the contours around those outlines, contours are stored as a vector of points |
| 26 | + * |
| 27 | + */ |
| 28 | +string colorSomeStuff(Mat& mbgra) { |
| 29 | + LOGI("Using thresholds to find contrast and then contours around those highcontrast items..."); |
| 30 | + |
| 31 | + int width = mbgra.size[1]; |
| 32 | + int height = mbgra.size[0]; |
| 33 | + |
| 34 | + // Separate the image in 3 places ( B, G and R ) |
| 35 | + vector<Mat> rgbPlanes; |
| 36 | + split(mbgra, rgbPlanes); |
| 37 | + |
| 38 | + /* |
| 39 | + Get the yellow plane since most people wont write in yellow on a white board, |
| 40 | + then apply an adaptive threshold with a 31pixel window to look for contrast, |
| 41 | + save the contrast into the thresh matrix |
| 42 | + */ |
| 43 | + Mat yellow = rgbPlanes[1]/2+rgbPlanes[2]/2; |
| 44 | + Mat thresh; |
| 45 | + adaptiveThreshold(yellow, thresh, 255, ADAPTIVE_THRESH_MEAN_C, THRESH_BINARY_INV, 31, 11); |
| 46 | + //Blackboard dont do an inverse, it will already be dark on light : adaptiveThreshold(yellow, thresh, 255, ADAPTIVE_THRESH_MEAN_C, THRESH_BINARY, 31, 10); |
| 47 | + |
| 48 | + |
| 49 | + |
| 50 | + /* |
| 51 | + Take the thresh matrix, and look for contours in it |
| 52 | + */ |
| 53 | + vector<vector<Point> > contours; |
| 54 | + findContours(thresh, contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE);//### |
| 55 | + LOGI("Contours: %d", contours.size()); |
| 56 | + |
| 57 | + /* |
| 58 | + For each contour, find out how rectangle it is (between .8 and 1 is a pretty good rectangle candidate) |
| 59 | + */ |
| 60 | + vector<vector<Point> > checkboxes; |
| 61 | + for (int i = 0; i < contours.size(); i++) { |
| 62 | + Rect rect = boundingRect(contours[i]); |
| 63 | + |
| 64 | + // Make contour convex, not sure why but probably to get the outermost line of the contour |
| 65 | + vector<Point> convex; |
| 66 | + convexHull(contours[i], convex); |
| 67 | + |
| 68 | + // Check roundness |
| 69 | + double roundness = calcCircularity(convex); |
| 70 | + |
| 71 | + if (roundness < 0.8) |
| 72 | + continue; |
| 73 | + checkboxes.push_back (convex); |
| 74 | + } |
| 75 | + LOGI("Potential Checkboxes: %d", checkboxes.size()); |
| 76 | + |
| 77 | + |
| 78 | + |
| 79 | + /* |
| 80 | + * Draw the checkbox-like contours which were found in green Scalar(0, 255, 0, 255) so you can see what the function is doing... |
| 81 | + */ |
| 82 | + drawContours(mbgra, checkboxes, -1, Scalar(0, 255, 0, 255), 2); |
| 83 | + |
| 84 | + /* |
| 85 | + * Draw a line below the "roundish" objects if you wish |
| 86 | + */ |
| 87 | + for (int i = 0; i < checkboxes.size(); i++) { |
| 88 | + /* |
| 89 | + * Get the rectangle around the contour, use this if you need to know the x,y of the contour, |
| 90 | + * or if you want to know the width or height of the contour. |
| 91 | + * |
| 92 | + * WATCHOUT: if you hold the andriod in portrait, x will be y, and you y will be x. |
| 93 | + */ |
| 94 | + Rect rect = boundingRect(checkboxes[i]); |
| 95 | + |
| 96 | + /* |
| 97 | + * Draw a line across the screen, under the "checkbox" |
| 98 | + * you will see if your x is truely the x or if it is the y... |
| 99 | + */ |
| 100 | + if(rect.width >10){ |
| 101 | + int margin = 5; |
| 102 | + int width = mbgra.size[0]; |
| 103 | + int height = mbgra.size[1]; |
| 104 | + line(mbgra |
| 105 | + , Point(rect.x + rect.width+margin, 0) |
| 106 | + , Point(rect.x + rect.width, height+margin) |
| 107 | + , Scalar(200, 200, 0, 255) |
| 108 | + , 2); |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + /* |
| 113 | + * Draw the threshold matrix in red Scalar(0, 0, 255, 255)as a mask on top of the image |
| 114 | + */ |
| 115 | + mbgra.setTo(Scalar(0, 0, 255, 255), thresh); |
| 116 | + |
| 117 | + /* |
| 118 | + * Concatinate a string and return it to the Java, |
| 119 | + * if you have something that you want to return |
| 120 | + */ |
| 121 | + stringstream ss; |
| 122 | + ss << "This is the result for this frame " << contours.size(); |
| 123 | + string resultString = ss.str(); |
| 124 | + |
| 125 | + return resultString; |
| 126 | +} |
0 commit comments