๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ’ก EE's DEV/์˜์ƒ์ฒ˜๋ฆฌ

[์˜์ƒ์ฒ˜๋ฆฌ] openCV2 ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์ด์šฉํ•˜๊ธฐ - Canny Edge detector, Finding contours

by Danna 2017. 2. 27.
728x90
728x90

OpenCV ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์˜ˆ์ œ


  • Sobel Derivatives   
    https://docs.opencv.org/master/d2/d2c/tutorial_sobel_derivatives.html
  • Laplace Operator   
    https://docs.opencv.org/master/d5/db5/tutorial_laplace_operator.html
  • Canny Edge Detector   
    https://docs.opencv.org/master/da/d5c/tutorial_canny_detector.html
  • Hough Circle Transform
    https://docs.opencv.org/master/d4/d70/tutorial_hough_circle.html
  • Finding contours in your image
    https://docs.opencv.org/master/df/d0d/tutorial_find_contours.html


Canny Edge Detector



Canny Edge Detection ์€ ์•„๋ž˜์™€ ๊ฐ™์€ ์•Œ๊ณ ๋ฆฌ์ฆ˜์œผ๋กœ ๊ตฌ์„ฑ๋˜์–ด ์žˆ๋‹ค.

1๋‹จ๊ณ„ : ๋…ธ์ด์ฆˆ ์ œ๊ฑฐ 
  • 5x5 ๊ฐ€์šฐ์‹œ์•ˆ ํ•„ํ„ฐ๋ฅผ ์ด์šฉํ•ด ์ด๋ฏธ์ง€์˜ ๋…ธ์ด์ฆˆ๋ฅผ ์ค„์—ฌ์ค€๋‹ค.

2๋‹จ๊ณ„ : Gradient ๊ฐ’์ด ๋†’์€ ๋ถ€๋ถ„ ์ฐพ๊ธฐ 

  • Sobel ์ปค๋„์„ ์ˆ˜ํ‰๋ฐฉํ–ฅ, ์ˆ˜์ง๋ฐฉํ–ฅ์œผ๋กœ ์ ์šฉํ•ด ๊ฐ ๋ฐฉํ–ฅ์˜ gradient๋ฅผ ํš๋“ํ•œ๋‹ค.           - 
  • ์ˆ˜ํ‰๋ฐฉํ–ฅ์˜ gradient๋ฅผ Gx, ์ˆ˜์ง๋ฐฉํ–ฅ์˜ gradient๋ฅผ Gy๋ผ๊ณ  ํ• ๋•Œ, G = |Gx| + |Gy|

3๋‹จ๊ณ„ : ์ตœ๋Œ€๊ฐ’์ด ์•„๋‹Œ ํ”ฝ์…€์˜ ๊ฐ’์„ 0์œผ๋กœ ๋งŒ๋“ค๊ธฐ 



  • 2๋‹จ๊ณ„๋ฅผ ๊ฑฐ์นœ ํ›„, ์—์ง€์— ๊ธฐ์—ฌํ•˜์ง€ ์•Š์€ ํ”ฝ์…€์„ ์ œ๊ฑฐํ•˜๊ธฐ ์œ„ํ•ด ์ด๋ฏธ์ง€ ์ „์ฒด๋ฅผ ์Šค์บ”ํ•œ๋‹ค.            - 
  • gradient ๋ฐฉํ–ฅ์€ ์ˆ˜ํ‰๋ฐฉํ–ฅ์ด๊ณ  A๋Š” ์ˆ˜์ง๋ฐฉํ–ฅ์˜ ์—์ง€์œ„์—, B, C๋Š” gradient ๋ฐฉํ–ฅ์— ๋†“์—ฌ์žˆ๋‹ค. 
  • A์ง€์ ์—์„œ gradient ๊ฐ’์ด B, C๋ณด๋‹ค ๊ฐ’์ด ํฐ์ง€ ์•„๋‹Œ์ง€ ์ฒดํฌํ•œ๋‹ค.
  • A์—์„œ์˜ ๊ฐ’์ด ๊ฐ€์žฅ ํฌ๋ฉด ๋‹ค์Œ ๋‹จ๊ณ„๋กœ ๋„˜์–ด๊ฐ€๊ณ , ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด ๊ฐ’์„ 0์œผ๋กœ ๋งŒ๋“ ๋‹ค.

