100일 챌린지/컴퓨터비전

Day 2 - Probability, OpenCV_matplotlib

ksyke 2024. 11. 13. 22:15

목차

    Probability

    sample(오메가) : 실험에서 나올 수 있는 모든 결과

    random variable : 어떤 사건에 대해 나올 수 있는 값을 결정하는 함수(function)

    Event Space: 샘플 셋이 나올 수 있는 power set

    Bayes' Theorem

    posterior ∝ linklihood × prior

    Gaussian Distribution (정규 분포)

    평균과 분산을 가지고 estimate을 할 수 있다.

    OpenCV_matplotlib

    OpenCV

    컴퓨터 비전을 목적으로 하는 오픈 소스 라이브러리

    pip install opencv-python

    https://docs.opencv.org/4.x/index.html

    [OpenCV: OpenCV modules

    OpenCV 4.10.0-dev Open Source Computer Vision

    docs.opencv.org](https://docs.opencv.org/4.x/index.html)

    Matpoltlib

    python과 numpy array를 기반으로 plotting과 시각화를 목적으로 하는 라이브러리

    pip install matplotlib

    https://matplotlib.org/stable/index.html

    [Matplotlib documentation — Matplotlib 3.9.2 documentation

    Matplotlib 3.9.2 documentation Matplotlib is a comprehensive library for creating static, animated, and interactive visualizations. Install For more detailed instructions, see the installation guide. Learn What's new Learn about new features and API change

    matplotlib.org](https://matplotlib.org/stable/index.html)

    Image Read and Visualize

    import cv2
    import matplotlib.pyplot as plt
    
    im_name='img1.jpg'
    image=cv2.imread(im_name)
    image
    
    // 이미지 나타내기
    plt.imshow(image)

    Multiple Image Visualize, figure/axs in matplotlib

    // 이미지  rgb로 변환
    image_rgb=cv2.cvtColor(image,cv2.COLOR_BGR2RGB)
    plt.imshow(image_rgb)
    
    // Grayscale로 이미지 나타내기
    image_gray=cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    plt.imshow(image_gray,cmap='gray')
    
    // 세 장의 이미지 병렬로 표현하기
    im_name='img1.jpg'
    image=cv2.imread(im_name)
    image=cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
    image_rgb=cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
    image_gray=cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    
    plt.figure(figsize(15,8))
    
    plt.subplot(1,3,1)
    plt.imshow(image)
    plt.axis('off')
    plt.title('Image 1')
    
    plt.subplot(1,3,2)
    plt.imshow(image_rgb)
    plt.axis('off')
    plt.title('Image 2')
    
    plt.subplot(1,3,3)
    plt.imshow(image_gray)
    plt.axis('off')
    
    plt.show()

    라인 플롯

    import matplotlib.pyplot as plt
    import numpy as np
    a=np.linespace(0,10,100)
    b=np.exp(-a)
    plt.plot(a,b)
    plt.show()

    히스토그램

    import matplotlib.pyploy as plt
    from numpy.random import normal,rand
    x=normal(size=200)
    plt.hist(x,bins=30)
    plt.show()

    산점도

    import matplotlib.pyplot as plt
    from numpy.random import rand
    a=rand(100)
    b=rand(100)
    plt.scatter(a,b)
    plt.show()

    3D 플롯

    from matplotlib import cm
    from mpl_toolkits.mplot3d import Axes3D
    import matplotlib.pyplot as plt
    import numpy as np
    fig=plt.figure()
    ax=fig.gca(projection='3d')
    X=np.arange(-5,5,0.25)
    Y=np.arange(-5,5,0.25)
    X,Y=np.meshgrid(X,Y)
    R=np.sqrt(X**2+Y**2)
    Z=np.sin(R)
    surf=ax.plot_surface(X,Y,Z,rstride=1,cstride=1,cmap=cm.coolwarm)
    plt.show()

    Image Cropping, masking and save

    import cv2
    import matplotlib.pyplot as plt
    def read_image(im_name):
    	image=cv2.imread(im_name)
        image_rgb=cv2.cvtColor(image,cv2.COLOR_BGR2RGB)
        plt.imshow(image_rgb)
        plt.show()
        return image_rgb
        
     ## To make function and read image2
     im1=read_image('image.jpeg')
     im2=read_image('image2.jpeg')
     
     print(im1.shape,im2.shape)

    image cropping

    h1,w1=im1.shape[:2]
    h2,w2=im2.shape[:2]
    size=720
    
    def center_crop(im,h,w,size=720):
    	im=im[int(h/2-size/2):int(h/2+size/2), int(w/2-size/2):int(w/2+size/2)]
        return im
        
    im1=center_crop(im1,h1,w1,size);
    im2=center_crop(im2,h2,w2,size)
    
    def two_image_show(im1,im2):
    	plt.subplot(1,2,1)
        plt.imshow(im1)
        plt.subplot(1,2,2)
        plt.imshow(im2)
        plt.show()
        
    two_image_show(im1,im2)

    image masking

    import numpy as np
    
    im1_mask=np.zeros_like(im1)
    im1_mask[int(size/2:,]=im1[int(size/2):,]
    im2_mask=np.zeros_like(im2)
    im2_mask[:int(size/2),]=im2[:int(size/2),]
    
    two_image_show(im1_mask,im2_mask)
    
    merge=cv1.bitwise_or(im1_mask,im2_mask)
    plt.imshow(merge)
    plt.show()

    circle mask

    circle_mask=np.zeros_like(im2)
    # cv2.circle(대상이미지,(원점x,원점y),반지름,(색상),채우기)
    cv2.circle(circle_mask,(size//2,size//2),size//3,(255,255,255),-1)
    
    masked)im2=cv2.bitwise_and(im2,circle_mask)
    
    plt.imshow(masked_im2)

    저장하기

    plt.imsave('save_img.plg',masked_im2)

    Image matching and visualize

    import numpy as np
    import cv2 as cv
    import matplotlib.pyplot as plt
    
    queryImage='cv_cover.jpg'
    trainImage='cv_desk.png'
    
    plt.figure(figsize=(15,8))
    plt.subplot(1,2,1)
    plt.imshow(cv.cvtColor(cv.imread(queryImage),cv.COLOR_BGR2RGB))
    plt.title('QueryImage')
    plt.subplot(1,2,2)
    plt.imshow(cv.cvtColor(cv.imread(trainImage),cv.COLOR_BGR2RGB))
    plt.title('Dest image')
    plt.show()
    
    img1=cv.imread(queryImage,cv.IMREAD_GRAYSCALE)
    img2=cv.imread(trainImage,cv.IMREAD_GRAYSCALE)
    
    # Initiate IRB detector
    orb=cv.ORB_create()
    
    # find the keypoints and descriptiors with ORB
    kp1,des1=orb.detoectAndConpute(img1,None)
    kp2,des2=orb.detectAndCompute(img2,None)
    
    # create BFMatcher object
    bf=cv.BFMatcher(cv.NORM_HAMMING,crossCheck=True)
    
    #matches=bf.match(des1,des2)
    
    # Sort them in the order of their distance
    matches=sorted(matches,key=lanbda x:x.distance)
    
    # Draw first 10 matches
    img3=cv.drawMatches(img1,kp1,img2,kp2,matches[:10],None,flags=cv.DrawMatchesFlags_NOT_DRAW_SINGLE_POINT
    
    plt.figure(figsize=(15,8))
    plt.imshow(img3)
    plt.show()

    Find homography and warping

    # Extract location of good matches
    points1=np.zeros((len(matches),2),dtype=np.float32)
    points2=np.zeros((len(matches),2),dtype=np.float32)
    
    for i,match in enumerate(matches):
    	points1[i,:]=kp1[match.queryIdx].pt
    	points2[i,:]=kp2[match.trainIdx].pt
        
    h,mask=cv.findHomography(points1,points2,cv.RANSAC)
    
    height,width=img2.shape
    
    im1_warped=cv.warpPerspective(img1,h,(width,height)
    
    
    plt.figure(figsize=(15,8))
    plt.subplot(1,3,1)
    plt.imshow(img1,cmap='gray')
    plt.subplot(1,3,2)
    plt.imshow(img2,cmap='gray')
    plt.subplot(1,3,3)
    plt.imshow(img1_warped,cmap='gray')
    plt.show()

    Brute-Force matching with SIFT Descriptors and Ratio Test

    # Initiate SIFT detector
    sift=cv.SIFT_create()
    
    # find the keypoints and descriptos with SIFT
    kp1,des1=sift.detectAndCompute(img1,None)
    kp2,des2=sift.detectAndCompute(img2,None)
    
    # BFMatcher with default params
    bf=cv.BFMatcher()
    matches=bf.knnMatch(des1,des2,k=2)
    
    # Apply ratio test
    good=[]
    for m,n in matches:
    	if m.distance < 0.75*n.distance:
        	good.append([m])
            
    # cv.drawMatchesKnn expects list of lists as matches
    img3=cv.drawMatchesKnn(img1,kp1,img2,kp2,good,None,flags=cv.DrawMatchesFlags_NOT_DRAW_SINGLE_POINT
    
    plt.figure(figsize=(15,8))
    plt.imshow(img3)
    plt.show()

     

    '100일 챌린지 > 컴퓨터비전' 카테고리의 다른 글

    Day 3 - PIL, pytorch-torchvision, logging-wandb  (1) 2024.11.19
    Day 1 - linear algebra  (1) 2024.11.12