2번 동영상 코드에 원찾기 코드 추가
import org.opencv.core.*;
import org.opencv.highgui.HighGui;
import org.opencv.imgproc.Imgproc;
import org.opencv.videoio.VideoCapture;
class MyVideoTest {
public void run() {
String filename = "WIN_20220603_16_47_52_Pro.mp4";
VideoCapture capture = new VideoCapture(filename);
if (!capture.isOpened()) {
System.out.println("Unable to open this stream");
System.exit(-1);
}
Mat frame = new Mat();
Mat frame_gray = new Mat();
while (capture.read(frame)) {
if (frame.empty()) {
break;
}
// Image processing start
Imgproc.cvtColor(frame, frame_gray, Imgproc.COLOR_BGR2GRAY);
//Blur
Imgproc.medianBlur(frame_gray, frame_gray, 5);
// HSV 이미지 만들기
Mat frame_hsv = new Mat();
Imgproc.cvtColor(frame, frame_hsv, Imgproc.COLOR_BGR2HSV);
Imgproc.medianBlur(frame_hsv, frame_hsv, 5);
Mat circles = new Mat();
Imgproc.HoughCircles(frame_gray, circles, Imgproc.HOUGH_GRADIENT, 1.0,
(double)frame_gray.rows()/16, // change this value to detect circles with different distances to each other
100.0, 30.0, 165, 175); // change the last two parameters
// (min_radius & max_radius) to detect larger circles
for (int x = 0; x < circles.cols(); x++) {
double[] c = circles.get(0, x);
if(c != null) {
Point center = new Point(Math.round(c[0]), Math.round(c[1]));
//hsv
int i = (int) Math.round(c[0]);
int j = (int) Math.round(c[1]);
double[] data = frame_hsv.get(i,j);
//System.out.println(data.length);
if (data != null) {
System.out.printf("(%d %d) => h:%.1f s:%.1f v:%.1f\n",i,j,
frame_hsv.get(i,j)[0],frame_hsv.get(i,j)[1],frame_hsv.get(i,j)[2]);
// 중심점 x, y , HSV(H) , HSV(S), HSV(V)
}
// circle center
Imgproc.circle(frame, center, 1, new Scalar(0,255,255), 10, 8, 0 );
// circle outline
int radius = (int) Math.round(c[2]);
System.out.println(radius);
Imgproc.circle(frame, center, radius, new Scalar(0,0,255), 3, 8, 0 );
}
}
// Image processing End
HighGui.imshow("Frame", frame);
int keyboard = HighGui.waitKey(30);
if (keyboard == 'q' || keyboard == 27) {
break;
}
}
System.exit(0);
}
}
public class opencv_run {
public static void main(String[] args) {
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
new MyVideoTest().run();
}
}
https://docs.opencv.org/4.x/d4/d70/tutorial_hough_circle.html
OpenCV: Hough Circle Transform
Prev Tutorial: Hough Line Transform Next Tutorial: Remapping Original author Ana Huamán Compatibility OpenCV >= 3.0 Goal In this tutorial you will learn how to: Use the OpenCV function HoughCircles() to detect circles in an image. Theory Hough Circle Tran
docs.opencv.org
public class opencv_run {
public static void main(String[] args) {
// opencv 라이브러리를 읽어오기
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
// 원본이미지 파일 읽어오기
String default_file = "WIN_20220527_09_35_04_Pro.jpg";
String filename = ((args.length > 0) ? args[0] : default_file);
// Load an image
Mat frame = Imgcodecs.imread(filename, Imgcodecs.IMREAD_COLOR);
// Check if image is loaded fine
if( frame.empty() ) {
System.out.println("Error opening image!");
System.out.println("Program Arguments: [image_name -- default "
+ default_file +"] \n");
System.exit(-1);
}
// 이미지를 Gray로 바꾸기
Mat frame_gray = new Mat();
Imgproc.cvtColor(frame, frame_gray, Imgproc.COLOR_BGR2GRAY);
//Blur
Imgproc.medianBlur(frame_gray, frame_gray, 5);
// HSV 이미지 만들기
Mat frame_hsv = new Mat();
Imgproc.cvtColor(frame, frame_hsv, Imgproc.COLOR_BGR2HSV);
Imgproc.medianBlur(frame_hsv, frame_hsv, 5);
Mat circles = new Mat();
Imgproc.HoughCircles(frame_gray, circles, Imgproc.HOUGH_GRADIENT, 1.0,
(double)frame_gray.rows()/16, // change this value to detect circles with different distances to each other
100.0, 30.0, 160, 190); // change the last two parameters
// (min_radius & max_radius) to detect larger circles
for (int x = 0; x < circles.cols(); x++) {
double[] c = circles.get(0, x);
Point center = new Point(Math.round(c[0]), Math.round(c[1]));
//hsv
int i = (int) Math.round(c[0]);
int j = (int) Math.round(c[1]);
double[] data = frame_hsv.get(i,j);
//System.out.println(data.length);
System.out.printf("%d %d %.1f %.1f %.1f\n",i,j,
frame_hsv.get(i,j)[0],frame_hsv.get(i,j)[1],frame_hsv.get(i,j)[2]);
// 중심점 x, y , HSV(H) , HSV(S), HSV(V)
// circle center
Imgproc.circle(frame, center, 1, new Scalar(0,255,255), 10, 8, 0 );
// circle outline
int radius = (int) Math.round(c[2]);
System.out.println(radius);
Imgproc.circle(frame, center, radius, new Scalar(0,0,255), 3, 8, 0 );
}
// 이미지 결과 출력
HighGui.imshow("Original Image", frame);
HighGui.waitKey(0);
System.exit(0);
}
}
'PROGRAM > OpenCV' 카테고리의 다른 글
OpenCV-Java #2 동영상 / 카메라 제어 (0) | 2022.06.07 |
---|---|
OpenCV-JAVA #1 시작하기 (0) | 2022.05.27 |
Contour 최대 최소값 찾기 (0) | 2021.04.02 |
Object Tracking (0) | 2021.01.12 |
pyQt5 + cv2.findContours (0) | 2020.06.11 |