본문 바로가기
프로그래밍/OpenCV.

[OpenCV] 03.. OpenCV 히스토그램(feat.JAVA)

by _Chavi 2020. 10. 17.

이번엔 JAVA를 이용하여 RGB Histogram과 Gray Histogram을 그려보겠습니다. 

 

RGB Image Histogram
public static void showLennaHist() {
	try {
		String locLenna = "경로생략/Lenna.png";
		
		Mat imageLenna = Imgcodecs.imread(locLenna);
		
		// Histogram Start
		List<Mat> bgrPlanes = new ArrayList<>();
		Core.split(imageLenna, bgrPlanes);
		
		int histSize = 256;
		boolean accumulate = false;
		
		float[] range = {0, 256};
		MatOfFloat histRange = new MatOfFloat(range);
		Mat bHist = new Mat(), gHist = new Mat(), rHist = new Mat();
		Imgproc.calcHist(bgrPlanes, new MatOfInt(0), new Mat(), 
        				bHist, new MatOfInt(histSize), histRange, accumulate);
		Imgproc.calcHist(bgrPlanes, new MatOfInt(1), new Mat(), 
        				gHist, new MatOfInt(histSize), histRange, accumulate);
		Imgproc.calcHist(bgrPlanes, new MatOfInt(2), new Mat(), 
        				rHist, new MatOfInt(histSize), histRange, accumulate);
		
		int histW = 512, histH = 400;
		int binW = (int) Math.round((double) histW / histSize);
		
		Mat imageLennaHist = new Mat(histH, histW, CvType.CV_8UC3, new Scalar(0, 0, 0));
		
		Core.normalize(bHist, bHist, 0, imageLennaHist.rows(), Core.NORM_MINMAX);
		Core.normalize(gHist, gHist, 0, imageLennaHist.rows(), Core.NORM_MINMAX);
		Core.normalize(rHist, rHist, 0, imageLennaHist.rows(), Core.NORM_MINMAX);

		float[] bHistData = new float[(int) (bHist.total() * bHist.channels())];
		bHist.get(0, 0, bHistData);
		float[] gHistData = new float[(int) (gHist.total() * gHist.channels())];
		gHist.get(0, 0, gHistData);
		float[] rHistData = new float[(int) (rHist.total() * rHist.channels())];
		rHist.get(0, 0, rHistData);
		
		for( int i = 1; i < histSize; i++ ) {
			Imgproc.line(imageLennaHist,
            		new Point(binW * (i - 1), histH - Math.round(bHistData[i - 1])),
					new Point(binW * (i), histH - Math.round(bHistData[i])),
                    new Scalar(255, 0, 0), 1);
			Imgproc.line(imageLennaHist,
            		new Point(binW * (i - 1), histH - Math.round(gHistData[i - 1])),
					new Point(binW * (i), histH - Math.round(gHistData[i])),
                    new Scalar(0, 255, 0), 1);
			Imgproc.line(imageLennaHist,
            		new Point(binW * (i - 1), histH - Math.round(rHistData[i - 1])),
					new Point(binW * (i), histH - Math.round(rHistData[i])),
                    new Scalar(0, 0, 255), 1);
		}
		// Histogram End

		// Show
		HighGui.imshow("Lenna", imageLenna);
		HighGui.imshow("LennaHist", imageLennaHist);
		
	} catch (Exception e) {
		e.printStackTrace();
	}
}

01
실행결과

 

Gray Image Histogram
public static void showGrayLennaHist() {
	try {
		String locGrayLenna = "경로생략/LennaGray.png";
		
		Mat imageGrayLenna = Imgcodecs.imread(locGrayLenna);
		
		// Histogram Start
		List<Mat> bgrPlanes = new ArrayList<>();
		Core.split(imageGrayLenna, bgrPlanes);
		
		int histSize = 256;
		boolean accumulate = false;
		
		float[] range = {0, 256};
		MatOfFloat histRange = new MatOfFloat(range);
		Mat iHist = new Mat();
		Imgproc.calcHist(bgrPlanes, new MatOfInt(0), new Mat(),
						iHist, new MatOfInt(histSize), histRange, accumulate);
		
		int histW = 512, histH = 400;
		int binW = (int) Math.round((double) histW / histSize);
		
		Mat imageHistLenna = new Mat(histH, histW, CvType.CV_8UC3, new Scalar(0, 0, 0));
		
		Core.normalize(iHist, iHist, 0, imageHistLenna.rows(), Core.NORM_MINMAX);

		float[] iHistData = new float[(int) (iHist.total() * iHist.channels())];
		iHist.get(0, 0, iHistData);
		
		for( int i = 1; i < histSize; i++ ) {
			Imgproc.line(imageHistLenna
				,new Point(binW * (i - 1), histH - Math.round(iHistData[i - 1]))
				,new Point(binW * (i), histH - Math.round(iHistData[i]))
				,new Scalar(255, 255, 255), 1);
		}
		// Histogram End

		// Show
		HighGui.imshow("LennaGray", imageGrayLenna);
		HighGui.imshow("LennaGrayHist", imageHistLenna);
		
	} catch (Exception e) {
		e.printStackTrace();
	}
}

01
실행결과

 

댓글