4๋‹จ๊ณ„ : Hyteresis Thresholding


  • 3๋‹จ๊ณ„๋ฅผ ๊ฑฐ์นœ ๊ฒƒ๋“ค์ด ์‹ค์ œ ์—์ง€์ธ์ง€ ์•„๋‹Œ์ง€ ํŒ๋‹จํ•˜๋Š” ๋‹จ๊ณ„

  • Threshold ๊ฐ’์„ minVal, maxVal 2๊ฐœ๋ฅผ ์žก๋Š”๋‹ค.

  • maxVal ๋ณด๋‹ค ๋†’์€ ๋ถ€๋ถ„์€ ํ™•์‹คํ•œ ์—์ง€, minVal๋ณด๋‹ค ๋‚ฎ์€ ๋ถ€๋ถ„์€ ์—์ง€๊ฐ€ ์•„๋‹ˆ๋ผ๊ณ  ํŒ๋‹จ

  • minVal, maxVal ์‚ฌ์ด์— ์žˆ๋Š” ๊ฐ’๋“ค์€ ์ด ํ”ฝ์…€๋“ค์˜ ์—ฐ๊ฒฐ๊ตฌ์กฐ๋ฅผ ๋ณด๊ณ  ์—์ง€์ธ์ง€ ์•„๋‹Œ์ง€ ํŒ๋‹จ



  • A๋Š” maxVal๋ณด๋‹ค ์œ„์— ์žˆ์œผ๋ฏ€๋กœ ํ™•์‹คํ•œ ์—์ง€
  • B, C๋Š” minVal, maxVal ์‚ฌ์ด์— ์žˆ๋Š” ํ”ฝ์…€
  • B๋Š” ํ™•์‹คํ•œ ์—์ง€์™€ ์—ฐ๊ฒฐX ์—์ง€๊ฐ€ ์•„๋‹ˆ๋ผ๊ณ  ํŒ๋‹จ. ์ œ๊ฑฐํ•œ๋‹ค.
  • C๋Š” ํ™•์‹คํ•œ ์—์ง€์™€ ์—ฐ๊ฒฐO ์—์ง€๋ผ๊ณ  ํŒ๋‹จํ•˜์—ฌ ๋‚จ๊ฒจ๋‘”๋‹ค.


OpenCV - Canny ํ•จ์ˆ˜


static void CannyThreshold(int, void*)
{
    blur( src_gray, detected_edges, Size(3,3) ); // minVal, maxVal
    Canny( detected_edges, detected_edges, lowThreshold, lowThreshold*ratio, kernel_size );
    dst = Scalar::all(0);
    src.copyTo( dst, detected_edges);
    imshow( window_name, dst );
}

Canny_Detector_Tutorial_Original_Image.jpgCanny_Detector_Tutorial_Result.jpg


OpenCV - Finding Contour ๊ด€๋ จ ํ•จ์ˆ˜

cv2.findContours(thr, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) 
์ด ํ•จ์ˆ˜๋Š” ์›๋ณธ ์ด๋ฏธ์ง€๋ฅผ ๋ณ€๊ฒฝ์‹œํ‚ค๊ธฐ ๋•Œ๋ฌธ์— ๋ณต์‚ฌ๋ณธ์„ ๊ฐ€์ง€๊ณ  ํ•จ์ˆ˜๋ฅผ ์ ์šฉํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Œ

thresh : contour ์ฐพ๊ธฐ๋ฅผ ํ•  ์†Œ์Šค ์ด๋ฏธ์ง€. thresholding์„ ํ†ตํ•ด ๋ณ€ํ™˜๋œ ๋ฐ”์ด๋„ˆ๋ฆฌ ์ด๋ฏธ์ง€์—ฌ์•ผํ•จ

cv2.RETR_TREE : contour ์ถ”์ถœ ๋ชจ๋“œ

cv2.RETR_EXTERNAL : ์ด๋ฏธ์ง€์˜ ๊ฐ€์žฅ ๋ฐ”๊นฅ์ชฝ์˜ contour๋งŒ ์ถ”์ถœ

cv2.RETR_LIST : contour ๊ฐ„ ๊ณ„์ธต๊ตฌ์กฐ ์ƒ๊ด€๊ด€๊ณ„๋ฅผ ๊ณ ๋ คํ•˜์ง€ ์•Š๊ณ  contour๋ฅผ ์ถ”์ถœ

