Skip to content

Commit

Permalink
hough circle transform extended
Browse files Browse the repository at this point in the history
  • Loading branch information
isinsuarici committed Jul 5, 2022
1 parent eea61fc commit 7dc6ea5
Show file tree
Hide file tree
Showing 18 changed files with 180 additions and 4 deletions.
17 changes: 17 additions & 0 deletions GUIFeatures/DrawingFunctions/Drawing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import numpy as np
import cv2 as cv


def draw_circle(event, x, y, flags, param):
if event == cv.EVENT_LBUTTONDBLCLK: # çift tıklamada çizer
cv.circle(img, (x, y), 100, (200, 110, 200), -1)


img = np.zeros((515, 1000, 3), np.uint8) # pencere boyutları
cv.namedWindow('img')
cv.setMouseCallback('img', draw_circle)
while True:
cv.imshow('img', img)
if cv.waitKey(20) & 0xFF == 27: # esc ye basınca çıkış
break
cv.destroyAllWindows()
32 changes: 32 additions & 0 deletions GUIFeatures/Trackbar/Trackbar.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import numpy as np
import cv2 as cv


def nothing(x):
pass


img = np.zeros((300, 512, 3), np.uint8)
cv.namedWindow('image')

cv.createTrackbar('R', 'image', 0, 255, nothing)
cv.createTrackbar('G', 'image', 0, 255, nothing)
cv.createTrackbar('B', 'image', 0, 255, nothing)

switch = '0 : OFF \n1 : ON'
cv.createTrackbar(switch, 'image', 0, 1, nothing)
while True:
cv.imshow('image', img)
k = cv.waitKey(1) & 0xFF
if k == 27: # ESC
break

r = cv.getTrackbarPos('R', 'image')
g = cv.getTrackbarPos('G', 'image')
b = cv.getTrackbarPos('B', 'image')
s = cv.getTrackbarPos(switch, 'image')
if s == 0:
img[:] = 0
else:
img[:] = [b, g, r]
cv.destroyAllWindows()
17 changes: 17 additions & 0 deletions GUIFeatures/VideoFromCamera/CaptureVideo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import cv2 as cv

cap = cv.VideoCapture(0) # 1'de ikinci kamerayı açar
if not cap.isOpened():
print("Cant open the camera")
exit()
while True:
ret, frame = cap.read() # frameleri döner
if not ret:
print("Cant receive frame")
break
gray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)
cv.imshow('frame', gray)
if cv.waitKey() == ord('s'):
break
cap.release()
cv.destroyAllWindows()
14 changes: 14 additions & 0 deletions GUIFeatures/VideoFromCamera/PlayingVideo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import cv2 as cv

cap = cv.VideoCapture('output.avi')
while cap.isOpened():
ret, frame = cap.read()
if not ret:
print("Can't receive the frame")
break
gray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)
cv.imshow('frame', gray)
if cv.waitKey(1) == ord('q'):
break
cap.release()
cv.destroyAllWindows()
18 changes: 18 additions & 0 deletions GUIFeatures/VideoFromCamera/SaveVideo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import cv2 as cv

cap = cv.VideoCapture(0)
fourcc = cv.VideoWriter_fourcc(*'DIVX')
out = cv.VideoWriter('output.avi', fourcc, 20.0, (640, 480))
while cap.isOpened():
ret, frame = cap.read()
if not ret:
print("Can't receive the frame")
break
frame = cv.flip(frame, 0)
out.write(frame)
cv.imshow('frame', frame)
if cv.waitKey(1) == ord('q'):
break
cap.release()
out.release()
cv.destroyAllWindows()
34 changes: 34 additions & 0 deletions HoughCircleTransform-2/Hough-Ex1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import cv2 as cv
import numpy as np
import argparse

# konsoldan çalıştırmak için:
# python Hough-Ex1.py -i ../input_pictures/soda.png

# minDist çok küçük olursa daire olmayan yerlerde bile daire algılayabiliriz.
# minDist çok büyük olursa bazı daireleri algılayamayabiliriz
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", required=True, help="Path of image")
args = vars(ap.parse_args())

img = cv.imread(args["image"])
img2 = img.copy() # bu işlemi yaptığım yere göre outputun size'ı(kB) değişiyor.

img = cv.GaussianBlur(img, (7, 7), 1.5)


img = cv.cvtColor(img, cv.COLOR_BGR2GRAY)

circles = cv.HoughCircles(img, cv.HOUGH_GRADIENT, 1.3, 30, param1=60, param2=70)

# param2 küçükse yanlış daireler de algılıyor
circles = np.uint16(np.around(circles))

