mirror of https://github.com/opencv/opencv.git
parent
9ff33dacfc
commit
d99ced6ec8
8 changed files with 319 additions and 96 deletions
After Width: | Height: | Size: 77 KiB |
@ -1,71 +0,0 @@ |
||||
#include "opencv2/imgcodecs.hpp" |
||||
#include "opencv2/highgui.hpp" |
||||
#include "opencv2/imgproc.hpp" |
||||
|
||||
#include <iostream> |
||||
|
||||
using namespace cv; |
||||
using namespace std; |
||||
|
||||
static void help() |
||||
{ |
||||
cout << "\nThis program demonstrates circle finding with the Hough transform.\n" |
||||
"Usage:\n" |
||||
"./houghcircles <image_name>, Default is ../data/board.jpg\n" << endl; |
||||
} |
||||
|
||||
int main(int argc, char** argv) |
||||
{ |
||||
cv::CommandLineParser parser(argc, argv, |
||||
"{help h ||}{@image|../data/board.jpg|}" |
||||
); |
||||
if (parser.has("help")) |
||||
{ |
||||
help(); |
||||
return 0; |
||||
} |
||||
//![load]
|
||||
string filename = parser.get<string>("@image"); |
||||
Mat img = imread(filename, IMREAD_COLOR); |
||||
if(img.empty()) |
||||
{ |
||||
help(); |
||||
cout << "can not open " << filename << endl; |
||||
return -1; |
||||
} |
||||
//![load]
|
||||
|
||||
//![convert_to_gray]
|
||||
Mat gray; |
||||
cvtColor(img, gray, COLOR_BGR2GRAY); |
||||
//![convert_to_gray]
|
||||
|
||||
//![reduce_noise]
|
||||
medianBlur(gray, gray, 5); |
||||
//![reduce_noise]
|
||||
|
||||
//![houghcircles]
|
||||
vector<Vec3f> circles; |
||||
HoughCircles(gray, circles, HOUGH_GRADIENT, 1, |
||||
gray.rows/16, // change this value to detect circles with different distances to each other
|
||||
100, 30, 1, 30 // change the last two parameters
|
||||
// (min_radius & max_radius) to detect larger circles
|
||||
); |
||||
//![houghcircles]
|
||||
|
||||
//![draw]
|
||||
for( size_t i = 0; i < circles.size(); i++ ) |
||||
{ |
||||
Vec3i c = circles[i]; |
||||
circle( img, Point(c[0], c[1]), c[2], Scalar(0,0,255), 3, LINE_AA); |
||||
circle( img, Point(c[0], c[1]), 2, Scalar(0,255,0), 3, LINE_AA); |
||||
} |
||||
//![draw]
|
||||
|
||||
//![display]
|
||||
imshow("detected circles", img); |
||||
waitKey(); |
||||
//![display]
|
||||
|
||||
return 0; |
||||
} |
@ -0,0 +1,65 @@ |
||||
/**
|
||||
* @file houghcircles.cpp |
||||
* @brief This program demonstrates circle finding with the Hough transform |
||||
*/ |
||||
#include "opencv2/imgcodecs.hpp" |
||||
#include "opencv2/highgui.hpp" |
||||
#include "opencv2/imgproc.hpp" |
||||
|
||||
using namespace cv; |
||||
using namespace std; |
||||
|
||||
int main(int argc, char** argv) |
||||
{ |
||||
//![load]
|
||||
const char* filename = argc >=2 ? argv[1] : "../../../data/smarties.png"; |
||||
|
||||
// Loads an image
|
||||
Mat src = imread( filename, IMREAD_COLOR ); |
||||
|
||||
// Check if image is loaded fine
|
||||
if(src.empty()){ |
||||
printf(" Error opening image\n"); |
||||
printf(" Program Arguments: [image_name -- default %s] \n", filename); |
||||
return -1; |
||||
} |
||||
//![load]
|
||||
|
||||
//![convert_to_gray]
|
||||
Mat gray; |
||||
cvtColor(src, gray, COLOR_BGR2GRAY); |
||||
//![convert_to_gray]
|
||||
|
||||
//![reduce_noise]
|
||||
medianBlur(gray, gray, 5); |
||||
//![reduce_noise]
|
||||
|
||||
//![houghcircles]
|
||||
vector<Vec3f> circles; |
||||
HoughCircles(gray, circles, HOUGH_GRADIENT, 1, |
||||
gray.rows/16, // change this value to detect circles with different distances to each other
|
||||
100, 30, 1, 30 // change the last two parameters
|
||||
// (min_radius & max_radius) to detect larger circles
|
||||
); |
||||
//![houghcircles]
|
||||
|
||||
//![draw]
|
||||
for( size_t i = 0; i < circles.size(); i++ ) |
||||
{ |
||||
Vec3i c = circles[i]; |
||||
Point center = Point(c[0], c[1]); |
||||
// circle center
|
||||
circle( src, center, 1, Scalar(0,100,100), 3, LINE_AA); |
||||
// circle outline
|
||||
int radius = c[2]; |
||||
circle( src, center, radius, Scalar(255,0,255), 3, LINE_AA); |
||||
} |
||||
//![draw]
|
||||
|
||||
//![display]
|
||||
imshow("detected circles", src); |
||||
waitKey(); |
||||
//![display]
|
||||
|
||||
return 0; |
||||
} |
After Width: | Height: | Size: 89 KiB |
@ -0,0 +1,77 @@ |
||||
package sample; |
||||
/** |
||||
* @file HoughCircles.java |
||||
* @brief This program demonstrates circle finding with the Hough transform |
||||
*/ |
||||
|
||||
import org.opencv.core.*; |
||||
import org.opencv.core.Point; |
||||
import org.opencv.highgui.HighGui; |
||||
import org.opencv.imgcodecs.Imgcodecs; |
||||
import org.opencv.imgproc.Imgproc; |
||||
|
||||
class HoughCirclesRun { |
||||
|
||||
public void run(String[] args) { |
||||
|
||||
//! [load]
|
||||
String default_file = "../../../../data/smarties.png"; |
||||
String filename = ((args.length > 0) ? args[0] : default_file); |
||||
|
||||
// Load an image
|
||||
Mat src = Imgcodecs.imread(filename, Imgcodecs.IMREAD_COLOR); |
||||
|
||||
// Check if image is loaded fine
|
||||
if( src.empty() ) { |
||||
System.out.println("Error opening image!"); |
||||
System.out.println("Program Arguments: [image_name -- default " |
||||
+ default_file +"] \n"); |
||||
System.exit(-1); |
||||
} |
||||
//! [load]
|
||||
|
||||
//! [convert_to_gray]
|
||||
Mat gray = new Mat(); |
||||
Imgproc.cvtColor(src, gray, Imgproc.COLOR_BGR2GRAY); |
||||
//! [convert_to_gray]
|
||||
|
||||
//![reduce_noise]
|
||||
Imgproc.medianBlur(gray, gray, 5); |
||||
//![reduce_noise]
|
||||
|
||||
//! [houghcircles]
|
||||
Mat circles = new Mat(); |
||||
Imgproc.HoughCircles(gray, circles, Imgproc.HOUGH_GRADIENT, 1.0, |
||||
(double)gray.rows()/16, // change this value to detect circles with different distances to each other
|
||||
100.0, 30.0, 1, 30); // change the last two parameters
|
||||
// (min_radius & max_radius) to detect larger circles
|
||||
//! [houghcircles]
|
||||
|
||||
//! [draw]
|
||||
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])); |
||||
// circle center
|
||||
Imgproc.circle(src, center, 1, new Scalar(0,100,100), 3, 8, 0 ); |
||||
// circle outline
|
||||
int radius = (int) Math.round(c[2]); |
||||
Imgproc.circle(src, center, radius, new Scalar(255,0,255), 3, 8, 0 ); |
||||
} |
||||
//! [draw]
|
||||
|
||||
//! [display]
|
||||
HighGui.imshow("detected circles", src); |
||||
HighGui.waitKey(); |
||||
//! [display]
|
||||
|
||||
System.exit(0); |
||||
} |
||||
} |
||||
|
||||
public class HoughCircles { |
||||
public static void main(String[] args) { |
||||
// Load the native library.
|
||||
System.loadLibrary(Core.NATIVE_LIBRARY_NAME); |
||||
new HoughCirclesRun().run(args); |
||||
} |
||||
} |
@ -0,0 +1,59 @@ |
||||
import sys |
||||
import cv2 |
||||
import numpy as np |
||||
|
||||
|
||||
def main(argv): |
||||
## [load] |
||||
default_file = "../../../../data/smarties.png" |
||||
filename = argv[0] if len(argv) > 0 else default_file |
||||
|
||||
# Loads an image |
||||
src = cv2.imread(filename, cv2.IMREAD_COLOR) |
||||
|
||||
# Check if image is loaded fine |
||||
if src is None: |
||||
print ('Error opening image!') |
||||
print ('Usage: hough_circle.py [image_name -- default ' + default_file + '] \n') |
||||
return -1 |
||||
## [load] |
||||
|
||||
## [convert_to_gray] |
||||
# Convert it to gray |
||||
gray = cv2.cvtColor(src, cv2.COLOR_BGR2GRAY) |
||||
## [convert_to_gray] |
||||
|
||||
## [reduce_noise] |
||||
# Reduce the noise to avoid false circle detection |
||||
gray = cv2.medianBlur(gray, 5) |
||||
## [reduce_noise] |
||||
|
||||
## [houghcircles] |
||||
rows = gray.shape[0] |
||||
circles = cv2.HoughCircles(gray, cv2.HOUGH_GRADIENT, 1, rows / 8, |
||||
param1=100, param2=30, |
||||
minRadius=1, maxRadius=30) |
||||
## [houghcircles] |
||||
|
||||
## [draw] |
||||
if circles is not None: |
||||
circles = np.uint16(np.around(circles)) |
||||
for i in circles[0, :]: |
||||
center = (i[0], i[1]) |
||||
# circle center |
||||
cv2.circle(src, center, 1, (0, 100, 100), 3) |
||||
# circle outline |
||||
radius = i[2] |
||||
cv2.circle(src, center, radius, (255, 0, 255), 3) |
||||
## [draw] |
||||
|
||||
## [display] |
||||
cv2.imshow("detected circles", src) |
||||
cv2.waitKey(0) |
||||
## [display] |
||||
|
||||
return 0 |
||||
|
||||
|
||||
if __name__ == "__main__": |
||||
main(sys.argv[1:]) |
Loading…
Reference in new issue