Skip to content

Commit 86a4fdb

Browse files
committed
First
0 parents  commit 86a4fdb

File tree

4 files changed

+134
-0
lines changed

4 files changed

+134
-0
lines changed

.DS_Store

6 KB
Binary file not shown.

README.md

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# Circle Detection
2+
3+
This project contains a Python code that detects the pixel diameters using Python and OpenCV, and marks them in green color.
4+
5+
## Example
6+
7+
In example, code detects the pixel diameters of the 5 bolt holes and the center hub on a wheel image.
8+
9+
> Input Image
10+
![Image1](https://github.com/mstftmk/Circle-Detection/blob/main/Images/Image1.png?raw=true)
11+
12+
> Result Image
13+
![Image2](https://github.com/mstftmk/Circle-Detection/blob/main/Images/Image2.png?raw=true)
14+
15+
16+
## Requirements
17+
18+
Install the required dependencies:
19+
20+
```shell
21+
pip install -r requirements.txt
22+
```
23+
24+
## Usage
25+
26+
1. Clone the repository to your local machine using the following command:
27+
28+
```shell
29+
git clone https://github.com/mstftmk/Circle-Detection.git
30+
```
31+
32+
2. Navigate to the project directory:
33+
```shell
34+
cd repo-path
35+
```
36+
37+
3. Run the code:
38+
39+
```shell
40+
python circle_detection.py Image1.png
41+
```
42+
43+
## Contributions
44+
45+
Contributions to the project are welcome! If you find any bugs or want to enhance the functionality, feel free to open an issue or submit a pull request.
46+
47+
- Fork the project (https://github.com/mstftmk/Circle-Detection/fork)
48+
- Create your feature branch (git checkout -b feature/fooBar)
49+
- Commit your changes (git commit -am 'Add some fooBar')
50+
- Push to the branch (git push origin feature/fooBar)
51+
- Create a new Pull Request

circle_detection.py

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
import cv2
2+
import numpy as np
3+
import sys
4+
import os
5+
6+
def circle_detect(img_path):
7+
'''
8+
Bu kod, terminal üzerinden circle detection dosyasını çalıştırmak için yazılmıştır.
9+
10+
dp=1, #dp: çözünürlük oranı.
11+
minDist=200, #minDist: algılanabilecek daireler arasındaki minimum mesafe.
12+
param1=255, # param1: Canny Edge Detector için üst eşik değeri.
13+
param2=20, # param2: dairesel merkezlerin tespiti için eşik değeri.
14+
minRadius=50, # minRadius: algılanacak dairelerin minimum yarıçapı.
15+
maxRadius=5000 # maxRadius: algılanacak dairelerin maksimum yarıçapı.
16+
'''
17+
18+
image_path = img_path # Görselin Path'ini alıyoruz.
19+
image = cv2.imread(image_path, cv2.IMREAD_COLOR) # Path üzerinden görseli okuyoruz.
20+
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) # Görseli Grayscale olarak convert ediyoruz.
21+
edges = cv2.Canny(gray, threshold1=254, threshold2=255) # Edge Detection için Canny yöntemini kullanıyoruz. threshold1: kenar olarak kabul edilen zayıf piksel değerlerinin alt sınırı, threshold2: kenar olarak kabul edilen güçlü piksel değerlerinin üst sınırı.
22+
23+
# Morfolojik işlemler uygulayarak bir maskeye dönüştürüyoruz.
24+
# kernel: morfolojik işlemler için kullanılan kernel, burada 5x5 boyutunda bir matris. Çünkü yüksek çözünürlüklü görseller için 5x5 matris kullanabiliriz. Yoksa 3x3 kullanmalıydık.
25+
kernel = np.ones((5, 5), np.uint8)
26+
closed_mask = cv2.morphologyEx(edges, cv2.MORPH_CLOSE, kernel, iterations=4) # cv2.MORPH_CLOSE, morfolojik kapanma işlemi olarak bilinir. Kenarların içerisindeki küçük boşlukları doldurur. iterations: işlemi kaç kez tekrarlayacağını belirler, burada 4 kez tekrarlıyoruz.
27+
28+
circles = cv2.HoughCircles( #Hough Dönüşümü ile daireleri algılıyoruz.
29+
closed_mask,
30+
cv2.HOUGH_GRADIENT,
31+
dp=1,
32+
minDist=200,
33+
param1=255,
34+
param2=20,
35+
minRadius=50,
36+
maxRadius=5000
37+
)
38+
39+
if circles is not None: # Eğer daireler algılandıysa, her bir daireyi çiziyoruz ve yarıçap bilgisini yazdırıyoruz.
40+
circles = np.uint16(np.around(circles))
41+
for circle in circles[0, :]:
42+
cv2.circle(image, (circle[0], circle[1]), circle[2], (0, 255, 0), 2)
43+
cv2.circle(image, (circle[0], circle[1]), 2, (0, 0, 255), 3)
44+
45+
radius_info = f"Radius: {circle[2]}"
46+
text_size = cv2.getTextSize(radius_info, cv2.FONT_HERSHEY_SIMPLEX, 0.5, 2)[0]
47+
text_x = circle[0] - text_size[0] // 2
48+
text_y = circle[1] + circle[2] + text_size[1] + 5
49+
cv2.putText(image, radius_info, (text_x, text_y), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 2)
50+
51+
# Sonunda, detect edilen daireler ile görüntüyü kaydediyoruz ve bununla ilgili kullanıcıyı bilgilendiriyoruz.
52+
output_path = os.path.splitext(image_path)[0] + '_output.jpg'
53+
cv2.imwrite(output_path, image)
54+
cv2.imshow("Result", image)
55+
print(f"Press any key to exit.")
56+
cv2.waitKey(0)
57+
cv2.destroyAllWindows()
58+
print(f"Result image saved at: {output_path}")
59+
60+
61+
def main():
62+
if (len(sys.argv) < 2):
63+
print("Usage of this code: python3 circle_detection.py <image_path>")
64+
sys.exit(1)
65+
else:
66+
if len(sys.argv[1]) <= 2:
67+
print("Usage of this code: python3 circle_detection.py <image_path>")
68+
sys.exit(1)
69+
else:
70+
image = sys.argv[1]
71+
circle_detect(image)
72+
73+
74+
if len(sys.argv) < 2:
75+
print("Kullanım: python3 circle_detection.py <label_folder>")
76+
sys.exit(1)
77+
78+
79+
80+
if __name__ == "__main__":
81+
main()

requirements.txt

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
opencv-python
2+
numpy

0 commit comments

Comments
 (0)