cv2.RETR_CCOMP : ์ด๋ฏธ์ง€์—์„œ ๋ชจ๋“  contour๋ฅผ ์ถ”์ถœํ•œ ํ›„, 2๋‹จ๊ณ„ contour ๊ณ„์ธต ๊ตฌ์กฐ๋กœ ๊ตฌ์„ฑํ•จ

cv2.RETR_TREE : ์ด๋ฏธ์ง€์—์„œ ๋ชจ๋“  contour๋ฅผ ์ถ”์ถœํ•˜๊ณ  contour๋“ค๊ฐ„์˜ ์ƒ๊ด€๊ด€๊ณ„๋ฅผ ์ถ”์ถœํ•จ


cv2.CHAIN_APPROX_SIMPLE : contour ๊ทผ์‚ฌ ๋ฐฉ๋ฒ•

cv2_CHAIN_APPROX_NONE : contour๋ฅผ ๊ตฌ์„ฑํ•˜๋Š” ๋ชจ๋“  ์ ์„ ์ €์žฅํ•จ

cv2_CHAIN_APPROX_SIMPLE : contour์˜ ์ˆ˜ํ‰, ์ˆ˜์ง, ๋Œ€๊ฐ์„  ๋ฐฉํ–ฅ์˜ ์ ์€ ๋ชจ๋‘ ๋ฒ„๋ฆฌ๊ณ  ๋ ์ ๋งŒ ๋‚จ๊ฒจ๋‘ 

cv2_CHAIN_APPROX_TC39__1 : The-Chin ์—ฐ๊ฒฐ ๊ทผ์‚ฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์ ์šฉํ•จ


cv2.drawContours(img, contours, -1, (0, 255, 0), 3) 
์šฐ๋ฆฌ๊ฐ€ ์ฐพ์€ contou๋ฅผ ์‹ค์ œ๋กœ ๊ทธ๋ฆฌ๋Š” ํ•จ์ˆ˜

img : contou๋ฅผ ๋‚˜ํƒ€๋‚ผ ๋Œ€์ƒ ์ด๋ฏธ์ง€

contours : img์— ๊ทธ๋ฆด contour. ์ด ๊ฐ’์€ cv2.findContours() ํ•จ์ˆ˜์˜ 2๋ฒˆ์งธ ๋ฆฌํ„ด ๊ฐ’์ด๋‹ค.

-1 : img์— ์‹ค์ œ๋กœ ๊ทธ๋ฆด contour ์ธ๋ฑ์Šค ํŒŒ๋ผ๋ฏธํ„ฐ. ์ด ๊ฐ’์ด ์Œ์ˆ˜์ด๋ฉด ๋ชจ๋“  contour๋ฅผ ๊ทธ๋ฆผ

(0, 255, 0) : contour ์„ ์˜ BGR ์ƒ‰์ƒ๊ฐ’. ์—ฌ๊ธฐ์„œ๋Š” Green ๊ฐ’์„ ๋œปํ•จ

1 : contour ์„ ์˜ ๋‘๊ป˜

Find_Contours_Original_Image.jpgFind_Contours_Result.jpg


* OpenCV์—์„œ Contour ์ฐพ๊ธฐ๋Š” ๊ฒ€์ •์ƒ‰ ๋ฐฐ๊ฒฝ์—์„œ ํฐ์ƒ‰ ๋ฌผ์ฒด๋ฅผ ์ฐพ๋Š” ๊ฒƒ๊ณผ ๋น„์Šทํ•˜๊ธฐ ๋•Œ๋ฌธ์—, Contour๋ฅผ ์ฐพ๊ณ ์ž ํ•˜๋Š” ๋Œ€์ƒ์€ ํฐ์ƒ‰์œผ๋กœ ๋ฐฐ๊ฒฝ์€ ๊ฒ€์ •์ƒ‰์œผ๋กœ ๋ณ€๊ฒฝํ•ด์•ผ ํ•จ




  • 2020.12.06 ์ˆ˜์ • (2018.02.14 ์„ธ๋ฏธ๋‚˜ ์ž๋ฃŒ)
  • ์ด๋ฏธ์ง€ ์ถœ์ฒ˜ OpenCV ์˜ˆ์ œ 


728x90
728x90