I have been fiddling around with convex hulls in OpenCV trying to detect an irregularly shaped wound (in order to calculate its size later on using a reference marker). The problem is that the wound's irregularity in color is providing a problem with thresholding. I can't exactly figure out what kind of preprocessing do I need or how to set applicable color ranges.
I tried to apply a Gaussian blur, dilation, and fiddle around with the Red ranges, so far to no avail.
private void detectWound(String procpath) {
Bitmap bitmap = BitmapFactory.decodeFile(procpath);
Mat mat = new Mat(bitmap.getWidth(), bitmap.getHeight(), CvType.CV_8UC3);
Utils.bitmapToMat(bitmap, mat);
Mat rgbMat = new Mat();
Imgproc.cvtColor(mat, rgbMat, Imgproc.COLOR_RGBA2BGR);
/**Mat dilatedMat = new Mat();
Mat kernel = Imgproc.getStructuringElement(Imgproc.MORPH_ELLIPSE, new Size(7, 7));
Imgproc.morphologyEx(rgbMat, dilatedMat, Imgproc.MORPH_OPEN, kernel);**/
//red
Mat redMat = new Mat();
Core.inRange(rgbMat, new Scalar(0, 0, 120), new Scalar(100, 100, 255), redMat);
//find contour
Mat ghierarchy = new Mat();
List<MatOfPoint> gcontours = new ArrayList<>();
Mat rhierarchy = new Mat();
List<MatOfPoint> rcontours = new ArrayList<>();
Imgproc.findContours(redMat, rcontours, rhierarchy, Imgproc.RETR_TREE, Imgproc.CHAIN_APPROX_SIMPLE);
List<MatOfPoint> rhullList = new ArrayList<>();
for (MatOfPoint contour : rcontours) {
MatOfInt hull = new MatOfInt();
Imgproc.convexHull(contour, hull);
Point[] contourArray = contour.toArray();
Point[] hullPoints = new Point[hull.rows()];
List<Integer> hullContourIdxList = hull.toList();
for (int i = 0; i < hullContourIdxList.size(); i++) {
hullPoints[i] = contourArray[hullContourIdxList.get(i)];
}
rhullList.add(new MatOfPoint(hullPoints));
}
double rlargest_area =0;
int rlargest_contour_index = 0;
for (int contourIdx = 0; contourIdx < rcontours.size(); contourIdx++) {
double contourArea = Imgproc.contourArea(rcontours.get(contourIdx));
if (contourArea > rlargest_area) {
rlargest_area = contourArea;
rlargest_contour_index = contourIdx;
}
}
double rcurrentMax = 0;
for (MatOfPoint c: rhullList){
double area= Imgproc.contourArea(c);
if(area>rcurrentMax){
rcurrentMax = area;
}
}
Imgproc.drawContours(mat, rhullList, rlargest_contour_index, new Scalar(0, 255, 0, 255), 3);
}
Expected result: https://i.ibb.co/TrmyDG0/Inkedmotasem2-LI.jpg
Actual result: https://i.ibb.co/ccdkCkj/motasem2-jpg-copy2.png
I would appreciate any tips pointing me towards the right direction!