获取连通区域

阅读更多

 

获取连通区域,jdk8,引入dcm4che-core-5.11.0.jar和opencv-320.jar

	public static void getConnectedRegion() {
		try {
			System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
			File f = new File("F:\\dcm\\a\\00000002.DCM");
			int[]huarray = DicomUtil.getHUArray(f);
			byte[]bwarray  = new byte[huarray.length];
			for (int i = 0; i < huarray.length; i++) {
				if (huarray[i]>-500) {
					bwarray[i]=127;
				}else {
					bwarray[i]=0;
				}
			}
			Mat src = new Mat(512, 512, CvType.CV_8U);
			src.put(0, 0, bwarray);
			//canny边缘检测
//			Imgproc.Canny(src, dest, 100, 300);
			List contours = new ArrayList();
			Mat hierarchy = src.clone();
			/**
			 *  CV_RETR_EXTERNAL = 0,//表示只检测外轮廓
	            CV_RETR_LIST = 1,//检测的轮廓不建立等级关系
	            CV_RETR_CCOMP = 2,//建立两个等级的轮廓,上面的一层为外边界,里面的一层为内孔的边界信息。如果内孔内还有一个连通物体,这个物体的边界也在顶层。
	            CV_RETR_TREE = 3,//建立一个等级树结构的轮廓。
	            CV_RETR_FLOODFILL = 4,
	            
			 */
			int mode =  Imgproc.RETR_EXTERNAL;//
			/**
			 *  CV_CHAIN_APPROX_NONE = 1,//存储所有的轮廓点,相邻的两个点的像素位置差不超过1,即max(abs(x1-x2),abs(y2-y1))==1  
	            CV_CHAIN_APPROX_SIMPLE = 2,//压缩水平方向,垂直方向,对角线方向的元素,只保留该方向的终点坐标,例如一个矩形轮廓只需4个点来保存轮廓信息   
	            CV_CHAIN_APPROX_TC89_L1 = 3,//使用teh-Chinl chain 近似算法
	            CV_CHAIN_APPROX_TC89_KCOS = 4,//使用teh-Chinl chain 近似算法
			 */
			int method = Imgproc.CHAIN_APPROX_NONE;

			Imgproc.findContours(src, contours, hierarchy, mode, method);
			System.out.println("Contours size:"+contours.size());
			System.out.println("hierarchy size:"+hierarchy.size());
			
			List colorList = new ArrayList();
//			colorList.add(new Color(255, 255, 255));
			colorList.add(new Color(0, 255, 255));
			colorList.add(new Color(255, 0, 255));
			colorList.add(new Color(255, 255, 0));
			colorList.add(new Color(0, 0, 255));
			colorList.add(new Color(255, 0, 0));
			colorList.add(new Color(0, 255, 0));
			colorList.add(new Color(0, 0, 0));
			
			
			BufferedImage bi = new BufferedImage(512, 512, BufferedImage.TYPE_INT_RGB);
			
			Mat dest = src.clone();
			
			for(int i=0;i pointList = mp.toList();
//				System.out.println(pointList.size());
					int point_size = pointList.size();
					if (point_size>100) {
						int sum_x = 0;
						int sum_y = 0;
						int index_color = i%colorList.size();
						int rgb = colorList.get(index_color).getRGB();
						for (Point point : pointList) {
//					System.out.println(point.x+"="+point.y);
							int x = new Double(point.x).intValue();
							int y = new Double(point.y).intValue();
							bi.setRGB(x,y,rgb);
							sum_x+=x;
							sum_y+=y;
						}
						System.out.println(sum_x/point_size);
						System.out.println(sum_y/point_size);
						System.out.println("===");
					}
				}
		    } 
//			Imgcodecs.imwrite("F:\\dcm\\image\\5.jpg", dest);
			ImageIO.write(bi, "jpg", new File("F:\\dcm\\image\\2.jpg"));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

 

你可能感兴趣的:(获取连通区域)