我已经花了一些时间尝试从植物叶子的轮廓中实现链码,直到我遇到this question声称可以使用OpenCV方法findContours
找到链码。
到目前为止,我有这段代码:
public static List<MatOfPoint> chainCode;
public static void main(String[] args) {
// load the Core OpenCV library by name
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
// create a display window
Imshow window1 = new Imshow("My Image - original");
// load an image from file (read and decode JPEG file)
Mat inputImg = Highgui.imread("files/11. Acer palmaturu/iPAD2_C11_EX01.JPG");
Mat grayImg = new Mat(inputImg.height(), inputImg.width(), CvType.CV_8UC1);
//turn into binary image and invert
Imgproc.cvtColor(inputImg, grayImg, Imgproc.COLOR_RGB2GRAY);
Imgproc.threshold(grayImg, grayImg, 0, 255, Imgproc.THRESH_BINARY | Imgproc.THRESH_OTSU);
Mat invertcolormatrix= new Mat(grayImg.rows(),grayImg.cols(), grayImg.type(), new Scalar(255,255,255));
Core.subtract(invertcolormatrix, grayImg, grayImg);
//get chain code
chainCode = new Vector<MatOfPoint>();
Imgproc.findContours(grayImg, chainCode, new Mat(), Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_NONE);
//loop through indices of individual contours to find largest contour
double largest_area = 0;
int largest_contour_index = 0;
for(int i = 0; i< chainCode.size(); i++){
//find the area of the contour
double a = Imgproc.contourArea(chainCode.get(i),false);
//find largest contour and it's index
if(a > largest_area){
largest_area= a;
largest_contour_index = i;
}
}
//draw largest contour
if(chainCode.size() > 0){
Imgproc.drawContours(inputImg, chainCode, largest_contour_index, new Scalar (0, 0, 255), 1);
System.out.println("chain code at " + largest_contour_index + "; " + chainCode.get(largest_contour_index));
}
//show image
window1.showImage(inputImg);
}
现在我被困在可以获得原始链码的地方,如V = 000567454 .... 或者我应该采用完全不同的方法尝试?因为我真的被卡住了,请有人给我一些帮助!
我希望稍后使用链式代码进行进一步处理和图像识别。