๐ ๊ณต๋ถํ๋ ์ง์ง์ํ์นด๋ ์ฒ์์ด์ง?
5. OpenCV๋ก ์ด๋ฏธ์ง ์ฐจ์ด ๋ถ์ํ๊ธฐ – “์ฒ์ฒด ์ด๋ฏธ์ง๋ก ๋ช ์์ฑ์ ์ฐพ์๋ณด์” ๋ณธ๋ฌธ
5. OpenCV๋ก ์ด๋ฏธ์ง ์ฐจ์ด ๋ถ์ํ๊ธฐ – “์ฒ์ฒด ์ด๋ฏธ์ง๋ก ๋ช ์์ฑ์ ์ฐพ์๋ณด์”
์ง์ง์ํ์นด 2022. 10. 20. 13:50
221020 ์์ฑ
<๋ณธ ๋ธ๋ก๊ทธ๋์ค์ ํ์ด์ฌ ํธ์ฆ์จ ํ๋ก์ ํธ์ github ๋ฅผ ์ฐธ๊ณ ํด์ ๊ณต๋ถํ๋ฉฐ ์์ฑํ์์ต๋๋ค>
https://www.onlybook.co.kr/m/entry/python-projects
์ค์ ํ์ด์ฌ ํธ์ฆ์จ ํ๋ก์ ํธ
์ค์ ํ์ด์ฌ ํธ์ฆ์จ ํ๋ก์ ํธ ๋ฌธ์ ํด๊ฒฐ๊ณผ ์ค๋ฌด ์์ฉ๋ ฅ์ ํค์ฐ๊ธฐ ์ํ ๋๋ง์ ํ์ด์ฌ ํฌํธํด๋ฆฌ์ค ๋ง๋ค๊ธฐ ๋ฆฌ ๋ณธ ์ง์ | ์คํ์ ์ฎ๊น 420์ชฝ | 28,000์ | 2022๋ 5์ 31์ผ ์ถ๊ฐ | 185*240*20 | ISBN13 9791.
www.onlybook.co.kr
https://github.com/rlvaugh/Real_World_Python
GitHub - rlvaugh/Real_World_Python: Code and supporting files for book Real World Python
Code and supporting files for book Real World Python - GitHub - rlvaugh/Real_World_Python: Code and supporting files for book Real World Python
github.com
๐ OpenCV์ NumPy
: ํด๋ผ์ด๋ ํฐ๋ณด๊ฐ 1930๋ ๋ช ์์ฑ์ ๋ฐ๊ฒฌํ ๋ ์ฌ์ฉํ ์ฅ์น์ธ ‘๋ฐ์ง ๋น๊ต์ ’์ ๋ณต์
: ํ๋์ ์ธ ์ปดํจํฐ ๋น์ ๊ธฐ์ ์ ์ฌ์ฉํด ์๋์ผ๋ก ํ์ฑ์ด๋ ์ด์๊ณผ ๊ฐ์ด ๋ณ ์์ผ์์ ์ด๋ ์ค์ธ ์๊ฐ์ ์ธ ์ฒ์ฒด๋ฅผ ์ฐพ์๋ธ๋ค.
# """ํ์ด๋ง๋ ์ด๋ฏธ์ง๊ฐ ์๋ 2๊ฐ์ ํด๋๋ฅผ ์ํํ๊ณ ์ด๋ฏธ์ง๋ฅผ ๋ฑ๋กํ๊ณ ๊น๋ฐ์"""
def main():
night1_files = sorted(os.listdir('./night_1'))
night2_files = sorted(os.listdir('./night_2'))
path1 = Path.cwd() / './night_1'
path2 = Path.cwd() / './night_2'
path3 = Path.cwd() / './night_1_registered'
for i, _ in enumerate(night1_files):
img1 = cv.imread(str(path1 / night1_files[i]), cv.IMREAD_GRAYSCALE)
img2 = cv.imread(str(path2 / night2_files[i]), cv.IMREAD_GRAYSCALE)
print("Comparing {} to {}.\n".format(night1_files[i], night2_files[i]))
# ํต์ฌ ํฌ์ธํธ์ ๊ทธ๋ค ์ฌ์ด์ ๊ฐ์ฅ ์ข์ ์ผ์น๋ฅผ ์ฐพ๊ธฐ
kp1, kp2, best_matches = find_best_matches(img1, img2)
img_match = cv.drawMatches(img1, kp1, img2, kp2,
best_matches, outImg=None)
# ๋ ์ด๋ฏธ์ง ์ฌ์ด์ ์ ์ ๊ทธ๋ฆผ
height, width = img1.shape
cv.line(img_match, (width, 0), (width, height), (255, 255, 255), 1)
QC_best_matches(img_match) # ๋ฌด์ํ๋ ค๋ฉด ์ฃผ์ ์ฒ๋ฆฌ
# ํคํฌ์ธํธ๋ฅผ ์ฌ์ฉํ์ฌ ์ผ์ชฝ ์ด๋ฏธ์ง๋ฅผ ๋ฑ๋ก
img1_registered = register_image(img1, img2, kp1, kp2, best_matches)
# QC ๋ฑ๋ก ๋ฐ ๋ฑ๋ก๋ ์ด๋ฏธ์ง ์ ์ฅ(์ ํ ๋จ๊ณ):
blink(img1, img1_registered, 'Check Registration', num_loops=5)
out_filename = '{}_registered.png'.format(night1_files[i][:-4])
cv.imwrite(str(path3 / out_filename), img1_registered) # ๋ฎ์ด์ด๋ค
cv.destroyAllWindows()
# ๊น๋ฐ์ ๋น๊ต๊ธฐ ์คํ
blink(img1_registered, img2, 'Blink Comparator', num_loops=15)
# """๋ ์ด๋ฏธ์ง์ ๋ํ ํคํฌ์ธํธ ๋ชฉ๋ก๊ณผ ๊ฐ์ฅ ์ ์ผ์นํ๋ ๋ชฉ๋ก์ ๋ฐํ"""
def find_best_matches(img1, img2):
orb = cv.ORB_create(nfeatures=100) # ORB ๊ฐ์ฒด๋ฅผ ์์
# ORB๋ก ํคํฌ์ธํธ์ ๋์คํฌ๋ฆฝํฐ๋ฅผ ์ฐพ๊ธฐ
kp1, desc1 = orb.detectAndCompute(img1, mask=None)
kp2, desc2 = orb.detectAndCompute(img2, mask=None)
# Brute Force Matcher๋ฅผ ์ฌ์ฉํ์ฌ ํคํฌ์ธํธ ์ผ์น๋ฅผ ์ฐพ๊ธฐ
bf = cv.BFMatcher(cv.NORM_HAMMING, crossCheck=True)
matches = bf.match(desc1, desc2)
# ๊ฑฐ๋ฆฌ์ ์ค๋ฆ์ฐจ์์ผ๋ก ์ผ์น ํญ๋ชฉ์ ์ ๋ ฌํ๊ณ ์ต์์ n๊ฐ ์ผ์น ํญ๋ชฉ์ ์ ์ง
matches = sorted(matches, key=lambda x: x.distance)
best_matches = matches[:MIN_NUM_KEYPOINT_MATCHES]
return kp1, kp2, best_matches
# """์ปฌ๋ฌ ๋ผ์ธ์ผ๋ก ์ฐ๊ฒฐ๋ ์ต์์ ํคํฌ์ธํธ ์ผ์น๋ฅผ ๊ทธ๋ฆฐ๋ค"""
def QC_best_matches(img_match):
cv.imshow('Best {} Matches'.format(MIN_NUM_KEYPOINT_MATCHES), img_match)
cv.waitKey(2500) # ์ฐฝ์ 2.5์ด ๋์ ํ์ฑ ์ํ๋ก ์ ์ง
# """๋ ๋ฒ์งธ ์ด๋ฏธ์ง์ ๋ฑ๋ก๋ ์ฒซ ๋ฒ์งธ ์ด๋ฏธ์ง๋ฅผ ๋ฐํ"""
def register_image(img1, img2, kp1, kp2, best_matches):
if len(best_matches) >= MIN_NUM_KEYPOINT_MATCHES:
src_pts = np.zeros((len(best_matches), 2), dtype=np.float32)
dst_pts = np.zeros((len(best_matches), 2), dtype=np.float32)
for i, match in enumerate(best_matches):
src_pts[i, :] = kp1[match.queryIdx].pt
dst_pts[i, :] = kp2[match.trainIdx].pt
# findHomography : ๋ ํ๋ฉด ์ฌ์ด์ ํฌ์ ๋ณํ(Perspective transform)์ ์๋ฏธ, homography matrix, H ์ถ์ถ
h_array, mask = cv.findHomography(src_pts, dst_pts, cv.RANSAC)
height, width = img2.shape # ์ด๋ฏธ์ง 2์ ์น์๋ฅผ ๊ฐ์ ธ์ค๊ธฐ
# warpPerspective : ์๊ทผ ๋งต ํ๋ ฌ์ ๋ํ ๊ธฐํํ์ ๋ณํ์ ์ํ
img1_warped = cv.warpPerspective(img1, h_array, (width, height))
return img1_warped
else:
print("WARNING: Number of keypoint matches < {}\n".format
(MIN_NUM_KEYPOINT_MATCHES))
return img1
# """๋ ๊ฐ์ ์ด๋ฏธ์ง๋ก ๊น๋ฐ์ ๋น๊ต๊ธฐ๋ฅผ ๋ณต์ """
def blink(image_1, image_2, window_name, num_loops):
for _ in range(num_loops):
cv.imshow(window_name, image_1)
cv.waitKey(330)
cv.imshow(window_name, image_2)
cv.waitKey(330)
if __name__ == '__main__':
main()
๐ WSL2 UBUNTU GUI๋ก ์ด๋ฏธ์ง ์ฐจ์ด ๋ถ์ํ๊ธฐ
WSL2 UBUNTU GUI๋ก ์ฌ์ฉํ๊ธฐ
www.youtube.com/watch?v=IL7Jd9rjgrM&list=LL&index=1 dev.to/darksmile92/linux-on-windows-wsl-with-desktop-environment-via-rdp-522g Linux on Windows: WSL with Desktop Environment via RDP WSL (Windows..
qkrm.tistory.com
๊ณ ๋๋ก ๋ฐ๋ผํ์ธ์ฉ
'๐ฉโ๐ป IoT (Embedded) > Image Processing' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
OpenCV ์์ ๊ตฌ์กฐ์ ํํ (0) | 2022.10.20 |
---|---|
OpenCV์ ์ปดํจํฐ ๋น์ & ์์์ฒ๋ฆฌ (0) | 2022.10.20 |
์์ ์ฒ๋ฆฌ ๋ถ์ผ์์ ์ด์ ํ์ง(Anomaly Detection) (1) | 2022.10.18 |
2D ์์์ฒ๋ฆฌ์ 3D ์์์ฒ๋ฆฌ (0) | 2022.10.18 |
1. ๋ฒ ์ด์ฆ ํ๋ฅ ๋ก ์์น ์์ธกํ๊ธฐ โ์ค์ข ๋ ๋ํ์ ์ ์์ ์ฐพ์๋ผโ (0) | 2022.10.17 |