for c in circles[0, :]:
cv.circle(img2, (c[0], c[1]), c[2], (0, 255, 0), 2)
cv.circle(img2, (c[0], c[1]), 1, (0, 0, 255), 3)

cv.imshow("img", img2)
# cv.imshow("img", np.hstack([img, img2]))
cv.waitKey(0)
cv.imwrite("houghcircle-ex1.png", img2)
37 changes: 37 additions & 0 deletions HoughCircleTransform-2/Hough-Ex2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import cv2 as cv
import numpy as np
import argparse

# konsoldan çalıştırmak için:
# python Hough-Ex2.py -i ../input_pictures/input_bobin.png

ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", required=True, help="Path of image")
args = vars(ap.parse_args())

img = cv.imread(args["image"])
img2 = img.copy()
img = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
img = cv.GaussianBlur(img, (7, 7), 1.5)
img = cv.Canny(img, 50, 120)

cv.imshow("image with blur and grayscale", img)
circles = cv.HoughCircles(img, cv.HOUGH_GRADIENT, 1, 20, param1=50, param2=30, minRadius=0, maxRadius=0)

# param1 : Upper threshold for the internal Canny edge detector.
# param2 : Threshold for center detection.


# dp 0 ile 2 arasında double sayılar olmalı
# param2 küçükse yanlış daireler de algılıyor
circles = np.uint16(np.around(circles))

for c in circles[0, :]:
print(c) # x, y, r
cv.circle(img2, (c[0], c[1]), c[2], (0, 255, 0), 2)
cv.circle(img2, (c[0], c[1]), 1, (0, 0, 255), 3)

cv.imshow(" output img", img2)
# cv.imshow("img", np.hstack([img, img2]))
cv.waitKey(0)
cv.imwrite("houghcircle-ex2.png", img2)
Binary file added HoughCircleTransform-2/houghcircle-ex1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added HoughCircleTransform-2/houghcircle-ex2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 2 additions & 3 deletions HoughCircleTransform/HoughCircle.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,11 @@
# Bu yüzden, eğer biliyorsak yarıçap aralığını minRadius ve maxRadius ile belirtmeliyiz!
# Yarıçap aratmadan sadece merkezleri döndürmek için maxRadius'u negatif yap.


img = cv.imread("../input_pictures/input_money.png", 0)
img = cv.GaussianBlur(img, (7, 7), 1.5) # Bu kısmı idedeki dökümanda özellikle belirtmiş GaussianBlur() with 7x7
img = cv.GaussianBlur(img, (7, 7), 1.5) # Bu kısmı idedeki dökümanda belirtmiş GaussianBlur() with 7x7
# kernel and 1.5 sigma !!!!! False negatifi baya azalttı.
cimg = cv.cvtColor(img, cv.COLOR_GRAY2BGR)
circles = cv.HoughCircles(img, cv.HOUGH_GRADIENT, 1.3, 30, param1=150, param2=80, minRadius=0, maxRadius=0)
circles = cv.HoughCircles(img, cv.HOUGH_GRADIENT, 1.3, 30, param1=150, param2=70, minRadius=0, maxRadius=0)
# ide dökümanında param1 için 300 civarı iyi diyor. 60 gibi düşük değerler verdiğimde bazılarını algılayamamıştı.
# param2 için küçük değerler verince false positive çok artıyor.

Expand Down
Binary file modified HoughCircleTransform/houghcircle.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 6 additions & 1 deletion ImagePyramids/ImagePyramids.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import cv2 as cv
import sys

# Normalde sabit boyutlu bir görüntü ile çalışıyorduk.
# Ancak bazı durumlarda aynı görüntünün farklı çözünürlükleriyle çalışmamız gerekir.
Expand All @@ -9,7 +10,11 @@

""" 1. Gaussian Pyramids *********************************"""

gray = cv.imread("../input_pictures/input_agac.png")
gray = cv.imread(cv.samples.findFile("../input_pictures/input_agac.png"))

if gray is None:
sys.exit("coulnt read the image")

lower_reso = cv.pyrDown(gray) # level_1
cv.imwrite("lower_reso.png", lower_reso)

Expand Down
3 changes: 3 additions & 0 deletions ImageSegmentationWithWatershedAlg/WatershedAlg.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import cv2 as cv
import numpy as np

Binary file added input_pictures/input_agacyasi.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added input_pictures/input_bobin.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added input_pictures/input_circles.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added input_pictures/input_money2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added input_pictures/input_soda.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 7dc6ea5

Please sign in to comment.